5
votes

If I may, I have another question about reading a csv file while using Shiny.

I did spend quite a bit of time searching and rtfm ... my apologies if I missed something. Most answers seemed a bit too fancy, with user interaction when selecting a data file. I simply want R Shiny to read a data file (just the one) without any user interaction.

I have the standard files ui.R and server.R I place them in a working directory.

I have a csv file with data which I place in a subdirectory called 'data' (based on a tutorial here http://shiny.rstudio.com/tutorial/lesson5/)

From within R Studio I manually set the working directory to the one with the ui.R and server.R files. I load shiny and do runApp(). A line of script in server.R tries to use read.csv to read the data into an object 'd.in'.

This did not work, so I tried coercing the working directory before reading the csv file, and then resetting it after reading the data and before the shinyServer code.

code snippet:

wd.datapath = paste0(getwd(),"/data")
wd.init = getwd()
setwd(wd.datapath)

d.in = read.csv("shinyDataITB.csv", header = TRUE)

setwd(wd.init)

I get an error message: "ERROR: object 'd.in' not found"

If I manually load the csv data file prior to running runApp then everything else seems to work. I'm not sure how I've botched this but any help will be welcome.

The ui.R file

##### ui.R #####

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Supply ITB"), 

  sidebarPanel( 

    radioButtons(inputId = "in.facnum",
                 label = "Choose Facility",
                 choices = levels(d.in$facnum)) 
    ),  # end sidebarPanel

  mainPanel(
    h3("SPC chart"), 
    plotOutput("plotDisplay")
    )   # end mainPanel

  ))    # end Sidebar

And the server.R file

##### server.R #####

# load packages -------------------------------------------------------------

library(shiny)
library(qcc)

# load the data -------------------------------------------------------------


wd.datapath = paste0(getwd(),"/data")
wd.init = getwd()
setwd(wd.datapath)

#d.in = read.csv(file.choose(), header = TRUE)

d.in = read.csv("shinyDataITB.csv", header = TRUE)

setwd(wd.init)


# add proportions related to fill_lines -------------------------------------

d.in$whprop = d.in$wh / d.in$volume
d.in$dmprop = d.in$dm / d.in$volume
d.in$mmprop = d.in$mm / d.in$volume


# select SPC response variable (using proportions) --------------------------

qccvar = "whprop"


# shiny server body ---------------------------------------------------------

shinyServer(function(input,output) { 


# Individuals (X) chart -----------------------------------------------------

  output$plotDisplay <- renderPlot({

    # select subset for specific facility

    d.strata = subset(d.in, d.in$facnum == input$in.facnum)  # subset

    d.strata = d.strata[order(d.strata$year, d.strata$monthnum, 
                              decreasing = FALSE),]  # order by month

    # create SPC chart

    x.chart = qcc( d.strata[,qccvar], type = "xbar.one", 
               title = paste("Individuals chart\n", input$in.facnum, qccvar) )


  })  # end renderPlot

})    # end shinyServer


### END CODE ###

I've put the data file in a dropbox folder here shinyDataITB.csv

2

2 Answers

3
votes

What could also work is building the radio button in server.R via:

output$ui <- renderUI({sidebarPanel( 

radioButtons(inputId = "in.facnum",
             label = "Choose Facility",
             choices = levels(d.in$facnum)) 
),  # end sidebarPanel

And in ui.R via:

uiOutput("ui")),
1
votes

Your problem is in the ui.R. You have a reference to d.in$facnum but d.in does not exist in the UI. It only exists in server.R. Variables are not shared across the two instances like that. The server and UI need to communicate via the input and output parameters.

If you want to set the value of choices for your radio button from the data on the server, you can use updateRadioButtons. First, make sure you include the session parameter in the shinyServer function.

shinyServer(function(input,output, session) { 
    ...
    updateRadioButtons(session, "in.facnum", choices=levels(d.in$facnum))
    ...
}

Then, this will set the values in that input.