如何使 rhandsontable 对数据上传和用户编辑做出反应?
How do I make a rhandsontable reactive on both data upload as well as user edits?
在我的应用程序中,我希望允许用户上传 CSV 文件并选择一组特定的列进行进一步处理。
为此,我从上传的文件中获取列名,并使用 rhandsontable 显示每个列名的复选框。我需要存储应处理哪些列的详细信息。
我的问题是我能够解决问题中的个别问题。如果进行了编辑,我知道如何在后端存储 rhandsontable(如 here 所述)。在下面的示例应用程序中,我知道如何在上传新数据时更新列名和复选框的显示。
但是,我无法同时解决这两个问题 - 即在进行用户编辑时将列列表存储在后端,同时在新数据出现时允许将其完全重置已上传。
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)
感谢 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)
在我的应用程序中,我希望允许用户上传 CSV 文件并选择一组特定的列进行进一步处理。
为此,我从上传的文件中获取列名,并使用 rhandsontable 显示每个列名的复选框。我需要存储应处理哪些列的详细信息。
我的问题是我能够解决问题中的个别问题。如果进行了编辑,我知道如何在后端存储 rhandsontable(如 here 所述)。在下面的示例应用程序中,我知道如何在上传新数据时更新列名和复选框的显示。
但是,我无法同时解决这两个问题 - 即在进行用户编辑时将列列表存储在后端,同时在新数据出现时允许将其完全重置已上传。
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)
感谢 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)