首页 > 解决方案 > 闪亮的输入可以读回从.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")

标签: rshinyshinyjs

解决方案


您可以使用reactiveValues存储您的对象input$***并将其保存到.reactiveValuesRData

如果要加载文件,只需阅读它并将其命名为与变量名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")

推荐阅读