我是Shiny的新手,我尝试从应用中的observeEvent()实例生成下载按钮(对操作按钮作出反应),然后我得到"错误:对象类型'关闭'不是子集表格"在"下载"输出
我检查了类似问题的几个现有答案,以避免重复答案,但我无法在我的应用程序中找到错误,没有对象名称与内置函数冲突。也许它可能是由反应性条款引起的。
这是代码的结构,我尽可能地减少了它,使其变得通用,并将变量和对象名称翻译成英语以便更好地理解,因为它是西班牙语。非常感谢你的帮助。
library(shiny)
ui = fluidPage(
mainPanel(
selectInput(inputId = "reportOption",
label = "Select report",
choices = c("A","B","C"),
selected = "A",
multiple = FALSE
),
fileInput(inputId = "lastMonth",
label = "Select last report",
multiple = FALSE,
accept = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
buttonLabel = "Browse",
placeholder = "..."
),
fileInput(inputId = "currentMonth",
label = "Select current file",
multiple = FALSE,
accept = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
buttonLabel = "Browse",
placeholder = "..."
),
actionButton(inputId = "execute",
label = "Generate report"
),
uiOutput("download")
)
)
server = function(input, output) {
require("openxlsx")
require("tibble") # Agregar columnas intermedias a un data frame
observeEvent(input$execute,
{fileLastMonth = input$lastMonth
last.report = fileLastMonth$datapath
fileCurrentMonth = input$currentMonth
current.report = fileCurrentMonth$datapath
# Nombre del nuevo reporte
# recicle old file in order to preserve formatting attributes
newFile = fileLastMonth$datapath
# some code for calculations
wb = loadWorkbook(newFile)
# some code to write data in the workbook
saveWorkbook(wb, file = newFile, overwrite = TRUE)
output$download <- renderUI({
downloadHandler(
filename = function() {
paste(switch(input$reportOption,
"A"="A-File",
"B"="B-File",
"C"="C-File"),"xlsx",sep = ".")
},
content = function(file) {
file.copy(newFile, file)
}
)
})
}
)
}
shinyApp(ui = ui, server = server)