2013-09-10 74 views
2

我想加載一個excel文件並顯示摘要。該文件正在加載沒有任何錯誤,但不顯示任何內容。在Shiny R App中使用read.xlsx

這裏是我的代碼

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

我沒有測試過這一點,但它看起來像你實際上並不返回任何東西來自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但你實際上並沒有回國了。 Reactives的工作只是看看任何R功能。嘗試運行此:

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

您應該看到,f()不會返回任何內容。它所做的只是做一個無處可去的任務。要真正恢復'hello'你會怎麼做:

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

或者只是:

f <- function() 'hello' 
+0

我也曾嘗試以下,但沒有運氣'infile_read = read.xlsx(INFILE $數據通路,1) 回報( infile_read)' – BigDataScientist

+0

把'browser()'放在你讀入文件的地方之後。該文件是否正在讀入? – MadScone

+0

我在命令行中包含了'browser()'它顯示的瀏覽器>,並在那裏停止。沒有錯誤,沒有輸出。我也嘗試過'browser(text =「Done !!」)',但是「完成!!」不打印。 – BigDataScientist