如何对数据上传和用户编辑做出反应性的反应?

时间:2019-01-31 19:30:41

标签: r shiny rhandsontable

在我的应用程序中,我希望允许用户上传CSV文件并选择一组特定的列以进行进一步处理。

为此,我从上载的文件中获取列名,并使用rhandsontable针对每个列名显示一个复选框。我需要存储应处理哪些列的详细信息。

我的问题是我能够解决问题中的单个问题。我知道如果进行了编辑(如here所述),如何将rhandsontable存储在后端。在下面的示例应用程序中,如果上传了新数据,我知道如何更新列名称和复选框的显示。

但是,我无法同时解决这两个问题-即在进行用户编辑时将列的列表存储在后端,同时还允许在上载新数据时将其完全重置。

library(shiny)
library(rhandsontable)

dataTabUI <- function(id, i, od, os) {
  ns <- NS(id)
  tagList(i,
          column(6, tableOutput(od)),
          column(6, rHandsontableOutput(os)))
}

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

}

csvFileInput <- function(id, label = "CSV file") {
  ns <- NS(id)
  tagList(
    fileInput(ns("file"), label)
  )
}

csvFile <- function(input, output, session) {
  userFile <- reactive({
    validate(need(input$file, message = FALSE))
    input$file
  })
  dataframe <- reactive({
    df <- read.csv(
      userFile()$datapath,
      header = TRUE
    )
  })
  dataframe
}

ui <- shinyUI(
  navbarPage(
    "My Application",
    tabPanel(
      "File Upload",
      dataTabUI("tab1", csvFileInput("datafile", "Upload CSV"),
                "data", "vars")
    )
  )
)

server <- function(input, output, session) {
  dataframe <- callModule(csvFile, "datafile")
  output$data <- renderTable({
    head(dataframe(), n = 10L)
  })
  output$vars <- renderRHandsontable({
    df <- dataframe()
    cnames <- colnames(df)
    ctypes <- vapply(cnames, function(cname) {
      class(df[[cname]])
    }, character(1))
    datavars <- data.frame(cnames = cnames, ctypes = ctypes,
                           treat = rep(TRUE, length(cnames)),
                           stringsAsFactors = FALSE)
    rhandsontable(datavars)
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

感谢RStudio Community中的有用建议,我能够回答自己的问题。

library(shiny)
library(rhandsontable)

dataTabUI <- function(id, i, od, os) {
  ns <- NS(id)
  tagList(i,
          column(6, tableOutput(od)),
          column(6, rHandsontableOutput(os)))
}

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

}

csvFileInput <- function(id, label = "CSV file") {
  ns <- NS(id)
  tagList(
    fileInput(ns("file"), label)
  )
}

csvFile <- function(input, output, session) {
  userFile <- reactive({
    validate(need(input$file, message = FALSE))
    input$file
  })
  dataframe <- reactive({
    df <- read.csv(
      userFile()$datapath,
      header = TRUE
    )
  })
  dataframe
}

ui <- shinyUI(
  navbarPage(
    "My Application",
    tabPanel(
      "File Upload",
      dataTabUI("tab1", csvFileInput("datafile", "Upload CSV"),
                "data", "vars")
    )
  )
)

server <- function(input, output, session) {
  dataframe <- callModule(csvFile, "datafile")
  output$data <- renderTable({
    head(dataframe(), n = 10L)
  })

  dfvars <- reactive({
    df <- dataframe()
    cnames <- colnames(df)
    ivars <- input$vars
    if (length(ivars) > 0) {
      odf <- hot_to_r(ivars)
      ocnames <- odf$cnames
      if (!all(vapply(ocnames, function(x) {
        x %in% cnames
      }, logical(1)))) {
        ctypes <- vapply(cnames, function(cname) {
          class(df[[cname]])
        }, character(1))
        datavars <- data.frame(cnames = cnames, ctypes = ctypes,
                               treat = rep(TRUE, length(cnames)),
                               stringsAsFactors = FALSE)
      } else {
        datavars <- odf
      }
    } else {
      ctypes <- vapply(cnames, function(cname) {
        class(df[[cname]])
      }, character(1))
      datavars <- data.frame(cnames = cnames, ctypes = ctypes,
                             treat = rep(TRUE, length(cnames)),
                             stringsAsFactors = FALSE)
    }
    datavars
  })

  output$vars <- renderRHandsontable({
    rhandsontable(dfvars())
  })

  observe({
    print(dfvars())
  })
}

shinyApp(ui, server)