R保存到服务器
我正在构建一个R闪亮的应用程序,作为我的团队构建的模拟模型的GUI。用户定义参数,单击run,模型生成一组图表和表格作为输出。我的问题是,每次用户打开应用程序时,他们都必须再次输入参数。我希望他们能够保存自己的参数,并在返回应用程序时再次显示 我最初的方法是让用户使用downloadHandler将csv中的参数下载到本地机器,然后在下一个会话中上载。这是不可行的,因为有太多的风险,格式将被弄乱,或者用户将对文件进行更改,然后我将有错误,当他们再次上传它 我认为最有意义的是将参数保存在服务器上的文件中(我更喜欢.Rdata文件,这样我可以将参数保存在列表中),并使用selectInput小部件允许用户调用他们想要的参数文件。我不知道如何从一个闪亮的应用程序中保存到服务器,也不知道如何让downloadHandler这样做 编辑 例如,当我这样做时: 用户界面: 服务器:R保存到服务器,r,shiny,R,Shiny,我正在构建一个R闪亮的应用程序,作为我的团队构建的模拟模型的GUI。用户定义参数,单击run,模型生成一组图表和表格作为输出。我的问题是,每次用户打开应用程序时,他们都必须再次输入参数。我希望他们能够保存自己的参数,并在返回应用程序时再次显示 我最初的方法是让用户使用downloadHandler将csv中的参数下载到本地机器,然后在下一个会话中上载。这是不可行的,因为有太多的风险,格式将被弄乱,或者用户将对文件进行更改,然后我将有错误,当他们再次上传它 我认为最有意义的是将参数保存在服务器上的
output$saveParams <- downloadHandler(
filename <- function(){
paste0(input$nameModel,".RData")
},
content = function(file) {
inputparams<-inputparams()
save(inputparams, file = file)
}
)
output$saveParams下面是一个工作示例,使用textInput
和action按钮
保存,使用selectInput
加载文件。请注意,/home/user
是您的应用程序具有写入权限的文件夹。您可能需要更复杂的验证来确保用户输入有效的文件名
如果您的Shining应用程序有多个用户,您还需要找到一种方法来确保一个用户不会覆盖另一个用户保存的文件(例如,前缀为用户名,后缀为当前时间等),但这超出了此问题的范围
用户界面
服务器.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')
})
})
库(闪亮)
shinyServer(功能(输入、输出、会话){
#使用指定文件夹中的所有RData文件呈现selectInput
输出$load只要你的闪亮应用程序有适当的权限写入服务器上的文件夹,这应该相当简单。可能是一个触发R save()的按钮方法,并使用downloadLink
获取文件下载链接。我原以为它会这么简单,但downloadHandler只为用户提供下载位置的选项;我希望它自动下载到服务器上,而不给用户任何将其保存到其他地方的选择。实际上,只需使用selectInput
来询问用户选择保存在服务器上的一个文件。如果只有一个用户,则将所需参数设置为默认参数。如果有多个用户具有不同的参数首选项,则必须保留并保存用户特定的文件,这在免费开源版本的Shining中是不可能的。这可能会影响您的外观正在查找。闪亮应用程序的状态通过将其输入值编码到URL来保存。在这里,您可以找到一个。
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")
)
)
))
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')
})
})