如何在单击操作按钮后添加新的/单独的数据表

问题描述 投票:0回答:1

我需要帮助编写一个闪亮的应用程序,该应用程序在输入每个相应的输入并单击操作按钮后会输出一个单独的数据表。我让应用程序简单地输出到数据表,但它会覆盖新条目。我检查了 R Shiny 的参考部分,似乎我需要使用 insertUI() 但我不太了解 Shiny 来弄清楚它。

library(shiny)
library(rhandsontable)

#UI
ui <-shinyUI(fluidPage(
  titlePanel('Drug Prep'),
  sidebarPanel(textInput('expid','Experiment ID'),
               textInput('nsc','NSC Number'),
               textInput('solvent','Solvent'),
               numericInput('stkcon','Stock Conc(mg/ml)',0),
               numericInput('Repeat','Repeat',0),
               textAreaInput('instruct','Instructions',height='50px'),
               numericInput('amt','Amt to weigh(mg)',0),
               numericInput('vehicle','Tot_vol of vehicle(ml)',0),
               hr(),
               numericInput('grp','Group',0),
               numericInput('micedose','Mice Dose/vial',0),
               numericInput('dose','Dose(mg/kg)',0),
               numericInput('doseconc','Dose Concentration(mg/ml)',0),
               numericInput('numvial','No. of Vials',0),
               numericInput('volA','Vol of Vehicle_A(ml)',0),
               numericInput('volB','Vol of Vehicle_B(ml)',0),
               numericInput('volC','Vol of Vehicle_C(ml)',0),
               br(),
               actionButton('save','Save')
  ),
  mainPanel(
    textOutput('nsc'),
    verbatimTextOutput('instruct'),
    uiOutput("hot"),
    hr(),
    uiOutput('hot2')
  )
)
)

#SERVER
server=function(input, output) {
  global <- reactiveValues()
  
  observeEvent(input$save, {
    global$data[[length(global$data) + 1]] = data.frame(ExpID=input$expid,NSC=input$nsc,Stock.conc=input$stkcon,
                                                        Repeats=input$Repeat,Amt=input$amt,vol=input$vehicle,
                                                        stringsAsFactors = F)
  })
  observeEvent(input$save, {
    global$ditto[[length(global$ditto) + 1]] = data.frame(group=input$grp,No_of_mice_dosed=input$micedose,dose=input$dose,dose_conc=input$doseconc,
                                                          No_vial=input$numvial,vol_Vehicle_A=input$volA,vol_Vehicle_B=input$volB,vol_Vehicle_C=input$volC,
                                                          tot_vol=input$volA+input$volB+input$volC,stringsAsFactors = F)  
  })
  
  observeEvent(input$save, {
    for(nr in 1:length(global$data)){ 
      local({
        df <- global$data[[nr]]
        output[[paste0("hot", nr)]] <- renderRHandsontable(rhandsontable(df))
      })
    }
  })
  
  observeEvent(input$save, {
    for(nr in 1:length(global$ditto)){
      local({
        df<-global$ditto[[nr]]
        output[[paste0('hot2',nr)]] <- renderRHandsontable(rhandsontable(df))
      })
    }
  })
  output$instruct<-renderText({input$save
    paste(isolate(input$nsc),isolate(input$instruct),sep='\n')
  })
  output$hot <- renderUI({
    lapply(1:length(global$data), function(nr) rHandsontableOutput(paste0("hot", nr)))
    })
  output$hot2<-renderUI({
    lapply(1:length(global$ditto), function(nr) rHandsontableOutput(paste0("hot2", nr)))
  })
}
shinyApp(ui = ui, server=server)

enter image description here

我希望应用程序对这个 Excel 电子表格进行微型化,请注意每个表(提交)的列数有何不同。

r shiny rhandsontable action-button
1个回答
1
投票

我不是 100% 清楚您是想向表中添加新条目还是想要执行其他操作(例如替换表)。下面您将找到一个在表中添加新行的示例。我还添加了一项检查,以便使用

duplicated
函数不会输入重复项。

library(shiny)
library(rhandsontable)

#UI
ui <-shinyUI(fluidPage(
  titlePanel('Drug Prep'),
  sidebarPanel(textInput('expid','Experiment ID'),
               textInput('nsc','NSC Number'),
               textInput('solvent','Solvent'),
               numericInput('stkcon','Stock Conc(mg/ml)',0),
               numericInput('Repeat','Repeat',0),
               textAreaInput('instruct','Instructions',height='50px'),
               numericInput('amt','Amt to weigh(mg)',0),
               numericInput('vehicle','Tot_vol of vehicle(ml)',0),
               hr(),
               numericInput('grp','Group',0),
               numericInput('micedose','Mice Dose/vial',0),
               numericInput('dose','Dose(mg/kg)',0),
               numericInput('doseconc','Dose Concentration(mg/ml)',0),
               numericInput('numvial','No. of Vials',0),
               numericInput('volA','Vol of Vehicle_A(ml)',0),
               numericInput('volB','Vol of Vehicle_B(ml)',0),
               numericInput('volC','Vol of Vehicle_C(ml)',0),
               br(),
               actionButton('save','Save')
  ),
  mainPanel(
    textOutput('nsc'),
    verbatimTextOutput('instruct'),  
    rHandsontableOutput("hot"),
    br(),
    rHandsontableOutput('hot2')),
  hr()

)
)

#SERVER
server=function(input, output, session) {

  output$instruct <- renderText({input$save
    paste(isolate(input$nsc),isolate(input$instruct),sep='\n')
  })
  mydf <- reactiveValues()

  observeEvent(input$save,{
    data <- data.frame(ExpID=input$expid,NSC=input$nsc,Stock.conc=input$stkcon,
                       Repeats=input$Repeat,Amt=input$amt,vol=input$vehicle,
                       stringsAsFactors = F)
    mydf$df <- rbind(mydf$df,data)
    mydf$df <- mydf$df[!duplicated(mydf$df), ]

    data2 <- data.frame(group=input$grp,No_of_mice_dosed=input$micedose,dose=input$dose,dose_conc=input$doseconc,
                        No_vial=input$numvial,vol_Vehicle_A=input$volA,vol_Vehicle_B=input$volB,vol_Vehicle_C=input$volC,
                        tot_vol=input$volA+input$volB+input$volC,stringsAsFactors = F)
    mydf$df2 <- rbind(mydf$df2,data2)
    mydf$df2 <- mydf$df2[!duplicated(mydf$df2), ]
  })

  output$hot <- renderRHandsontable({ 
    if(is.null(mydf$df)){
      return()
    }
    rhandsontable(mydf$df)
  })  

  output$hot2 <- renderRHandsontable({
    if(is.null(mydf$df2)){
      return()
    }
    rhandsontable(mydf$df2)
  })

}
shinyApp(ui = ui, server=server)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.