我试图以闪亮的方式存储和加载用户选择的设置,但无法使其正常工作。 dput
和dget()
函数似乎可以正常工作,但是用户界面没有更新。我想念什么?
library(shiny)
outputDir <- "responses"
saveData <- function(inData) {
# Create a unique file name
fileName <- sprintf("%s_%s.txt", as.integer(Sys.time()), digest::digest(inData))
# Write the file to the local system
dput(inData, file = paste0(outputDir, '/', fileName))
}
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
selectInput("columns", "Select columns to exclude From gather", multiple = T,
choices = c('choice1', 'choice2','choice3')),
actionButton('submit', "Save inputs"),
actionButton('load', "Load inputs")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output, session) {
AllInputs <- reactive({
reactiveValuesToList(input)
})
observeEvent(input$submit, {
saveData(AllInputs())
})
observeEvent(input$load,{
files <- list.files(outputDir, full.names = TRUE)
inData <- dget(file = files)
for (i in 1:length(inData)) {
session$sendInputMessage(names(inData)[i], list(inData[[names(inData)[i]]]))
}
})
# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
shinyApp(ui, server)