【问题标题】:Uploading excel files in RShiny在 R Shiny 中上传 excel 文件
【发布时间】:2020-07-29 07:11:36
【问题描述】:

我正在尝试使用闪亮的库在 R 中上传包含多张工作表的 excel 文件。另外,我必须上传多个excel文件。 谁能告诉我如何解决这个问题

【问题讨论】:

  • 你有没有尝试过?
  • @RemkoDuursma 我尝试使用 read_excel,但在表单方面我无法加载
  • 你是说上传到闪亮的服务器吗?

标签: r excel user-interface server shiny


【解决方案1】:

这是一个简单的 Shiny 实用程序,用于打开 Excel 工作簿并从下拉列表中加载单个工作表。然后显示表格。希望这可以帮助您入门。

# Utility to read any Excel workbook and display a worksheet

library("shiny")
library("gdata")
library("tableHTML")

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Utility to read and display an Excel spreadsheet"),
  sidebarLayout(

    sidebarPanel (label="Data Source",
                  # Spreadsheet name and sheet name 
                  fileInput("spreadsheetName", "Spreadsheet Name",accept = c(".xlsx")),
                  selectInput(inputId = "worksheet", label="Worksheet Name",choices=''),
                  actionButton(inputId = "getData",label="Get Data")),

    mainPanel (
     tableHTML_output("table1Data")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  # Read the data from the spreadsheet
  observe({
    # Get the annual data
    # Check if file is there... if not, exit and wait for it again
    inFile <- input$spreadsheetName
    if (is.null(inFile)) {
      return (NULL)
    }
    else {
      # Set up the selection for the worksheet names within the selected file
      filePath <<- inFile$datapath
      selectionWorksheet <- sort(unique(sheetNames (inFile$datapath)))
      updateSelectInput(session, "worksheet", choices = selectionWorksheet)
    }
  })

  # Get data
  observeEvent(input$getData, {

    # Get the data from the spreadsheet worksheets
    worksheetData <- read.xls (filePath, sheet=input$worksheet, header=TRUE, stringsAsFactors = FALSE)

    # Print the Annual table
    output$table1Data <- renderTable ({
      worksheetData
    # End of main program
    })
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多