【问题标题】:Filter dataframe for multiple inputs in SelectizeInput Shiny在 SelectizeInput Shiny 中过滤多个输入的数据框
【发布时间】:2018-04-03 04:42:04
【问题描述】:

我有一个 Shiny 应用程序,它根据用户选择的人口统计数据显示来自名为 demographicdata 的数据框的各种指标。

5 个地区的样本数据-

demographicdata

    structure(list(Geography = c("AK", "AL", "AR", "AZ", "CA"), `Sum of Total Persons` = c(855385L, 
5293523L, 3766990L, 6420943L, 37431000L), `Sum of Total Families2` = c(199849L, 
1375812L, 991124L, 1551127L, 8540363L), `Sum of Total Pop Non-Hispanic White` = c(514947L, 
3513711L, 2825573L, 3712555L, 15076071L), `Sum of Total Pop Non-Hispanic Black/African American` = c(23112L, 
1430075L, 585477L, 239295L, 2165712L), `Sum of Total Pop Hispanic only` = c(44256L, 
195145L, 226525L, 1903995L, 14054449L), `Sum of Total Pop Non-Hispanic American Indian/Alaska Native` = c(164157L, 
28974L, 25122L, 259769L, 169113L), `Sum of Total Pop Non-Hispanic Asian` = c(45630L, 
54301L, 39553L, 170643L, 4777879L), `Sum of Total Pop Non-Hispanic Native Hawaiian/other Pacific Islander` = c(7749L, 
2082L, 5732L, 10969L, 128793L), `Sum of Total Female Pop2` = c(406897L, 
2722926L, 1914617L, 3230172L, 18821643L), `Sum of LMI Pop` = c(18648L, 
256265L, 95286L, 431282L, 2680252L), `Sum of MIN Pop` = c(229268L, 
1243264L, 531623L, 2059293L, 22733389L)), .Names = c("Geography", 
"Sum of Total Persons", "Sum of Total Families2", "Sum of Total Pop Non-Hispanic White", 
"Sum of Total Pop Non-Hispanic Black/African American", "Sum of Total Pop Hispanic only", 
"Sum of Total Pop Non-Hispanic American Indian/Alaska Native", 
"Sum of Total Pop Non-Hispanic Asian", "Sum of Total Pop Non-Hispanic Native Hawaiian/other Pacific Islander", 
"Sum of Total Female Pop2", "Sum of LMI Pop", "Sum of MIN Pop"
), class = "data.frame", row.names = c(NA, -5L))

当只选择一个人口统计时,该应用程序运行良好,但当我尝试在 selectizeInput 框中引入多个选择时,它会崩溃。

这是整个代码的最小示例

app.r

ui <- fluidPage(

sidebarLayout(
sidebarPanel(

  selectizeInput(inputId = "demography", 'Choose MSA/MD for calculating Demographics', 
                 choices = c("Select a geography",sort((demographicdata$Geography)))
  ),

  actionButton(inputId="DatabyPage", label="Calculate")

),

mainPanel(

  tabsetPanel(

    tabPanel("Page 1 - Demographic Summary",

             fluidRow(
               column(width =12, tableOutput("Population")))
    )))
 ))

server <- shinyServer(function(input, output, session) {

observeEvent(input$DatabyPage, {

  Population = c("Total Population",
                 "Total Families",
                 "White",
                 "Black",
                 "Hispanic/Latino",
                 "Native American",
                 "Asian",
                 "HOPI",
                 "Total Minority",
                 "Number of Females",
                 "Population in LMI Tracts",
                 "Population in Minority Tracts"
  )

  Values = c(demographicdata$`Sum of Total Persons`[demographicdata$Geography== input$demography], 
             demographicdata$`Sum of Total Families2`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Non-Hispanic White`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Non-Hispanic Black/African American`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Hispanic only`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Non-Hispanic American Indian/Alaska Native`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Non-Hispanic Asian`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of Total Pop Non-Hispanic Native Hawaiian/other Pacific Islander`[demographicdata$Geography==input$demography],
             (demographicdata$`Sum of Total Persons`[demographicdata$Geography==input$demography]) - (demographicdata$`Sum of Total Pop Non-Hispanic White`[demographicdata$Geography==input$demography]),
             demographicdata$`Sum of Total Female Pop2`[demographicdata$Geography==input$demography], 
             demographicdata$`Sum of LMI Pop`[demographicdata$Geography==input$demography],
             demographicdata$`Sum of MIN Pop`[demographicdata$Geography==input$demography])


  Percent = c("","",
              Values[3]/Values[1],
              Values[4]/Values[1],
              Values[5]/Values[1],
              Values[6]/Values[1],
              Values[7]/Values[1],
              Values[8]/Values[1],
              Values[9]/Values[1],
              Values[10]/Values[1],
              Values[11]/Values[1],
              Values[12]/Values[1])

  Values = prettyNum(Values,big.mark=',')
  Percent[3:12] = paste(round(100*as.numeric(Percent[3:12]), 2), "%", sep="")


  Population = data.frame(Population, Values, Percent)
  colnames(Population) = c("Population Demographics", "Number", "Percent")
  rm(Values, Percent)

  output$Population <- renderTable({

    Population
  })

})

 })
