Using read.xlsx in Shiny R App

流过昼夜 提交于 2019-12-07 13:59:41

问题


I am trying to load an excel file and display the summary. The file is loading without any errors but not displaying anything.

Here is my code

ui.R

library(shiny)
shinyUI(pageWithSidebar(
     headerPanel("Analysis"),
     sidebarPanel(wellPanel(fileInput('file1', 'Choose XLSX File',
          accept=c('sheetName', 'header'), multiple=FALSE))),
mainPanel(
tabsetPanel(
  tabPanel("Tab1",h4("Summary"), htmlOutput("summary"))    
)))

server.R

      library(shiny)


shinyServer(function(input, output) {
 dataset = reactive({

    infile = input$file1  


    if (is.null(infile))
      return(NULL)

    infile_read = read.xlsx(infile$datapath, 1)
    return(infile_read)

  })

 output$summary <- renderPrint({
   summary = summary(dataset())
   return(summary)
 })

  outputOptions(output, "summary", suspendWhenHidden = FALSE)

})

回答1:


I haven't tested this, but it looks like you're not actually returning anything from dataset(). Change the function to:

dataset = reactive({

  infile = input$file1  

  if (is.null(infile))
    return(NULL)

  read.xlsx(infile$datapath, 1)

})

When you do infile_read = read.xlsx(infile$datapath, 1), you're reading the file into infile_read but then you're not actually returning it. Reactives work just look any R function really. Try running this:

f <- function() x <- 10
f()

You should see that f() doesn't return anything. All it's doing is making an assignment that goes nowhere. To actually return 'hello' you would do:

f <- function() {
  x <- 'hello'
  x
}

Or just:

f <- function() 'hello'


来源:https://stackoverflow.com/questions/18727284/using-read-xlsx-in-shiny-r-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!