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
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")),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With