R Shiny 应用程序可保存输入以供以后使用

Rus*_*ian 7 r shiny

我正在编写一个闪亮的应用程序,它将有许多用户输入,因此我希望有一个选项,允许用户保存他们的输入并将其加载回来以供以后使用。我能够按照我在网上找到的示例来执行此操作,但现在我想寻求一些更改代码功能的帮助。这是代码:

\n\n
library(shiny)  \n\nui <- shinyUI(fluidPage(\n  br(),\n\n  actionButton("load_inputs", "Load inputs"),\n  br(),\n  br(),\n\n  numericInput("n", "Number",min = 1, value = 5),\n  numericInput("upper", "Upper",min = 0, max = 100, value = 15),\n  numericInput("lower", "Lower",min = 0, max = 100, value = 5),\n\n  actionButton(\'save_inputs\', \'Save inputs\')\n\n)) \n\nserver <-  shinyServer(function(input, output,session) { \n\n  switch(Sys.info()[[\'sysname\']],\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 Windows= {setwd(file.path(Sys.getenv("USERPROFILE"),"Desktop",fsep="\\\\"))},\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 Mac = { \xc2\xa0setwd("~/Desktop/")})\n\n  observeEvent(input$load_inputs,{   \n\n    if(!file.exists(\'inputs.RDS\')) {return(NULL)}\n\n    savedInputs <- readRDS(\'inputs.RDS\')\n\n    inputIDs      <- names(savedInputs) \n    inputvalues   <- unlist(savedInputs) \n    for (i in 1:length(savedInputs)) { \n      session$sendInputMessage(inputIDs[i],  list(value=inputvalues[[i]]) )\n    }\n  })\n\n  observeEvent(input$save_inputs,{ \n    saveRDS( reactiveValuesToList(input) , file = \'inputs.RDS\')\n  })  \n})\n\nshinyApp(ui=ui,server=server)\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我不想将文件保存为 .RDS 对象,而是希望将文件保存为 .csv 文件,这样不精通 R 的用户实际上可以读取该文件,并在以后根据需要进行更新。因此,我希望将输入保存为以下格式的 .csv 文件(带有示例值):

\n\n
n, 5\nlower,10\nupper, 29\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果在 R 中更容易操作,也可以将其格式化为

\n\n
n, lower, upper\n5, 10, 29\n
Run Code Online (Sandbox Code Playgroud)\n\n

最后,在加载文件时,应用程序最好要求用户指向文件位置,然后在保存应用程序的输入时询问用户将文件保存到哪个位置。

\n\n

我如何编辑我的代码来实现这一目标?

\n

eas*_*611 9

library(shiny)  

ui <- shinyUI(fluidPage(

  textInput(inputId = 'inputsLocation', label = 'Inputs Location', value = "~/Desktop/user_inputs.csv"),
  actionButton('load_inputs', 'Load inputs'),
  br(),
  br(),
  numericInput("n", "Number",min = 1, value = 5),
  numericInput("upper", "Upper",min = 0, max = 100, value = 15),
  numericInput("lower", "Lower",min = 0, max = 100, value = 5),
  actionButton('save_inputs', 'Save inputs')

)) 

server <-  shinyServer(function(input, output,session) {

  observeEvent(input$load_inputs, {
    # Load inputs
    uploaded_inputs <- read.csv(input$inputsLocation)
    # Update each input
    for(i in 1:nrow(uploaded_inputs)){
      updateNumericInput(session,
                         inputId = uploaded_inputs$inputId[i],
                         value = uploaded_inputs$value[i])
    }
  })

  observeEvent(input$save_inputs, {
    # Define inputs to save
    inputs_to_save <- c('n', 'upper', 'lower')
    # Declare inputs
    inputs <- NULL
    # Append all inputs before saving to folder
    for(input.i in inputs_to_save){
      inputs <- append(inputs, input[[input.i]])
    }
    # Inputs data.frame
    inputs_data_frame <- data.frame(inputId = inputs_to_save, value = inputs)
    # Save Inputs
    write.csv(inputs_data_frame, file = input$inputsLocation, row.names = FALSE)
  }) 

})

shinyApp(ui=ui,server=server)
Run Code Online (Sandbox Code Playgroud)

数据“user_inputs.csv”如下所示:

"inputId","value"
"n",5
"upper",7
"lower",4
Run Code Online (Sandbox Code Playgroud)

我添加了列名“inputId”和“value”,以便代码可以更加明确。

该代码是根据您的原始帖子进行更改的。要使用 .csv 文件,需要采用不同的方式加载/保存输入。我认为这种方法是一种简单的方法,相对容易遵循。

注意* 当手动输入文件路径输入时,用户需要在文件路径中使用双斜杠“\\”或反斜杠“/”(与 R 中相同)。