根据用户选择显示多个摘要统计信息

时间:2019-11-23 20:35:40

标签: r shiny

我正在创建一个闪亮的应用,用户可以从两个下拉菜单中选择州parent_location和县name。他们还可以选择感兴趣的变量layer,该变量然后将生成摘要统计表。至此,我的代码已经完成。

我需要做的是选择其他相似的县(包含在cluster列中),然后也显示该县的摘要统计信息。我似乎无法弄清楚如何A)显示多个汇总统计信息表,以及B)动态创建类似县的列表。

有效的代码

library(shiny)
library(tidyverse)
library(lubridate)

eviction_county_2010 <- read.csv("./eviction_county_2010.csv")

ui <- fluidPage(
  sliderInput(inputId = "year",
              label = "Select a Year:",
              min = 2010,
              max = 2016,
              value = 2010,
              step = 1),

  radioButtons(inputId = "layer",
               label = "Select a Dataset to View:",
               choices = c("Eviction Filing Rate"="eviction_filing_rate", "Percent Rent Burden"="rent_burden",
                           "Percent Renter Occupied"="pct_renter_occupied", "Poverty Rate"="poverty_rate")),

  selectInput(inputId = "state",
              label = "Select a State:",
              eviction_county_2010$parent_location),
  selectInput(inputId = "county",
              label = "Select a County:",
              choices = NULL),
  mainPanel(
    h2("Summary of the variable"),
    verbatimTextOutput("sum")
  )
)

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

  observe({
    x <- filter(eviction_county_2010,parent_location == input$state) %>%
      select(name)
    updateSelectInput(session,"county","Select a County:",choices = unique(x))}
  )

    output$sum <- renderPrint({
      ec <- eviction_county_2010 %>%
        filter(parent_location == input$state) %>%
        filter(name == input$county)
      summary(ec[,input$layer])
  })
}

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

我已尝试显示类似县的代码。它返回Error in .getReactiveEnvironment()$currentContext(): Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)我不确定哪个部分需要放在反应表达式中。

ui <- fluidPage(
  sliderInput(inputId = "year",
              label = "Select a Year:",
              min = 2010,
              max = 2016,
              value = 2010,
              step = 1),

  radioButtons(inputId = "layer",
               label = "Select a Dataset to View:",
               choices = c("Eviction Filing Rate"="eviction_filing_rate", "Percent Rent Burden"="rent_burden",
                           "Percent Renter Occupied"="pct_renter_occupied", "Poverty Rate"="poverty_rate")),

  selectInput(inputId = "state",
              label = "Select a State:",
              eviction_county_2010$parent_location),
  selectInput(inputId = "county",
              label = "Select a County:",
              choices = NULL),
  mainPanel(
    h2("Summary of the variable"),
    verbatimTextOutput("sum")
  )
)

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

  ec <- eviction_county_2010 %>%
    filter(parent_location == input$state) %>%
    filter(name == input$county)
  sel_clust <- unique(ec$cluster)
  sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
  sim_cty <- unique(sim_cty$GEOID)
  sim_cty <- append(sim_cty, unique(ec$GEOID))

  observe({
    x <- filter(eviction_county_2010,parent_location == input$state) %>%
      select(name)
    updateSelectInput(session,"county","Select a County:",choices = unique(x))}
  )

    output$sum <- renderPrint({
    df1 <- eviction_county_2010 %>%
      filter(GEOID == sim_cty[1])
    df2 <- eviction_county_2010 %>%
      filter(GEOID == sim_cty[2])
    df3 <- eviction_county_2010 %>%
      filter(GEOID == sim_cty[3])
    df4 <- eviction_county_2010 %>%
      filter(GEOID == sim_cty[4])
    df5 <- eviction_county_2010 %>%
      filter(GEOID == sim_cty[5])
    summary(df1[,input$layer])
    summary(df2[,input$layer])
    summary(df3[,input$layer])
    summary(df4[,input$layer])
    summary(df5[,input$layer])
  })
}

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

这甚至可能吗?我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

移动此部分:

ec <- eviction_county_2010 %>%
    filter(parent_location == input$state) %>%
    filter(name == input$county)
  sel_clust <- unique(ec$cluster)
  sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
  sim_cty <- unique(sim_cty$GEOID)
  sim_cty <- append(sim_cty, unique(ec$GEOID))

至反应式({})语句。我认为这就是您的错误所在。

例如:

ec <- reactive({
eviction_county_2010 %>%
    filter(parent_location == input$state) %>%
    filter(name == input$county)
  sel_clust <- unique(ec$cluster)
  sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
  sim_cty <- unique(sim_cty$GEOID)
  sim_cty <- append(sim_cty, unique(ec$GEOID))
})

然后在您的服务器代码中使用:

ec() %>%
...stuff...