2

I need help writing a shiny app that will spit out a separate datatable after entering into each respective input and clicking the actionbutton. I got the app to simply output to the datatable, but it will overwrite upon a new entry. I checked the reference section of R Shiny and it seems I need to use insertUI() but I don't really understand Shiny very well to figure it out. Please help. Thanks.

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

I want the app to minic this excel spreadsheet, notice how the amount of columns differ for each table (submission).

4

1 回答 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)

在此处输入图像描述

于 2017-05-05T07:23:11.407 回答