2014-04-06 17 views
5

Lavoro su una lunga app Shiny in cui voglio dare la possibilità all'utente di salvare l'input in un file Rdata per caricarlo dopo.Salva l'input nel file per continuare in un'altra sessione l'analisi dei dati in R Shiny App

riesco a farlo con downloadhandler, fileInput e renderUI, ma ho più di 200 di ingresso, sono sicuro che ci sia un modo semplice.

ogni idea sono i benvenuti, Grazie in anticipo

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

risposta

0

Forse questa voce su GitHub da "aagarw30/R-Shinyapp-Tutorial" sarebbe utile. Memorizzare un contatore di visitatori in un file separato è simile al tuo dilemma.

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

Il codice server.R carica aggiornamenti numero in un file counter.Rdata separata utilizzando questo codice:

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