闪亮的输入能否读回从 .Rdata 加载的值?

Can the shiny input read back the values loaded from .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 变量名相同的名称。

你可以看到这个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$nameinput$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")