R/R闪亮:使用下载处理程序下载数据表,出现错误-可以将<;字符&>转换为<;Double>; [英] R/R Shiny : Downloading Data Tables using Download Handler, getting error - Can't convert <character> to <double>
本文介绍了R/R闪亮:使用下载处理程序下载数据表,出现错误-可以将<;字符&>转换为<;Double>;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cell$value
必须与现有数据兼容。
列Trial ID:
出现I错误。
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, ".xlsx", sep=""),sheet = 1)
})
y <- reactive({
inFile <- input$file
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""),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()
}, 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)
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(), file, 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)
这篇关于R/R闪亮:使用下载处理程序下载数据表,出现错误-可以将<;字符&>转换为<;Double>;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文