【问题标题】:Display multiple summary statistics depending on user selection根据用户选择显示多个汇总统计信息
【发布时间】:2019-11-23 20:35:40
【问题描述】:

我正在创建一个闪亮的应用程序,用户可以在其中从两个下拉菜单中选择一个州 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)

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

【问题讨论】:

    标签: r shiny


    【解决方案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...
    

    【讨论】:

    • 如果我想做eviction_county_2010 %&gt;% filter(GEOID %in% sim_cty)怎么办?它似乎对我不起作用。
    • 如果您需要从响应式环境中返回多个内容,您可以将 list(sim_city = sim_city, sel_clst = sel_clyst) 作为响应式环境中的最后一个对象,然后稍后在您的代码中将其引用为 ec()$sel_clst, ec()$sim_city
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多