【问题标题】:Using read.xlsx in Shiny R App在 Shiny R App 中使用 read.xlsx
【发布时间】:2013-09-10 19:27:50
【问题描述】:

我正在尝试加载一个 Excel 文件并显示摘要。文件正在加载,没有任何错误,但没有显示任何内容。

这是我的代码

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"))    
)))

服务器.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)

})

【问题讨论】:

    标签: r excel spreadsheet summary shiny


    【解决方案1】:

    我尚未对此进行测试,但看起来您实际上并没有从 dataset() 返回任何内容。将函数改为:

    dataset = reactive({
    
      infile = input$file1  
    
      if (is.null(infile))
        return(NULL)
    
      read.xlsx(infile$datapath, 1)
    
    })
    

    当您执行infile_read = read.xlsx(infile$datapath, 1) 时,您正在将文件读入infile_read,但实际上并没有返回它。反应式工作看起来真的是任何 R 函数。尝试运行:

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

    您应该看到f() 没有返回任何内容。它所做的只是完成一项无处可去的任务。要真正返回'hello',你会这样做:

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

    或者只是:

    f <- function() 'hello'
    

    【讨论】:

    • 我也尝试了以下方法,但没有运气infile_read = read.xlsx(infile$datapath, 1) return(infile_read)
    • browser() 放在您在文件中读取的位置之后。文件肯定被读入了吗?
    • 我在命令行中包含了browser()它的显示浏览器>,然后就停在那里。没有错误,没有输出。我也试过browser(text = "Done!!"),但是“完成!!”没有打印。
    • 好的,好的,这就是应该发生的事情。现在四处逛逛,看看你发现了什么。例如,在浏览器提示符下,尝试输入infile$datapath 并查看是否获得了一个存在的文件的有效路径。尝试输入infile_read,看看你是否得到data.frame 对象。
    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 2016-12-09
    • 2012-09-19
    • 2015-08-21
    • 1970-01-01
    • 2016-09-20
    • 2020-11-23
    • 2021-04-16
    相关资源
    最近更新 更多