【发布时间】:2017-08-14 22:15:12
【问题描述】:
我有一个数据框,它将社会人口统计数据与多个网站的意识测量相结合。每个网站都有一个单独的列,说明此人是否知道该网站(“是”/“否”)。此外,每个受访者都应该根据他出席的人数来加权(变量 popWeight)。
我想创建一个闪亮的应用程序,为知道所选网站的人显示图表。该网站应该可以通过 selectInput () 按钮进行选择。
我发现了几篇关于 stackoverflow 的文章,其中涵盖了使用 dplyr+shiny 的数据集过滤器。但是它们都改变了变量值而不是变量本身。
我尝试使用以下方法,但没有成功(编码示例见下文)。
[Use shiny text input and dplyr to filter rows in a dataframe
示例数据框:
gender <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Male", "Female", "Missing Value"))
age <- sample(18:55, 5, replace=TRUE)
web1 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web2 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web3 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web4 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web5 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
popWeight <- sample(1000:1500, 5, replace=TRUE)
df <- data.frame(gender, age, web1, web2, web3, web4, web5, popWeight)
df
我想以交互方式做的事情:
library(ggplot2)
library(dplyr)
df1 <- filter (df, web1 == "Yes")
ggplot(df1)+
aes(x=gender, y=popWeight/sum(popWeight))+
stat_summary(fun.y = sum, geom = "bar")+
scale_y_continuous("Population (%)", labels = scales::percent)
我尝试了什么
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
selectInput(inputId = "WebsiteName", label = "Choose a Website", choices = names(df) [c(3:7)]),
plotOutput("Gender")
)
server <- function(input, output) {
dfInput <- reactive({
df %>% filter (input$WebsiteName == "Yes")
})
output$Gender <- renderPlot({
df1 <- dfInput()
ggplot(df1)+
aes(x=gender, y=popWeight/sum(popWeight))+
stat_summary(fun.y = sum, geom = "bar")+
scale_y_continuous("Population (%)", labels = scales::percent)
})
}
shinyApp(ui = ui, server = server)
有没有办法改变过滤器变量而不是值?我也愿意接受其他解决方案。
【问题讨论】: