Peter Miksza
Peter Miksza

Reputation: 409

Shiny app with reactive data from survey package object

I'm trying to build a simple app to look at responses to a Department of Education survey. I'm using the survey package to adjust the raw data for the complex sampling design. This package has several built in functions, one of which is svytable(). The output of svytable() can be plotted directly with the base function barplot(). This is easy enough to do - but, I can't get it to work in a reactive data environment in Shiny.

(I'd love to be able to plot the data in this table with ggplot2, which is possible if you coerce the survey table object to a data frame but I thought I'd start one step at a time to see where I'm going wrong with the app.)

Please let me know if you can identify what I'm getting wrong here:

I've uploaded a sample of the data to this link. Here is a simplified sample of my app code that is currently not working:

library(shiny)
library(foreign)
library(survey)

# Read in data (from wherever you save the data...)
secon_dat <- read.spss("FRSS_Short_Data.sav", 
                   use.value.labels = TRUE,
                   to.data.frame = TRUE)

# Define UI
ui <- fluidPage(

titlePanel("Secondary Arts Teacher Data 2009-2010"),

sidebarLayout(
  sidebarPanel(
    selectInput("xVar",
                 "Select Questionnaire Item",
                choices = c("School enrollment size" = "SIZE", "School 
                            community type" = "URBAN", 
                            "School region" = "OEREG", "School minority enrollment" = "MINST", 
                            "Percent of students in the school eligible for free or reduced-price lunch" = "POVST", 
                            "School level" = "LEVEL"),
                selected = "School community type"),
    selectInput("groupVar",
                "Select Grouping Item",
                choices = c("School enrollment size" = "SIZE", "School community type" = "URBAN", 
                            "School region" = "OEREG", "School minority enrollment" = "MINST", 
                            "Percent of students in the school eligible for free or reduced-price lunch" = "POVST", 
                            "School level" = "LEVEL"),
                selected = "School region")
  ),
  mainPanel(
     plotOutput("Plot")

  )))

server <- function(input, output) {

dat <- reactive({

 # Define complex survey design with replicate weights
      jrr_dat <- svrepdesign(variables = secon_dat[, c(which(colnames(secon_dat) == input$xVar),
                                                  which(colnames(secon_dat) == input$groupVar))],
                        repweights = secon_dat[, 9:58],
                        data = secon_dat,
                        type = "JK1",
                        combined.weights = TRUE,
                        weights = secon_dat[, 8],
                        scale = 1, rscales = 1)

 plot_tab <- svytable(~input$xVar + input$groupVar, jrr_dat)

 res <- list(plot_tab = plot_tab)

})

output$Plot <- renderPlot({

 res <- dat()

 barplot(res$plot_tab)

 })

}


shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 664

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24262

You need to create the model formula for svytable as follows:

model.formula <- formula(paste0("~",input$xVar," + ",input$groupVar))
plot_tab <- svytable(model.formula, jrr_dat)

enter image description here

Upvotes: 2

Related Questions