如何使用DT和Shiny从上传的文件中编辑表格? [英] How to edit a table using DT and Shiny from an uploaded file?
问题描述
我依靠此处找到的代码块进行创建一个闪亮的应用程序来上传表格,编辑表格,然后下载表格。我已经设法编辑了已经加载到内存(iris)中的表,但是如何编辑要在Shiny中上载的表?。
I am relying on the code chunk found here to create a Shiny app to upload a table, edit the table, then download the table. I have managed to edit a table that is already loaded in memory (iris) but how do I edit a table that is to be uploaded in Shiny?.
我已经尝试了上面链接中的代码,并验证了该代码是否有效。我也尝试过下面的代码,这也可以。我无法实现的是将数据框 x
转换为分配给上载文件的反应对象,并将所有引用编辑为 x
。
I have tried the code in the link above and verified that it works. I have also tried the code below and this works too. What I have not been able to achieve is to convert the data frame x
into a reactive object that is assigned to an uploaded file, and edit all the references to x
accordingly.
# This code works, but lacks a fileinput object
# and needs to be amended for a reactive dataframe...
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
# ~~ add fileInput("file1", "Choose file") here ~~
downloadButton("download")
),
fluidRow(
DT::dataTableOutput('x1')
)
),
server = function(input, output, session) {
# Do I make x reactive?
x = iris
x$Date = Sys.time() + seq_len(nrow(x))
output$x1 = DT::renderDataTable(x, selection = 'none', rownames = FALSE, edit = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col + 1
v = info$value
x[i, j] <<- DT:::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
})
output$download <- downloadHandler("example.csv",
content = function(file){
write.csv(x, file)
},
contentType = "text/csv")
}
)
以前的尝试会引发错误,主要是
Previous attempts at this have thrown errors, mostly to do with operations not being allowed without an active reactive context.
下面的代码显示了我想要实现的目标,但抛出错误:
argument expr丢失,没有默认值
The code below shows what I want to achieve, but throws an error: "argument "expr" is missing, with no default"
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
fileInput("upload", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
downloadButton("download")
),
fluidRow(
DT::dataTableOutput('x1')
)
),
server = function(input, output, session) {
#x = iris
# In this edited example x is now a reactive expression, dependent on input$upload
x <- eventReactive({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
req(input$upload)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
tryCatch(
{
x <- read.csv(input$upload$datapath,
header = TRUE,
sep = ",",
stringsAsFactors = TRUE,
row.names = NULL)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
})
#x$Date = Sys.time() + seq_len(nrow(x))
output$x1 = DT::renderDataTable(x(), selection = 'none', rownames = FALSE, edit = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col + 1
v = info$value
x()[[i, j]] <<- DT:::coerceValue(v, x()[[i, j]])
newdf <- x()
replaceData(proxy, newdf, resetPaging = FALSE, rownames = FALSE)
})
output$download <- downloadHandler("example.csv",
content = function(file){
write.csv(x(), file)
},
contentType = "text/csv")
}
)
推荐答案
感谢Stephane,以及我认为这个相关问题我有一个答案。
With thanks to Stephane, and inspiration from this related question, I think I have an answer.
关键是使用reactValues作为DT ::: coerceValue的解决方法,而不喜欢反应式表达式。我提供了verbatimTextOutput来说明您在编辑数据表后对表所做的存储更改。下载按钮也允许您下载已编辑的表格。
Key is to use reactiveValues as a workaround to DT:::coerceValue not liking reactive expressions. I've included a verbatimTextOutput to illustrate the stored changes to the table after you edit the data table. The download button allows you to download the edited table too.
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
fileInput("upload", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
downloadButton("download")
),
fluidRow(
DT::dataTableOutput('x1'),
verbatimTextOutput("print")
)
),
server = function(input, output, session) {
# In this edited example x is now a reactive expression, dependent on input$upload
# Key to the solution is the use of reactiveValues, stored as vals
vals <- reactiveValues(x = NULL)
observe({
# input$upload will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
req(input$upload)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
tryCatch(
{
x <- read.csv(input$upload$datapath,
header = TRUE,
sep = ",",
stringsAsFactors = TRUE,
row.names = NULL)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
# Reactive values updated from x
vals$x <- x
})
output$print <- renderPrint({
vals$x
})
output$x1 = DT::renderDataTable(vals$x, selection = 'none', rownames = FALSE, edit = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col + 1
v = info$value
# Below is the crucial spot where the reactive value is used where a reactive expression cannot be used
vals$x[i, j] <<- DT:::coerceValue(v, vals$x[i, j])
replaceData(proxy, vals$x, resetPaging = FALSE, rownames = FALSE)
})
output$download <- downloadHandler("example.csv",
content = function(file){
write.csv(vals$x, file, row.names = F)
},
contentType = "text/csv")
}
)
这篇关于如何使用DT和Shiny从上传的文件中编辑表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!