我依靠找到的here代码块来创建一个Shiny应用程序来上载表格,编辑表格,然后下载表格。我已经设法编辑了已加载到内存(iris)中的表,但是如何编辑要在Shiny中上载的表?。
我已经尝试了上面链接中的代码,并验证了它可以工作。我也尝试过下面的代码,这也可以。我无法实现的是将数据帧x
转换为分配给上载文件的反应对象,并相应地编辑对x
的所有引用。
# 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")
}
)
先前的尝试会引发错误,这主要与没有活动的响应上下文而不允许的操作有关。
下面的代码显示了我想要实现的目标,但抛出错误: “缺少参数“ expr”,没有默认值”
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")
}
)
答案 0 :(得分:0)
请参见?eventReactive
。
您应该这样做:
x <- eventReactive(input$upload, { # 'input$upload' was the "expr missing"
......
或
x <- reactive({
req(input$upload)
......
答案 1 :(得分:0)
多亏了斯蒂芬(Stephane)和this related question的启发,我想我有了答案。
关键是要使用reactactiveValues作为DT ::: coerceValue的解决方法,而不喜欢反应式表达式。我提供了一个verbatimTextOutput来说明在编辑数据表后对表的存储更改。下载按钮也允许您下载已编辑的表格。
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")
}
)