I\'m building an R Shiny app that acts as a GUI for a simulation model my team has built. The user defines parameters, clicks run, and the model produces a bunch of charts a
Here is a working example, using a textInput
and actionButton
to save, and a selectInput
to load the file. Note that /home/user
is a folder that your shiny app has write permission to. You might need more sophisticated validation to ensure that user enters a valid file name.
If you have multiple users for your Shiny app, you'll also need to find a way to ensure that one user will not overwrite the other's saved file (for example, prefix with user's name, suffix with current time, etc), but that is outside the scope of this question.
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")
)
)
))
server.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')
})
})