0

My team and I have developed a single graph that automates based on a selected country on one page of our code. This is for the individual profile page. On the comparison page, we'd like to make the same graph populate for multiple selected countries. Below is the code for the single SelectInput code and single graph code on the profile page. Underneath is the code for the multiple graph SelectizeInput and the multiple graph code on the compare page. The SelectizeInput code work but the graph doesn't show up when I run the app. I keep getting an error that says "Can't transform a data frame with duplicate names." Can anyone provide any input on how to make this graph work for multiple selected countries?

#List of packages

library(shiny) library(dplyr) library(ggplot2) library(tidyr) library(readxl) library(formattable) library(plotly) library(gt) library(plyr) library(factorial2x2) library(shinyBS) library(DT) library(cowplot)

#Profile page SelectInput code

fluidRow(
         column(6,
                wellPanel(
                    selectInput("country",
                                "Select Country",
                                choices = as.list(aypopdata.long$Country))
                )
         ),

#Profile page graph code

ay_res <- reactive({
res <- aypopdata.long %>% filter(aypopdata.long$Country == 
input$country)
req(nrow(res) > 0)
res })
  output$graph <- renderPlot({
bar_one <- (ggplot(ay_res(), aes(Country, Count, fill = Age_Group)) + geom_bar(stat = "identity") + 
                geom_text(aes(label=paste0(Count,"%", " ", "(", (round(Round_Total/1000000,1))," ", "Million", ")")), color="black", size=3.5, position = position_stack(vjust = 0.5))) +
    theme_classic() +
    scale_fill_manual(values = cbp2, labels = c("Young Adolescents (10-14)", "Older Adolescents (15-19)","Older Youth (20-24)"), name = "Age Group") + 
    theme(axis.line.y=element_blank(),
          axis.text.y=element_blank(),
          axis.title.y=element_blank(),
          axis.title.x = element_blank(),
          axis.ticks.y=element_blank(),
          axis.text.x =element_blank(),
          axis.ticks.x =element_blank(),
          axis.line.x =element_blank(),
          legend.position = "right")+
    coord_flip()+ scale_y_reverse() +
    labs(caption="Source: UN Population Division 2020", size=7) 

vals$bar_one <- bar_one
print(bar_one)
   })
output$downloadGraph <- downloadHandler(
filename = function() {
    paste("Adolescents and Youth", "png", sep = ".")
}, 
content = function(file) {
    png(file, width = 980, height = 400) 
    print(vals$bar_one)
    dev.off()
})

#Compare page SelectizeInput code

fluidRow(
         column(6,
                wellPanel(
                    selectizeInput("country",
                                   "Select Up to 4 Countries to Compare",
                                   choices = as.list(aypopdata.long$Country),
                                   multiple = TRUE,
                                   options = list(maxItems = 4))
                )
         ),

#Compare page graph code

 ay_res_compare <- reactive({ 
 req(input$country)
 res <- aypopdata.long %>% filter(aypopdata.long$Country %>% input$country) 
 %>% group_by(Country) %>% summarise(Count = sum(Count))
 output$graph12 <- renderPlot({
bar_compare <- (ggplot(ay_res_compare(), aes(Country, Count, fill = Age_Group)) + geom_bar(stat = "identity") + 
                geom_text(aes(label=paste0(Count,"%", " ", "(", (round(Round_Total/1000000,1))," ", "Million", ")")), color="black", size=3.5, position = position_stack(vjust = 0.5))) +
    theme_classic() +
    scale_fill_manual(values = cbp2, labels = c("Young Adolescents (10-14)", "Older Adolescents (15-19)","Older Youth (20-24)"), name = "Age Group") + 
    theme(axis.line.y=element_blank(),
          axis.text.y=element_blank(),
          axis.title.y=element_blank(),
          axis.title.x = element_blank(),
          axis.ticks.y=element_blank(),
          axis.text.x =element_blank(),
          axis.ticks.x =element_blank(),
          axis.line.x =element_blank(),
          legend.position = "right")+
    coord_flip()+ scale_y_reverse() +
    labs(caption="Source: UN Population Division 2020", size=7) 

vals$bar_compare <- bar_compare
print(bar_compare)

#sessionInfo() <- R version 4.0.2 (2020-06-22) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 14393)

Matrix products: default

Random number generation: RNG: Mersenne-Twister Normal: Inversion Sample: Rounding

locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] cowplot_1.1.1 DT_0.15 shinyBS_0.61 factorial2x2_0.2.0 [5] mvtnorm_1.1-1 survival_3.1-12 plyr_1.8.6 gt_0.2.1
[9] plotly_4.9.2.1 formattable_0.2.0.1 readxl_1.3.1 tidyr_1.1.0
[13] ggplot2_3.3.2 dplyr_1.0.2 shiny_1.5.0

loaded via a namespace (and not attached): [1] tinytex_0.25 tidyselect_1.1.0 xfun_0.16 purrr_0.3.4 splines_4.0.2
[6] lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.2 generics_0.0.2 htmltools_0.5.0
[11] viridisLite_0.3.0 rlang_0.4.11 later_1.1.0.1 pillar_1.4.6 glue_1.4.1
[16] withr_2.4.2 lifecycle_1.0.0 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 [21] htmlwidgets_1.5.1 evaluate_0.14 labeling_0.3 knitr_1.29 fastmap_1.0.1
[26] httpuv_1.5.4 Rcpp_1.0.7 xtable_1.8-4 promises_1.1.1 scales_1.1.1
[31] jsonlite_1.7.0 farver_2.0.3 mime_0.9 packrat_0.5.0 digest_0.6.25
[36] grid_4.0.2 cli_3.0.0 tools_4.0.2 magrittr_1.5 lazyeval_0.2.2
[41] tibble_3.0.3 crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1 Matrix_1.2-18
[46] rsconnect_0.8.16 data.table_1.13.0 rmarkdown_2.3 httr_1.4.2 rstudioapi_0.13
[51] R6_2.4.1 compiler_4.0.2

marci
  • 1
  • 1
  • Welcome to SO! To help us to help would you mind to provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a sample of your data. – stefan Aug 05 '21 at 06:27
  • ... any reason you use the pipe `%>%` in your filter statement? That should probably be `%in%`, i.e. `filter(aypopdata.long$Country %in% input$country)` or simply `filter(Country %in% input$country)` – stefan Aug 05 '21 at 06:28
  • @stefan thanks for your response! please see above I added more information on the package, data and session info for reproducible example. I hope this can help you see more on the issue. – marci Aug 23 '21 at 14:26

0 Answers0