请运行该应用,然后要求在相应的输入中添加内容。然后请保存对象。您会在工作目录中找到一个.Rdata文件。这是我无法解决的问题。
在下面的应用程序中,闪亮的输入(例如,input $ name,input $ age,input $ location等)可以读取保存在.Rdata中的值吗?
我可以将输入保存在工作目录中的.Rdata文件中。但是,当我重新加载文件时,有什么方法可以将输入框替换为.Rdata文件中存储的值,否则保存它们就没有意义了吗? 这是一个桌面应用程序,我们将在本地运行。因此,保存每个点的用户输入很重要。但是,当我们加载先前已选择输入的.Rdata文件时,面临的挑战是,我们无法用这些值替换闪亮的输入。因此,我必须再次从闪亮的输入中进行选择。因此,保存的文件没有用。
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")
答案 0 :(得分:1)
您可以使用reactiveValues
存储input$***
并将reactiveValues
对象保存到RData
。
如果要加载RData
文件,只需对其进行读取,然后将其命名为与reactiveValues
变量名相同。
您可以看到此shiny app,它将聊天记录保存到RDS
文件中(类似于RData
文件)。
这就是server.R
中的工作方式:
vars <- reactiveValues(chat=NULL, users=NULL)
# Restore the chat log from the last session.
if (file.exists("chat.Rds")){
vars$chat <- readRDS("chat.Rds")
} else {
vars$chat <- "Welcome to Shiny Chat!"
}
我仅在input$name
和input$age
上举例。
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()
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)
isolate({
vals$name=input$name
vals$age=input$age
})
observeEvent(c(vals$name,vals$age),{
updateTextInput(session,"name",label="Type your name",value=vals$name)
updateTextInput(session,"age",label="Type your age",value=vals$name)
})
observeEvent(input$save_objs, {
# Run whenever save_objs button is pressed
vals$username<-input$name
vals$userage<-input$age
vals$usergender<-input$gender
vals$userheight<-input$height
vals$userlocation<-input$location
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$age <- some$input_copy$age
# vals$name <- input$name
print("** done loading **")
})
}
shinyApp(ui, server, enableBookmarking = "server")