【问题标题】:Reading specific sheet in read_excel in Shiny app在 Shiny 应用程序中读取 read_excel 中的特定工作表
【发布时间】:2018-08-08 12:03:00
【问题描述】:

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

用户界面

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

服务器

inFile1 <- input$file1
sheetname1 <- input$file1sheet
df1 <- read_excel(inFile1$datapath,sheet = sheetname1)

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

谢谢!

斯蒂芬

【问题讨论】:

  • 你应该提供一个完整的可重现的例子。

标签: shiny


【解决方案1】:

我已尝试了解您要查找的内容,但无法重现您的错误。但也许你可以看看下面的代码,看看你的代码在哪里失败。以下应用程序允许用户选择一个 .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)

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 2022-11-05
    • 2022-01-13
    • 1970-01-01
    • 2017-04-28
    • 2018-06-07
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多