我的Shiny应用程序有几个输入,用于定义生成的绘图的几个参数.用户很可能会花费几分钟时间浏览所有可能的选项,直到他对输出感到满意为止.显然,可以以不同的格式导出绘图,但是用户可能希望稍后使用不同的数据重新创建相同的绘图,或者可能只更改一个小细节.
因此,我需要为用户提供一种方法来导出他的所有设置并保留该文件供以后使用.我开发了一种方法,但效果不佳.我正在使用reactiveValuesToList获取所有输入元素的名称并将其另存为具有该格式的简单文本文件inputname=inputvalue.这是downloadHandler上server.R:
output$bt_export <- downloadHandler(
filename = function() {
"export.txt"
},
content = function(file) {
inputsList <- names(reactiveValuesToList(input))
exportVars <- paste0(inputsList, "=", sapply(inputsList, function(inpt) input[[inpt]]))
write(exportVars, file)
})
Run Code Online (Sandbox Code Playgroud)
这工作正常,但加载不是很顺利.由于我没有(并且无法弄清楚如何)保存输入类型,我必须盲目更新这些值.我是这样做的:
importFile <- reactive({
inFile <- input$fileImport
if (is.null(inFile))
return(NULL)
lines <- readLines(inFile$datapath)
out <- lapply(lines, function(l) unlist(strsplit(l, "=")))
return(out)
})
observe({
imp <- importFile()
for (inpt in imp) {
if (substr(inpt[2], 0, 1) == "#") {
shinyjs::updateColourInput(session, inputId = inpt[1], value = inpt[2]) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个在本地使用的闪亮应用程序。我正在尝试开发一个系统,使用户能够恢复以前的会话。
为此,我从这个入口获取了代码:Saving state of Shiny app to be returned later,它确实有效,但是我希望能够在不同的会话中恢复输入,因此我添加了一个 fileInput (Restore Session )和代码中的 downloadButton (保存会话),但不幸的是我无法使其工作。
我的代码如下:
library(shiny)
ui <- fluidPage(
textInput("control_label",
"This controls some of the labels:",
"LABEL TEXT"),
numericInput("inNumber", "Number input:", min = 1, max = 20, value = 5, step = 0.5),
radioButtons("inRadio", "Radio buttons:",
c("label 1" = "option1",
"label 2" = "option2",
"label 3" = "option3")),
fileInput("load_inputs", "Restore Session", multiple = FALSE),
downloadButton("save_inputs", 'Save Session')
)
server <- function(input, output,session) {
# SAVE SESSION …Run Code Online (Sandbox Code Playgroud)