如何解决R/R Shiny:使用下载处理程序下载数据表,出现错误 - 无法将 <character> 转换为 <double>
我有两个数据表,我想在编辑它们的单元格后将它们下载到 xlsx 文件的两个不同工作表中。使用下面提到的方法我收到错误 -
警告:错误:分配的数据 cell$value
必须与现有数据兼容。
i 列 Trial ID:
发生错误。
x 无法转换为 .
[没有可用的堆栈跟踪]
相关服务器代码如下-
x<- reactive({
inFile <- input$file
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,paste(inFile$datapath,".xlsx",sep=""))
read_excel(paste(inFile$datapath,sep=""),sheet = 1)
})
y <- reactive({
inFile <- input$file
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,sheet = 2)
})
output$table1 <- renderDataTable({
x()
},filter="top",class = 'hover cell-border stripe',editable= TRUE,extensions= 'Buttons',options = list(dom = 'Bfrtip',pageLength =10,buttons = c('copy','csv','excel','pdf','print'),scrollX=TRUE),server=FALSE)
output$table2 <- renderDataTable({
y()
},server=FALSE)
observeEvent(input[["table1_cell_edit"]],{
cell <- input[["table1_cell_edit"]]
newdf <- x()
newdf[cell$row,cell$col] <- cell$value
x(newdf)
})
observeEvent(input[["table2_cell_edit"]],{
cell <- input[["table2_cell_edit"]]
newdf <- y()
newdf[cell$row,cell$col] <- cell$value
y(newdf)
})
output$dl <- downloadHandler(
filename = "test.xlsx",content = function(file) {
write.xlsx2(x(),file,sheetName = "Sheet1")
write.xlsx2(y(),sheetName = "Sheet2",append = TRUE)
}
)
谁能告诉我我哪里出错了?
解决方法
对于 x 和 y,也许您应该使用 reactiveValues
对象,而不是 reactive
对象。试试下面的例子
library(shiny)
library(DT)
library(readxl)
ui <- fluidPage(
fileInput("file","Import File",accept = ".xlsx"),DTOutput("t1")
)
server <- function(input,output,session) {
x <- reactiveValues()
observe({
xdf <- reactive({
req(input$file)
inFile <- input$file
if(is.null(inFile)) return(NULL)
file.rename(inFile$datapath,paste0(inFile$datapath,".xlsx"))
read_excel(paste0(inFile$datapath,".xlsx"),sheet = 1)
})
x$df <- xdf()
})
output$t1 <- renderDT({x$df},filter="top",class = 'hover cell-border stripe',selection = 'none',editable= list(target = 'cell'),extensions= 'Buttons',options = list(dom = 'Bfrtip',pageLength =10,buttons = c('copy','csv','excel','pdf','print'),scrollX=TRUE),server=FALSE)
observeEvent(input[["t1_cell_edit"]],{
cell <- input[["t1_cell_edit"]]
str(cell)
x$df <<- editData(x$df,cell)
})
}
shinyApp(ui,server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。