# Run the application 
shinyApp(ui = ui, server = server)

基本上我想访问数据框以获取多种人口统计数据

示例 - 如果用户选择 Alabama 和 Montana,我想访问 Alabama 和 Montana 的 demographicdata$'Sum of Total Persons' 的累积总和。

我知道我必须在这里引入某种反应性元素,但我很难将它们组合在一起。

有什么想法吗?非常感谢!

【问题讨论】:

    标签: r dataframe reactive-programming shiny


    【解决方案1】:

    有几件事要改变。

    1) 在selectizeInput 中使用multiple=TRUE 2) 将== 替换为%in% 进行多个元素比较

     ui <- fluidPage(
    
      sidebarLayout(
        sidebarPanel(
    
          selectizeInput(inputId = "demography", 'Choose MSA/MD for calculating Demographics', multiple = TRUE, 
                         choices = c("Select a geography",sort((demographicdata$Geography)))
          ),
    
          actionButton(inputId="DatabyPage", label="Calculate")
    
        ),
    
        mainPanel(
    
          tabsetPanel(
    
            tabPanel("Page 1 - Demographic Summary",
    
                     fluidRow(
                       column(width =12, tableOutput("Population")))
            )))
      ))
    
    server <- shinyServer(function(input, output, session) {
    
      observeEvent(input$DatabyPage, {
    
        Population = c("Total Population",
                       "Total Families",
                       "White",
                       "Black",
                       "Hispanic/Latino",
                       "Native American",
                       "Asian",
                       "HOPI",
                       "Total Minority",
                       "Number of Females",
                       "Population in LMI Tracts",
                       "Population in Minority Tracts"
        )
    
        Values = c(sum(demographicdata$`Sum of Total Persons`[demographicdata$Geography %in% input$demography]), 
                   sum(demographicdata$`Sum of Total Families2`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Non-Hispanic White`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Non-Hispanic Black/African American`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Hispanic only`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Non-Hispanic American Indian/Alaska Native`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Non-Hispanic Asian`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of Total Pop Non-Hispanic Native Hawaiian/other Pacific Islander`[demographicdata$Geography %in% input$demography]),
                   (sum(demographicdata$`Sum of Total Persons`[demographicdata$Geography %in% input$demography])) - (sum(demographicdata$`Sum of Total Pop Non-Hispanic White`[demographicdata$Geography %in% input$demography])),
                   sum(demographicdata$`Sum of Total Female Pop2`[demographicdata$Geography %in% input$demography]), 
                   sum(demographicdata$`Sum of LMI Pop`[demographicdata$Geography %in% input$demography]),
                   sum(demographicdata$`Sum of MIN Pop`[demographicdata$Geography %in% input$demography]))
    
    
        Percent = c("","",
                    Values[3]/Values[1],
                    Values[4]/Values[1],
                    Values[5]/Values[1],
                    Values[6]/Values[1],
                    Values[7]/Values[1],
                    Values[8]/Values[1],
                    Values[9]/Values[1],
                    Values[10]/Values[1],
                    Values[11]/Values[1],
                    Values[12]/Values[1])
    
        Values = prettyNum(Values,big.mark=',')
        Percent[3:12] = paste(round(100*as.numeric(Percent[3:12]), 2), "%", sep="")
    
    
        Population = data.frame(Population, Values, Percent)
        colnames(Population) = c("Population Demographics", "Number", "Percent")
        rm(Values, Percent)
    
        output$Population <- renderTable({
    
          Population
        })
    
      })
    
    })
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    -输出

    【讨论】:

    • 谢谢!无论如何,我可以将输出显示为所选输入的集合吗?我希望输出在一个实例中显示整个 AL AZ 的总人口总和,而不是在每个输入选择中显示一个低于另一个。例如上面的输出必须显示:Total Population = 583824
    • @AshwinRamesh 在这种情况下,只需执行sum。更新了帖子
    • 你是救生员,谢谢!!我不知道为什么我没有想到这一点,我想我只是盯着这个太久了。
    猜你喜欢
    • 2020-08-15
    • 2018-11-08
    • 1970-01-01
    • 2019-09-08
    • 2019-06-12
    • 2016-06-03
    • 2019-10-11
    • 2020-02-05
    • 2018-12-02
    相关资源
    最近更新 更多