r - 闪亮的输入可以读回从.Rdata加载的值吗?
问题描述
请运行该应用程序并要求在相应的输入中添加一些内容。然后请保存对象。您会在工作目录中找到保存的 .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")
解决方案
您可以使用reactiveValues
存储您的对象input$***
并将其保存到.reactiveValues
RData
如果要加载文件,只需阅读它并将其命名为与变量名RData
相同的名称。reactiveValues
你可以看到这个闪亮的应用程序,它将人们聊天记录保存到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
我只在and上做一个例子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")
推荐阅读
- python - Python 中的天文板块求解(Astropy)
- ios - iOS 上的 Firebase 动态链接有时会失败
- tensorflow - 如何在 Tensorflow Estimator 的 input_fn 中执行数据扩充
- angularjs - 无法读取量角器中未定义的属性“ARROW_DOWN”
- html - 多个倾斜矩形
- azure - 如何在 azure bot 框架中添加本地图像。我正在使用节点 js
- c# - 处理“任务被取消”异常
- android - 从 RecyclerView 中删除的最后一项但在 Layout 上仍然可见
- google-bigquery - BigQuery 中的访问被拒绝错误
- c# - 如何在 Linq 中用计数填充 NotMapped 属性?