[英]dataTableProxy to data.frame error in R Shiny
I have created a demo app that allows the user to edit an existing df
and then download the updated table as a.csv.我创建了一个演示应用程序,允许用户编辑现有的
df
,然后将更新的表下载为 a.csv。 The app runs almost fine, as when I click the download
button, I get the following error:该应用程序几乎可以正常运行,因为当我单击
download
按钮时,出现以下错误:
Warning: Error in as.data.frame.default: cannot coerce class ‘"dataTableProxy"’ to a data.frame
[No stack trace available]
How can this be fixed?如何解决这个问题?
Code代码
# Double click in a table cell to edit its value and then download the updated table
library(shiny)
library(DT)
library(tidyverse)
# Define UI for application that edits the table
ui = fluidPage(
DTOutput('x1'),
# App title
titlePanel("Downloading Data"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Input: Choose dataset
selectInput("dataset", "Choose a dataset:",
choices = c("Demo Table")),
# Button
downloadButton("downloadData", "Download")
),
# Main panel for displaying outputs
mainPanel(
tableOutput("table")
)
))
# Define server logic required
server = function(input, output) {
x = iris
x$Date = Sys.time() + seq_len(nrow(x))
output$x1 = renderDT(x, selection = 'none', editable = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE) # important
})
# Downloadable table (df) as csv
output$downloadData = downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(proxy, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
Replace代替
write.csv(proxy, file, row.names = FALSE)
with和
write.csv(x, file, row.names = FALSE)
This is not a full answer.这不是一个完整的答案。 I know where the error occurs and I can fix it.
我知道错误发生在哪里,我可以修复它。 But I can not make the downloadhandler to download the datatable (instead an empty csv file is occuring).
但我不能让下载处理程序下载数据表(而是出现一个空的 csv 文件)。
This might be due to:这可能是由于:
input$dataset
is not defined in your code. input$dataset
未在您的代码中定义。 Here is a similar post handling this issue: Shiny App Upload and Download Data Dynamically Not Working Shiny App动态上传下载数据不工作
Replace data in R formattable datatable using replaceData function 使用 replaceData function 替换 R 格式表数据表中的数据
replaceData
requires a dataframe in the second argument , not a datatable . replaceData
在第二个参数中需要 dataframe,而不是数据表。
This is the reason you get the error.这就是您收到错误的原因。
When you have a datatable proxy
, the dataframe is in proxy$x$data
.当您有数据表
proxy
时, dataframe 位于proxy$x$data
中。
Using this code removes the error, but a blank.csv file is downloaded使用此代码可消除错误,但会下载空白.csv 文件
# Downloadable table (df) as csv
output$downloadData = downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(proxy$proxy, file, row.names = FALSE)
}
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.