具有来自调查包对象的反应数据的闪亮应用程序
Shiny app with reactive data from survey package object
我正在尝试构建一个简单的应用程序来查看对教育部调查的回复。我正在使用 survey package 来调整复杂抽样设计的原始数据。这个包有几个内置函数,其中之一是 svytable()
。 svytable()
的输出可以直接用基函数 barplot()
绘制。这很容易做到——但是,我无法让它在 Shiny 的反应式数据环境中工作。
(我希望能够使用 ggplot2
绘制此 table 中的数据,如果您将调查 table 对象强制到数据框,这是可能的,但是我想我应该一次开始一个步骤,看看我的应用程序出了什么问题。)
如果您能找出我在这里出错的地方,请告诉我:
我已将数据示例上传到此 link。这是我的应用程序代码的简化示例,目前无法正常工作:
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)
您需要为 svytable
创建模型公式,如下所示:
model.formula <- formula(paste0("~",input$xVar," + ",input$groupVar))
plot_tab <- svytable(model.formula, jrr_dat)
我正在尝试构建一个简单的应用程序来查看对教育部调查的回复。我正在使用 survey package 来调整复杂抽样设计的原始数据。这个包有几个内置函数,其中之一是 svytable()
。 svytable()
的输出可以直接用基函数 barplot()
绘制。这很容易做到——但是,我无法让它在 Shiny 的反应式数据环境中工作。
(我希望能够使用 ggplot2
绘制此 table 中的数据,如果您将调查 table 对象强制到数据框,这是可能的,但是我想我应该一次开始一个步骤,看看我的应用程序出了什么问题。)
如果您能找出我在这里出错的地方,请告诉我:
我已将数据示例上传到此 link。这是我的应用程序代码的简化示例,目前无法正常工作:
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)
您需要为 svytable
创建模型公式,如下所示:
model.formula <- formula(paste0("~",input$xVar," + ",input$groupVar))
plot_tab <- svytable(model.formula, jrr_dat)