【问题标题】:R Shiny Save to ServerR Shiny 保存到服务器
【发布时间】:2016-03-26 02:30:21
【问题描述】:

我正在构建一个 R Shiny 应用程序,作为我的团队构建的仿真模型的 GUI。用户定义参数,点击运行,模型生成一堆图表作为输出。我的问题是,每次用户打开应用程序时,他们都必须再次输入参数。我希望他们能够保存他们的参数并在他们返回应用程序时再次显示它们。

我最初的方法是让用户使用 downloadHandler 将 csv 中的参数下载到他们的本地计算机,然后在他们的下一个会话中上传它们。这是行不通的,因为格式混乱或用户更改文件的风险太大,然后当他们再次上传时我会出错。

我认为最有意义的是将参数保存在服务器上的文件中(我更喜欢 .Rdata 文件,这样我可以将参数保存在列表中),并使用 selectInput 小部件来允许用户调用他们想要的参数文件。我不知道如何从 Shiny 应用程序中保存到服务器,或者如何让 downloadHandler 执行此操作。

编辑 例如,当我这样做时: 用户界面:

downloadLink("saveParams",label="Save Your Model")

服务器:

 output$saveParams <- downloadHandler(
    filename <- function(){
      paste0(input$nameModel,".RData")
    },

    content = function(file) {
      inputparams<-inputparams()
      save(inputparams, file = file)
    }
  )

它让用户可以选择保存文件的位置,这是我想避免的。我希望它自动将其放到服务器上。我尝试使用 actionButton 来触发使用 save 但无法运行的反应。 有什么建议吗?

【问题讨论】:

  • 这应该相当简单,只要你的闪亮应用程序有适当的权限写入服务器上的文件夹。可能是触发R save() 方法的按钮,并使用downloadLink 获取文件下载链接
  • 我以为就这么简单,但 downloadHandler 只给用户一个下载位置的选项;我希望它自动下载到服务器上,而不是让用户选择将其保存到其他地方。
  • 其实只要用selectInput让用户选择服务器上保存的文件之一即可。
  • 如果您只有 1 个用户,请将所需参数设为默认参数。如果您有超过 1 个具有不同参数偏好的用户,您将不得不保留和保存用户特定的文件,这对于 Shiny 的免费开源版本是不可能的。
  • 这个shiny extension 可能会满足您的需求。通过将其输入值编码到 URL 中来保存闪亮应用程序的状态。在这里你可以找到demo

标签: r shiny


【解决方案1】:

这是一个工作示例,使用textInputactionButton 保存,selectInput 加载文件。请注意,/home/user 是您的闪亮应用程序拥有写入权限的文件夹。您可能需要更复杂的验证来确保用户输入有效的文件名。

如果您的 Shiny 应用程序有多个用户,您还需要找到一种方法来确保一个用户不会覆盖另一个用户保存的文件(例如,以用户名作为前缀,以当前时间作为后缀等) ,但这超出了本问题的范围。

ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      textInput("save_file", "Save to file:", value="sample.RData"),
      actionButton("save", "Save input value to file"),
      uiOutput("load")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

服务器.R

library(shiny)

shinyServer(function(input, output, session) {
  # render a selectInput with all RData files in the specified folder
  output$load <- renderUI({
    choices <- list.files("/home/user", pattern="*.RData")
    selectInput("input_file", "Select input file", choices)
  })
  # Save input$bins when click the button
  observeEvent(input$save, {
    validate(
      need(input$save_file != "", message="Please enter a valid filename")
    )
    bins <- input$bins
    save(bins, file=paste0("/home/user/", input$save_file))
    choices <- list.files("/home/user", pattern="*.RData")
    updateSelectInput(session, "input_file", choices=choices)
  })
  # Load an RData file and update input
  observeEvent(input$input_file, {
    load(paste0("/home/user/",input$input_file))
    updateSliderInput(session, "bins", value=bins)
  })

  output$distPlot <- renderPlot({

    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')

  })

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2018-10-19
    • 2011-03-18
    • 2017-11-16
    • 1970-01-01
    • 2018-02-02
    • 2020-05-01
    相关资源
    最近更新 更多