2014-05-23 35 views
9

我有一個關於Shiny的問題。我會在前言中提到,我確實花時間在Google和SO檔案上,嘗試了一些東西,但仍然有些失望。我會爲任何發佈失言而道歉,並提前感謝您提供任何指導。R Shiny - ui.R似乎無法識別服務器讀取的數據幀。R

我想我認爲是一個非常基本的任務,以便學習Shiny,從其中一個Shiny畫廊示例中調整代碼。我將csv文件讀入數據幀(df.shiny)。我想選擇與一個設施相關的業務績效數據(ITBpct)(水平爲df.shiny$Facility),並將其顯示在SPC圖表中(使用qcc)。

我的問題似乎與server.R的數據可用於ui.R有關。我相信數據被讀入數據框(它在控制檯中打印),但不可用於ui.R。我相信我只是忽略了一些東西,但還沒有弄明白。

我使用Shiny網站上記錄的文件夾結構,在工作目錄子文件夾(「Shiny-App-1」)中的server.R和ui.R以及該文件夾中的子文件夾中的數據(Shiny -app-1 /數據)。

爲了幫助跟蹤錯誤,我插入的代碼通過在控制檯中打印SRV-2UI-1運行。 Firefox打開。然後錯誤。

options(browser = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe") 
library(shiny) 
runApp("Shiny-App-1") 

server.R代碼

library(shiny) 
library(qcc) 
print("SRV-1") # for debugging 

df.shiny = read.csv("data/ITBDATA.csv") 
print(df.shiny) # for debugging 
print("SRV-2") # for debugging 


shinyServer(function(input, output, session) { 
    # Combine the selected variables into a new data frame 
    # assign xrow <- Facility 

    print("SRV-3") # for debugging 
    selectedData <- reactive({ subset(df.shiny, Facility %in% input$xrow) }) 
    print("SRV-4") # for debugging 

    output$plot1 <- renderPlot({ qcc(selectedData$ITBpct, type = 'xbar.one') }) 
}) 

ui.R代碼

library(shiny) 
print("UI-1") # for debugging 

shinyUI(pageWithSidebar(
    headerPanel('SPC Chart by Facility'), 
    sidebarPanel(selectInput('xrow', 'Facility', levels(df.shiny$Facility))), 
    mainPanel(plotOutput('plot1')) 

)) 

錯誤消息

ERROR: object 'df.shiny' not found 

我可以使數據可用。 (不知道如何樣本附於本說明。)

會話信息

> sessionInfo() 
R version 3.1.0 (2014-04-10) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

attached base packages: 
[1] splines stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] plyr_1.8.1  forecast_5.4  timeDate_3010.98 zoo_1.7-11  doBy_4.5-10  
[6] MASS_7.3-31  survival_2.37-7 gplots_2.13.0 car_2.0-20  ggplot2_0.9.3.1 
[11] lattice_0.20-29 qcc_2.3   shiny_0.9.1 

回答

11

的問題是你在你的ui.R文件中使用df.shiny$Facilitydf.shiny是沒有定義。 ui無法看到server中的所有變量,它們都有其他溝通方式。

爲了實現此目的,您需要在服務器上構建selectInput,然後在UI中呈現它。在你的服務器上,添加

shinyServer(function(input, output, session) { 
    output$facilityControl <- renderUI({ 
     facilities <- levels(df.shiny$Facility) 
     selectInput('xrow', 'Facility', facilities) 
    }) 

    selectedData <- reactive({ subset(df.shiny, Facility %in% input$xrow) }) 
    output$plot1 <- renderPlot({ qcc(selectedData$ITBpct, type = 'xbar.one') }) 
}) 

,然後在UI改變

shinyUI(pageWithSidebar(
    headerPanel('SPC Chart by Facility'), 
    sidebarPanel(uiOutput("facilityControl"), 
    mainPanel(plotOutput('plot1'))  
)) 
+0

嗨......非常感謝!這讓我進入下一步。我現在有其他問題,但我會做更多的閱讀,也許另一個職位。再次,謝謝! – crlong