2016-11-24 76 views
0
过滤

我已经设置了以下闪亮代码:[R闪亮 - 如何通过checkboxGroupInput

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 :

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)行有关,但我无法弄清楚如何解决它。

回答

1

它在我将filter(continent == input$ContinentSelect)更改为filter(continent %in% input$ContinentSelect)时有效。菜鸟的错误。