闪亮:使用不同的变量创建反应过滤器。

时间:2017-03-22 16:39:20

标签: r filter shiny dplyr

我有一个数据框,它将社会人口统计数据与多个网站的意识测量结合起来。每个网站都有一个单独的列,说明该人是否知道该网站(“是”/“否”)。此外,每个受访者应该按他所呈现的人数(变量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)

有没有办法改变滤波器变量而不是值?我也愿意接受其他解决方案。

2 个答案:

答案 0 :(得分:0)

我认为您需要添加第二个UI,该UI取决于第一个选择的变量。在这里,我使用renderUI()在服务器中创建了它。然后,我使用选择的列等于所选变量来对数据进行子集化。希望这会有所帮助。

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  selectInput(inputId = "WebsiteName", label = "Choose a Website", choices = names(df) [c(3:7)]),
  htmlOutput("variableUI"),
  plotOutput("Gender")
)


server <- function(input, output) {

  output$variableUI <- renderUI({
    selectInput(inputId = "variable", label = "Choices", choices = df[,input$WebsiteName])
  })

  dfInput <- reactive({
   ##subsetting is a bit tricky here to id the column on which to subset        
    df[ df[ , input$WebsiteName ] == input$variable, ]
  })

  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)

答案 1 :(得分:0)

You can tidy you dataset to transform it in a more usable way and save yourself some headaches:

Tidy dataset

df<- df %>% 
    gather(web, value, -age, -gender, -popWeight)

UI

Changed the selectInput choices

ui <- fluidPage(
  selectInput(inputId = "websiteName", 
              label = "Choose a Website", 
              choices = unique(df$web)),
  plotOutput("Gender")
)

SERVER

Updated the reactive expression

server <- function(input, output) {

  dfInput <- reactive({
    df %>% filter(web == input$websiteName & value == "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)
  })
}