R Shiny:保存并加载进度

时间:2018-01-09 11:17:47

标签: r shiny rhandsontable

我正在开发一个使用rhandsontable的Shiny App,我想为用户提供保存和加载进度的选项。我的代码的最小示例如下:

library(shinydashboard)
library(shiny)
library(data.table)
library(rhandsontable)
library(markdown)

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Data", tabName = "data", icon = icon("file")),
    menuItem("Control", tabName = "control", icon = icon("list-alt"))
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "data", 
            fluidRow(
              box(title = h3("Input data manually or by importing a .csv file:"),
                  #fileInput("file1", "Choose CSV File:", width = '30%',
                  #    multiple = TRUE,
                  #    accept = c("text/csv",
                  #               "text/comma-separated-values,text/plain",
                  #              ".csv")),
                  width = 12, height = 800, rHandsontableOutput("hot"))
            )
    ),
    tabItem(tabName = "control",
            fluidRow(
              actionButton("save", "Save"), actionButton("load", "Load"),
              box(title = h2("1. General Information"), width = '100%',
                  radioButtons("Type", 
                               h4("Type:"), 
                               choices = list("1" = "1", "2" = "2")),
                  radioButtons("DataExtraction", 
                               h4("Extract information:"), 
                               choices = list("Yes" = "Yes", "No" = "No"), selected = "No")
              )
            )
    )
  )
)

ui <- dashboardPage(
  dashboardHeader(title = "Shiny"),
  sidebar,
  body
)

server <- function(input, output, session) {

  observeEvent(input$load,{   
    values <<- readRDS("C:/Documents/ws1.RData")
    if (exists("values")) {
      lapply(names(values),
             function(x) session$sendInputMessage(x, list(value = values[[x]]))
      )
    }
  })

  observeEvent(input$save,{ 
    values <<- lapply(reactiveValuesToList(input), unclass)
    saveRDS( values , file = "C:/Documents/ws1.RData")
  })

  filedata <- reactive({
    inFile <- input$file1

    if (is.null(inFile)){
      data.table(Number1 = numeric(20),
                 Number2 = numeric(20),
                 Date1 = seq(from = Sys.Date(), by = "days", length.out = 20),
                 Date2 = seq(from = Sys.Date(), by = "days", length.out = 20))
    } else{
      fread(input$file1$datapath)
    }
  })

  output$hot = renderRHandsontable({
    rhandsontable(filedata()) %>%
      hot_cols(columnSorting = TRUE) %>%
      hot_table(highlightCol = TRUE, highlightRow = TRUE)
  })

}

shinyApp(ui, server)

我遇到两个问题:

  1. 当我包含fileInput(“file1”,...)时,输入不会更新 一旦我点击加载动作按钮,
  2. Rhandsontable未更新。但是,当我查看值$ hot $ data时,似乎数据正确存储在值中。
  3. 有没有人知道我做错了什么?

    谢谢!

0 个答案:

没有答案