我试图通过读取Excel文件来动态创建tabsetPanel。也许,当阅读一个excel时,它必须读取它拥有多少个选项卡,并创建包括不同工作表内容的不同选项卡。
UI.R
shinyUI(pageWithSidebar(
headerPanel("Generating an arbitrary number of tabs - assay 7bis"),
sidebarPanel(
uiOutput("Radio"),
radioButtons("fileType_Input", label = "Choose File type", choices = list(".csv/txt" = 1, ".xlsx" = 2), selected = 1, inline = TRUE),
fileInput('datafile', 'Choose file',accept=c('application/vnd.ms-excel','text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("out1")
),
mainPanel(
uiOutput("twotabs")
#div(style="display:inline-block; padding: 8px 12px",
# tabsetPanel(
# id = 'dataFiles',
# tabPanel("Lab1", htmlOutput("Lab1_html"), div(align = 'left', style = 'height: 600px; width:1300px; overflow-x:scroll; overflow-y: scroll', tableOutput('table1')), style = "width:100%"),
# tabPanel("Lab2", htmlOutput("Lab2_html"), div(align = 'left', style = 'height: 600px; width:1300px; overflow-x:scroll; overflow-y: scroll', tableOutput('table2')), style = "width:100%"),
# tabPanel("Lab3", htmlOutput("Lab3_html"), div(align = 'left', style = 'height: 600px; width:1300px; overflow-x:scroll; overflow-y: scroll', tableOutput('table3')), style = "width:100%")
# )
#)
)
))
SERVER.R
shinyServer(function(input, output, session) {
## List of datasets to read: for an excel we can have more than one sheet.
observeEvent(input$fileType_Input,{
listData <- list(NULL)
})
## Reading the file.
listData <- reactive({
auxList <- list(NULL)
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(auxList)
}
#
# Depending on the radioButton value, the file should be CSV or XLSX.
if ((infile$type == 'text/csv' || infile$type == 'application/vnd.ms-excel') & input$fileType_Input == "1") {
# CSV File.
auxList[[1]] <- read.csv(infile$datapath)
listSheetsNames <- list("CSV")
nSheets <- 1
#
} else if (infile$type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' & input$fileType_Input == "2") {
# XLSX file.
wbFile <- loadWorkbook(infile$datapath)
sheetsFile <- getSheets(wbFile)
listSheetsNames <- names(sheetsFile)
#
# Reading any of the sheets.
for (j in 1:length(sheetsFile)){
auxList[[j]] <- read.xlsx(infile$datapath, sheetIndex = j, sheetName = NULL, as.data.frame = TRUE, header = TRUE)
}
nSheets <- length(sheetsFile)
} else {
nSheets <- 0
listSheetsNames <- NULL
auxList <- NULL
}
#
list(nSheets = nSheets, strNames = listSheetsNames, lData = auxList)
})
##
## Tabs.
output$twotabs <- renderUI({
listAuxData <- listData()
if (!is.null(listAuxData$lData)) {
listTabs <- list(NULL)
for (i in 1:listAuxData$nSheets) {
listTabs[[i]] <- tabPanel(listAuxData$strNames[i],
tableOutput(paste0("table",i))
)
##
}
listTabs$id <- "dataFiles"
do.call(tabsetPanel, listTabs)
}
})
##
## Tables
observe({
listAuxData <- listData()
if (!is.null(listAuxData$lData)) {
for (k in 1:listAuxData$nSheets) {
# Fill the tabs.
nameTable <- paste0("table",k)
dd <- listAuxData$lData[[k]]
output[[nameTable]] <- renderTable({dd}, striped=TRUE, bordered = TRUE, align = "l", digits = 0)
}
updateTabsetPanel(session, "dataFiles")
}
})
##
})
我希望任何工作表中的内容都在动态创建的不同选项卡中。我能得到什么?我总是在任何创建的选项卡(创建良好)上获得excel文件中的最后一张纸。
当我使用静态tabsetPanel静态地执行此操作时,它将起作用。假装我知道我在excel中有多少张纸时,例如3,我放了一个数字...
dd <- listAuxData$lData[[1]]
...代替计数器变量,它可以正常工作,并且可以准确地显示我放置的图纸。但是,使用动态计数器时,请始终将最后一张纸的内容放在三个选项卡中。
几天来我一直在为此奋斗,但尚未找到解决方案。
谢谢。