Please run the app and would request to put something in the respective inputs. Then please save the object. You would find a .Rdata file saved in your working directory. Here is my problem which I am unable to figure out.
In the below application can shiny input (e.g. input$name, input$age, input$location etc) read the values saved in .Rdata?
I can save the inputs in a .Rdata file in my working directory. However when I load the file back is there any way I can replace the input boxes with the values stored in .Rdata file, otherwise there is no point in saving them right? This is a desktop app which we would run locally. So it is important to save the user inputs at each point. However the challenge when we load the .Rdata file which has previously selected inputs, we are unable to replace the shiny inputs with those values. Hence I have to make those selections again from shiny input. Thus the saved file is of no use.
library(shiny)
library(pryr)
ui <- function(request){
fluidPage(
titlePanel("Put title of the application"),
sidebarLayout(
sidebarPanel(
textInput("name", "Type your name", ""),
textInput("age", "Type your age", ""),
radioButtons("gender", "Select your gender", list("Male", "Female"), ""),
sliderInput("height", "Select your height", min = 5.0, max = 8.0, value = 5.2, step = 0.1),
selectInput("location", "Select your location", choices = c("","Gurgaon", "Bangalore", "Mumbai")),
actionButton("save_objs", "Save Objects"),
actionButton("load_objs", "Load Objects"),
bookmarkButton()
),
mainPanel(
textOutput("username"),
textOutput("userage"),
textOutput("usergender"),
textOutput("userheight"),
textOutput("userlocation"),
textOutput("userload")
)
)
)
}
server <- function(input, output, session) {
vals <- reactiveValues(name = NULL)
output$username <- renderText(input$name)
output$userage <- renderText(input$age)
output$usergender <- renderText(input$gender)
output$userheight <- renderText(input$height)
output$userlocation <- renderText(input$location)
observeEvent(input$save_objs, {
# Run whenever save_objs button is pressed
print("** saving objects! **")
## Print the objects being saved
print(rls())
# ## Put objects into current environment
for(obj in unlist(rls())) {
if(class(get(obj, pos = -1))[1] == "reactive"){
## execute the reactive objects and put them in to this
## environment i.e. into the environment of this function
assign(obj, value = eval(call(obj)))
} else {
## grab the global variables and put them into this
## environment
assign(obj, value = get(obj, pos = -1))
}
}
input_copy <- list()
for(nm in names(input)){
# assign(paste0("input_copy$", nm), value <- input[[nm]])
input_copy[[nm]] <- input[[nm]]
}
## save objects in current environment
save(list = ls(), file = "shiny_env.Rdata", envir = environment())
print("** done saving **")
})
observeEvent(input$load_objs, {
# Run whenever load_objs button is pressed
## Load the objects
f.loaddata <- function()
{
myenv <- new.env()
load(file = file.choose(), envir = myenv)
myenv
}
print("** About to load objects! **")
# ## Put objects into current environment
some <- f.loaddata()
#print(some$input_copy$name)
vals$name <- some$input_copy$name
vals$name <- input$name
print("** done loading **")
})
}
shinyApp(ui, server, enableBookmarking = "server")
RData
file will be in the global environment, and shinyinput
is not global variable. – Jim Chen