将输入保存到文件中以在其他会话中继续R Shiny App中的数据分析

时间:2014-04-06 07:18:24

标签: r shiny

我在一个很长的Shiny应用程序上工作,我希望用户可以将输入保存在Rdata文件中,以便以后加载它。

我设法通过downloadhandlerfileInputrenderUI执行此操作, 但我有200多个输入,我相信有一个简单的方法。

欢迎所有想法,提前致谢

迪米特里

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 :(得分:0)

也许这个来自“aagarw30 / R-Shinyapp-Tutorial”的GitHub上的条目会很有用。将访客计数器存储在单独的文件中与您的困境类似。

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

server.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)
  })