将输入保存到文件中,以便在另一个会话中继续在R Shiny应用程序中进行数据分析

6

我在开发一个复杂的Shiny App,想为用户提供将输入保存到Rdata文件中以便于后续加载的功能。

我已经通过使用downloadhandlerfileInputrenderUI实现了这个功能,但是由于有超过200个输入框,我相信一定还有更简单的方法。

欢迎提出任何想法,提前感谢。

Dimitri

shiny::runApp(list(
  ui = pageWithSidebar(
    headerPanel("Save Input"),
    sidebarPanel(
      downloadButton("download.input","Download Input"),
      ## Bolean to read or not the old input of the file load bellow
      checkboxInput("use.list.input","Use Rdata for input",F),
      fileInput('file.Rdata','Reload the input of a last session')
      ),
    mainPanel(
      ## All the input will become uiOUtput
      uiOutput("num1"),
      uiOutput("num2")      
    )
  ),   
  server = function(input,output){
    ## The downloadHandler to write the current input
    output$download.input <- downloadHandler(
      filename = function() { paste0("input", '.csv') },
      content = function(name) {
        write.table(save.input(), file=name)
      }
    ) 
    ### Two object, one for write the current input, one for read the old input
    save.input<-reactive({
      data<-cbind(c("number1","number2"),c(input$number1,input$number2))
      return(data)
    })
    table.input<-reactive({
      inFile<-input$file.Rdata
      table.input<-read.table(inFile$datapath)
      return(table.input)
    })
    ### RenderUI ###
    output$num1<-renderUI({
      if(input$use.list.input==T){
          default<-table.input()[1,2]       
      }else{default<-1}     
      numericInput("number1","number1",default)
    })
    output$num2<-renderUI({
      if(input$use.list.input==T){
          default<-table.input()[2,2]        
      }else{default<-2}     
      numericInput("number2","number2",default)
    })
  }
))
1个回答

0
也许来自“aagarw30/R-Shinyapp-Tutorial”GitHub的这篇文章会有所帮助。将访问计数器存储在单独的文件中与您的困境类似。

https://github.com/aagarw30/R-Shinyapp-Tutorial/tree/master/ShinyAppVisitorHitCounter

服务器端的R代码使用以下代码将数字更新加载到单独的counter.Rdata文件中:
 output$counter <- 
    renderText({
      if (!file.exists("counter.Rdata")) 
        counter <- 0
      else
        load(file="counter.Rdata")
      counter  <- counter + 1
     save(counter, file="counter.Rdata")     
     paste("Hits: ", counter)
  })

这并不能解决问题。 - AnilGoyal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接