在 Shiny 应用程序中读取 read_excel 中的特定工作表

Ste*_*yet 2 shiny

我正在创建一个 Shiny 应用程序,它接受一个 Excel 文件并自动操作数据。我希望用户输入他们想要查看的特定 Excel 工作的名称。为此,我无法找到在 UI 中使用 textInput 并在服务器中使用 input$filesheet 的方法。我的代码可能有助于更了解这个问题:

用户界面

fileInput('file1', 'Insert File',
            accept = c(".xlsx"),
textInput('file1sheet','Name of Sheet (Case-Sensitive)')
Run Code Online (Sandbox Code Playgroud)

服务器

inFile1 <- input$file1
sheetname1 <- input$file1sheet
df1 <- read_excel(inFile1$datapath,sheet = sheetname1)
Run Code Online (Sandbox Code Playgroud)

问题是 sheetname1 似乎不起作用,因为 read_excel 无法将其识别为正确的表达式。我尝试了一些东西,包括 ShQuote 和 as.character。如果有人对此有解决方案,那就太棒了!

谢谢!

斯特凡

klu*_*luu 5

我试图了解您在寻找什么,但我无法重现您的错误。但也许你可以看看下面的代码,看看你的代码失败的地方。下面的应用程序让用户选择一个 .xlsx 文件,然后在显示相应的表格之前检索工作表名称。

library(shiny)
library(readxl)

ui <- fluidPage(
  fileInput('file1', 'Insert File', accept = c(".xlsx")),
  textInput('file1sheet','Name of Sheet (Case-Sensitive)'),
  tableOutput("value")
)

server <- function(input, output) {

  sheets_name <- reactive({
    if (!is.null(input$file1)) {
      return(excel_sheets(path = input$file1$datapath))  
    } else {
      return(NULL)
    }
  })

  output$value <- renderTable({
    if (!is.null(input$file1) && 
        (input$file1sheet %in% sheets_name())) {
      return(read_excel(input$file1$datapath, 
                        sheet = input$file1sheet))
    } else {
      return(NULL)
    }
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)