我有一个闪亮的应用程序,有很多输入文件要指定。每次我重新打开我的闪亮的应用程序时,我都需要再次指定所有它们。有没有办法让Shiny记住所选的文件?(不是通过在代码中写入默认值,而是通过单击保存按钮或类似的东西)。
发布于 2019-07-23 22:01:27
您可以创建一个按钮,用于在单击时保存所有输入的值,还可以创建另一个按钮,用于使用保存的值更新输入。下面是一个最小的例子:
library(shiny)
# Global variables
path_to_save <- "save_param.RData"
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
checkboxGroupInput(inputId = "color",
label = "Bins color",
choices = c("red", "blue", "green")),
actionButton(inputId = "save",
label = "Save parameters"),
tags$hr(),
actionButton(inputId = "apply_save",
label = "Load saved parameters")
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output, session) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = input$color, border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
observeEvent(input$save,{
params <- list(bins = input$bins, color = input$color)
save(params, file = path_to_save)
})
observeEvent(input$apply_save,{
load(path_to_save) # of course you need to add verifications about the existence of the file
updateSliderInput(session = session, inputId = "bins", min = 1, max = 50, value = params$bins)
updateCheckboxGroupInput(session = session, inputId = "color", label = "Bins color", choices = c("red", "blue", "green"),
selected = params$color)
})
}
shinyApp(ui, server)你可以升级这个想法,可以节省几笔钱,给这些钱命名,用selectInput选择你想要的那个,等等。
https://stackoverflow.com/questions/57163739
复制相似问题