This is a cross post from stack overflow.
I have an example app allowing users to do something in the shiny app and then bookmark the state. I understand that the state files are stored in the shiny_bookmarks folder. Typically, I would copy the bookmarked server URL and restore the app. But I'm wondering if it would be possible to upload the rds file and restore the state of the bookmarked app.
The shiny_bookmarks folder creates folders with .rds files. And my goal is to restore the state of the app by loading the .rds files.
Ideally, I would like to bundle the rds and other files into a single rda file and then upload the rda file using fileInput
. I think rda files would work better as they can save multiple objects.
For the example app below, I have the rda fileInput as a placeholder. I've been trying to restore the app using the files saved in the shiny_bookmarks folder but I'm not sure how to go about that as I couldn't find much documentation so far.
Example app:
library(shiny)
ui <- function(request){fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("data", "Choose CSV File", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
fileInput("file1", "Choose RDA File", accept = ".rda"),
bookmarkButton()
),
mainPanel(
tableOutput("data_head"),
tableOutput("contents"),
selectInput("select", "Select Variable", choices = NULL, selected = NULL),
plotOutput("boxplot")
)
)
)
}
server <- function(input, output) {
dataset <- reactive({
file <- input$data
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
my_data <- read.csv(file$datapath, header = input$header)
my_data
})
output$data_head <- renderTable({
head(dataset())
})
output$boxplot <- renderPlot({
req(dataset())
req(input$select)
boxplot(dataset()[[input$select]], horizontal = TRUE)
})
observeEvent(input$data, {
req(dataset())
updateSelectInput(session = getDefaultReactiveDomain(), "select", label = "Select Variable", choices = c("", names(dataset())))
})
output$out <- renderText({
if (input$caps)
toupper(input$txt)
else
input$txt
})
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "rds", "Please upload a RDA file"))
readRDS(file$datapath)
})
}
shinyApp(ui, server, enableBookmarking = "server")
Example csv data from base R:
write.csv("attitude", "attitude.csv")
I found a potential workaround here in this post, but they don't use bookmarks. Maybe their solution may be easier, but I'm struggling to integrate that with my example app.