R Shiny - 如何通过 checkboxGroupInput 过滤 [英] R Shiny - How to filter by checkboxGroupInput

查看:27
本文介绍了R Shiny - 如何通过 checkboxGroupInput 过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了以下 Shiny 代码:

I have set up the following Shiny code:

global.R:

library(shiny)
library(gapminder)
library(tidyverse)
library(scales)

ui.R:

fluidPage(
titlePanel("Gapminder Hierarchical Clustering of Countries"),
sidebarLayout(
    sidebarPanel(
        sliderInput("numCluster", "Choose number of clusters:", 2, 6, 2),
        checkboxGroupInput("ContinentSelect", "Select which continents to include in the cluster analysis:", 
                                             choices = levels(gapminder$continent), selected = levels(gapminder$continent)),
        sliderInput("numYear", "Select years to include in the cluster analysis:", min(gapminder$year), max(gapminder$year), 
                                c(min(gapminder$year), max(gapminder$year)), step = 5, ticks = FALSE, sep = "")
),
mainPanel(
plotOutput("Chart"),
br(),br(),
tableOutput("SummaryClusters")
)
)
)

和 server.R:

And server.R:

function(input, output){

gapcluster <- function(df, numCluster){
    df_scaled <- df %>% mutate(scale_lifeExp = scale(lifeExp),
                                                         scale_pop = scale(pop),
                                                         scale_gdpPercap = scale(gdpPercap))
    gapclusters <- df_scaled[,c("scale_lifeExp", "scale_pop", "scale_gdpPercap")] %>% dist() %>% hclust()
    Clustercut <- cutree(gapclusters, numCluster)
    return(Clustercut)
}

#Creating a data frame based on inputs
filtered_gap <- reactive({ #If no continents are selected
    if (is.null(input$ContinentSelect)) {
        return(NULL)
    }    

    gapminder %>%
        filter(year >= input$numYear[1],
                 year <= input$numYear[2],
                     continent == input$ContinentSelect)
})

filtered_gap2 <- reactive({ 
    filtered_gap() %>% mutate(cluster_group = gapcluster(filtered_gap(), input$numCluster),
                                                                     country = reorder(country, -1 * pop)) %>%
        arrange(year, country)
})

SummaryTable <- reactive({
    if (is.null(input$ContinentSelect)) {
        return(NULL)
    } 

    filtered_gap2() %>% group_by(cluster_group) %>% summarise(`Number of countries` = n(),
                                                                                                                        `Life expectancy` = mean(lifeExp),
                                                                                                                        `Population size` = prettyNum(mean(pop), big.mark = ","),
                                                                                                                        `GDP per capita` = prettyNum(mean(gdpPercap), big.mark = ",")) %>% 
        rename(`Cluster Group` = cluster_group)
})

output$Chart <- renderPlot({
    if (is.null(filtered_gap2())) {
        return()
    }

    filtered_gap2() %>% ggplot(aes(x = gdpPercap, y = lifeExp, fill = country)) +
        scale_fill_manual(values = country_colors) +
        facet_wrap(~ cluster_group) +
        geom_point(aes(size = pop), pch = 21, show.legend = FALSE) +
        scale_x_log10(limits = c(230, 115000), labels = comma) +
        scale_size_continuous(range = c(1,40)) + ylim(c(20, 87)) + 
        labs(x = "GDP per capita", y = "Life Expectancy")
})

output$SummaryClusters <- renderTable({
    SummaryTable()
})
}

大陆的过滤方式存在问题.在默认设置中,我们可以看到表中共有 344 个国家.但是如果我取消选中大洋洲,这个数字会上升(?)到 420 个国家.到底是怎么回事?我相当肯定这个问题与 server.R 文件中的 filter(continent == input$ContinentSelect) 行有关,但我不知道如何解决它.>

There is an issue in how the continents are being filtered. In the default setting, we can see in the table there are a total of 344 countries. But if I uncheck Oceania, the number goes up (?) to 420 countries. What is going on? I'm fairly certain the issue has something to do with the line filter(continent == input$ContinentSelect) in the server.R file, but I can't figure how to fix it.

推荐答案

当我将 filter(continent == input$ContinentSelect) 更改为 filter(continent %in% input$大陆选择).菜鸟错误.

it works when I change filter(continent == input$ContinentSelect) to filter(continent %in% input$ContinentSelect). Rookie mistake.

这篇关于R Shiny - 如何通过 checkboxGroupInput 过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