【问题标题】:Add rows directly in a r shiny apps直接在闪亮的应用程序中添加行
【发布时间】:2013-12-09 15:11:09
【问题描述】:

我正在开发一个带有 2 个表格、1 个图表和一个条件答案的 Shiny 应用程序。

我想直接在 Shiny 中向表中添加一行并更新结果。

例如,在具有 id、2 个日期和 2 个多项选择题的表格中。我有 3 个观察结果,我希望用户能够添加 n others 观察结果,这应该会导致其他表格和图表使用这些新观察结果进行更新。

有可能吗?如果是,你如何做到这一点?

谢谢你,原谅我的英语不好。

这里是我的 Shiny 应用程序的链接 http://www.hostingpics.net/viewer.php?id=459723Shiny.png

这里是工作的代码:

https://gist.github.com/wolf6541/7874968

我已经为我的问题尝试过这个:

**Ui.r**
 #Titre
    h3("Ajout d'un nouveau patient"), 

#Date inclusion
dateInput("date_inclu", "Date de l'inclusion", value = ,language = "fr"),

#Date reponse
dateInput("date_rep", "Date de reponse", value = ,language = "fr"),

#Dose
textInput("dose", "Dose administree", ),

#Reponse
textInput("tox", "Reponse toxicite", ),

updateData()


**Server.R**
    identifiant =length(data_time$identifiant)
    dose =reactive(input$dose)
    date_inclusion =reactive(input$date_inclu)
    date_reponse =reactive(input$date_rep)
    reponse =reactive(input$tox)

    nouveau_pat = cbind(identifiant,  dose,  date_inclusion,    date_reponse,   reponse)
    nouveau_pat = as.data.frame(nouveau_pat)

    data_time = rbind(data_time,nouveau_pat)

但这不起作用

【问题讨论】:

  • 添加您迄今为止尝试过的内容。如果 Stackoverflow 社区可以查看您的尝试,他们将能够为您提供帮助。
  • 感谢您的帮助,我刚刚更新了我的帖子

标签: r interface rows rstudio shiny


【解决方案1】:

一种方法是依靠shinyIncubator 包的matrixInput 函数。 shinyIncubator 包具有开发功能,有时最终会与shiny 合并。鉴于它的惊人之处,我希望 matrixInput 或类似的东西将成为我们有生之年基础 shiny 的一部分。

我做了一个演示:

library("shiny")
library("shinyIncubator")
runGist("https://gist.github.com/anonymous/8449201")

对我来说,最难的部分是让 matrixInput 显示其 colnames,默认情况下是隐藏的。您需要在这里做的是自定义显示样式。您可以在 css 中执行此操作,或者像我一样,在您的 ui.R 正文中使用标签。关键是将display: table-header-group分配给.tableinput .hide。其他显示功能可以通过浏览器的inspect element 选项找到:Firefox 和 Chrome 都具有该功能;其他浏览器也可能这样做。

tags$head(
  tags$style(type = "text/css"
    , ".tableinput .hide {display: table-header-group;}"
  )
)

【讨论】:

    【解决方案2】:

    我终于找到了办法,

    服务器.R

    #Stockage des donnes
      makeData <- observe({
        Data
        write.table(t(c(input$Identifiant,
                        as.character(input$datei),
                        input$dose,
                        as.character(input$dater),
                        input$reponse)),file="donnees",sep="\t",col.names=F,row.names=F,append=T)
      })
    
      #affichage table pat
      output$table_pat <- renderTable({
        table_pat = data.frame(Identifiant = input$Identifiant,
                               Date_inclusion =as.character(input$datei),
                               Dose = input$dose,
                               Date_reponse =as.character(input$dater),
                               Reponse = input$reponse)
      },include.rownames=FALSE)
    

    ui.r

    shinyUI(pageWithSidebar(
      headerPanel("OPTIMUM TRIAL"),
    
      sidebarPanel(
    
        h3("Ajout d'un nouveau patient"),
        #Identifiant
        textInput("Identifiant","Identifiant du patient:",value =""),
    
        #Dose
        selectInput("dose", "Dose:",
                    c(1,2,3,4,5,6), selected = 1),
        #Toxicite
        selectInput("reponse", "Reponse:",
                    c("Succes","Echec"), selected = "Succes"),
    
        #Date inclusion
        dateInput("datei", "Date de l'inclusion:", format = "yyyy-mm-dd",value =Sys.Date(),language = "fr"),
    
        #Date reponse
        dateInput("dater", "Date de reponse:", format = "yyyy-mm-dd",value =Sys.Date(),language = "fr"),
    
        submitButton("Ajouter le patient"),
    
        h3("Supprimer un patient"),
    
        textInput("Supprimer","Identifiant du patient a supprimer:", value = ""),
    
        submitButton("Supprimez le patient")
    
      ),
    
      mainPanel(
    
        h3("Nouveau patient"),
        tableOutput("table_pat"),
    
        h3("Historique des patients"),
        tableOutput("Data"),
    

    输入数据写入文件文本,并在代码开头使用 read.table 来更新表。但是,您需要重新启动您的应用程序。可能有更好的方法可以在不重新启动应用程序的情况下进行更新,但我还没有找到它

    您可以使用以下函数来避免第一次读取数据默认值(在stackoverflow中的另一个问题中找到(Delayed execution in R Shiny app

    #Fonction pour ne pas imprimer lors du lancement de la page
      values <- reactiveValues(starting = TRUE)
      session$onFlushed(function() {
        values$starting <- FALSE
      })
    
      #Stockage des donnees
      makeData <- observe({
        if (values$starting){
          return(NULL)}
    
        else {
          write.table(....
    
      })
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2017-07-27
      • 2013-07-08
      • 2018-06-11
      • 1970-01-01
      • 2021-11-05
      • 2015-06-15
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多