我在开发一个复杂的Shiny App,想为用户提供将输入保存到Rdata文件中以便于后续加载的功能。
我已经通过使用downloadhandler
、fileInput
和renderUI
实现了这个功能,但是由于有超过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)
})
}
))