R 在SelectizeInput中筛选多个输入的数据帧

R 在SelectizeInput中筛选多个输入的数据帧,r,dataframe,reactive-programming,shiny,R,Dataframe,Reactive Programming,Shiny,我有一个闪亮的应用程序,它根据用户选择的人口统计显示了名为demographicsdata的数据框中的各种指标 5个地理位置的样本数据- 人口统计数据 structure(list(Geography = c("AK", "AL", "AR", "AZ", "CA"), `Sum of Total Persons` = c(855385L, 5293523L, 3766990L, 6420943L, 37431000L), `Sum of Total Families2` = c(19

我有一个闪亮的应用程序,它根据用户选择的人口统计显示了名为
demographicsdata
的数据框中的各种指标

5个地理位置的样本数据-

人口统计数据

    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框时,它崩溃了

下面是整个代码的一个最小示例

应用程序r


ui有两件事需要改变

1) 在
selectizeInput
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)

ui感谢您的支持!!无论如何,我可以将输出显示为所选输入的集合吗?我希望输出在一个实例中显示整个AL AZ的总人口总数,而不是在每个输入选择中显示一个低于另一个。例如,上述输出必须显示:总人口=583824@AshwinRamesh在这种情况下,只需执行
求和
。更新了帖子你是个救生员谢谢!!我不知道为什么我没有想到这一点,我想我只是盯着这个看太久了。
 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)