首页 文章

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

提问于
浏览
2

我有一个数据框架,结合了社会人口统计学数据和多个网站的意识测量 . 每个网站都有一个单独的列,说明该人是否知道该网站(“是”/“否”) . 此外,每个受访者应该根据他呈现的人数(变量popWeight)进行加权 .

我想创建一个闪亮的应用程序,显示知道所选网站的人的情节 . 该网站应该可以通过selectInput()按钮进行选择 .

我在stackoverflow上找到了几篇关于dplyr有光泽的数据集过滤器的文章 . 但它们都改变了变量值而不是变量本身 .

我尝试使用以下内容,但没有成功(编码示例见下文) .

[Use shiny text input and dplyr to filter rows in a dataframe

Sample Data Frame:

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

What I want to do in an interactive way:

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)

What I tried

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

    我认为你需要添加第二个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)
    
  • 0

    您可以使用 tidy 数据集以更有用的方式对其进行转换,并为您节省一些麻烦:

    整洁的数据集

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

    UI

    更改了 selectInput 选项

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

    SERVER

    更新了反应表达式

    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)
      })
    }
    

相关问题