Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny - ui.R seems to not recognize a dataframe read by server.R

Tags:

r

shiny

I have a question about Shiny. I'll preface by offering that I did spend time with Google and SO archives, tried a few things, but still somehow miss something. I'll apologize for any posting faux pas and give thanks in advance for any guidance.

I'm trying what I believe is a very basic task in order to learn Shiny, adapting code from one of the Shiny gallery examples. I read a csv file into a dataframe (df.shiny). I want to select business performance data (ITBpct) related to one facility (levels of df.shiny$Facility) and display it in an SPC chart (using qcc).

My issue seems to be related to making the data from server.R available to ui.R. I believe that the data is read into the dataframe (it prints in the console), but is not available to ui.R. I'm convinced that I've simply overlooked something, but haven't yet figured it out.

I'm using the folder structure noted on the Shiny site, with server.R and ui.R in a working directory subfolder ("Shiny-App-1") and the data in a subfolder to this folder (Shiny-App-1/data).

The code that I've inserted to help track the error runs through printing SRV-2 and UI-1 in the console. Firefox opens. Then the error.

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

server.R Code

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 Code

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 Message

ERROR: object 'df.shiny' not found

I can make the data available. (Wasn't sure how to attach a sample to this note.)

SESSION INFO

> 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 
like image 330
crlong Avatar asked May 23 '14 15:05

crlong


2 Answers

The problem is you are using df.shiny$Facility in your ui.R file and df.shiny is not defined there. The ui cannot see all the variables in the server, they have other ways to communicate.

To get this to work, you need to build the selectInput on the server, and then render it in the UI. In your server, add

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')  })
})

and then change the ui to

shinyUI(pageWithSidebar(
    headerPanel('SPC Chart by Facility'),
    sidebarPanel( uiOutput("facilityControl" ),
    mainPanel( plotOutput('plot1') )    
))
like image 69
MrFlick Avatar answered Oct 18 '22 16:10

MrFlick


Alternatively you could put all your R objects that need to be accessible by both server.R and ui.R in a global.R file.

More on that: http://shiny.rstudio.com/articles/scoping.html#global-objects

like image 43
Jeremy Yeo Avatar answered Oct 18 '22 15:10

Jeremy Yeo