r - R Shiny openxlsx: uploaded xlsx file - select a sheet from drop-down menu
问题描述
In my Shiny app, I can upload an xlsx file and select sheet by typing the sheet's name:
library(shiny)
library(openxlsx)
runApp(
list(
ui = fluidPage(
titlePanel("openxlsx - choose sheet"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))),
mainPanel(tableOutput('contents'),
textInput("tab1", "Type in sheet name:", "Sheet1")))),
server = function(input, output,session){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,paste(inFile$datapath, ".xlsx", sep=""))
read.xlsx(paste(inFile$datapath, ".xlsx", sep=""), sheet=input$tab1)
})}))
I would prefer to be able to use a drop-down menu to select a sheet. I know I can use openxl package to get the sheets names, but I am not sure how to implement that in Shiny. Any help will be appreciated.
解决方案
在用户界面中:
uiOutput("dropdownUI")
在服务器中:
Workbook <- eventReactive(input$file1, {
loadWorkbook(input$file1$datapath)
})
Sheets <- eventReactive(Workbook(), {
names(Workbook())
})
output$dropdownUI <- renderUI({
req(Sheets())
selectInput("sheet", "Choose a sheet", Sheets())
})
Dat <- eventReactive(input$sheet, {
read.xlsx(Workbook(), sheet = input$sheet)
})
output$contents <- renderTable({
req(Dat())
Dat()
})
推荐阅读
- reactjs - onlineUrs.map 不是 React Js 前端的函数
- wix - 从自定义操作设置属性?
- java - 检索现有 Mongo DB 集合时出现空指针异常
- java - Bindings.select() 依赖没有更新
- python - pytorch自定义损失函数最小化向量之间的角度
- c# - 无法在 Unity 中使用 UnityWebRequest 发布数据,它给出错误:HTTP/1.1 500 内部服务器错误
- python - 列表中元素的最大总和
- google-apps-script - 状态 2020 年 12 月 - “部署 ID 未与当前项目关联”
- memory-management - 使用 Fortran 90 将 txt 文件读入可分配数组时出现错误结果
- python - 尝试创建虚拟环境时 pipenv FileNotFoundError