Skip to content
 

mysterious shiny things

(Disclaimer: I’m new to Shiny, and blog posts, but I know something about geography.) ┬áIn the Shiny gallery, take a look at 2001 versus 2002. Something funny happens to Switzerland (and other European countries), in terms of the legend, it moves from Europe to the Middle East. Also, the legend color scheme switches.

 

2001_Shiny

2002_Shiny

 

To reproduce it yourself: download ui.R, server.R, and healthexp.Rds

Have a folder called “App-Health-Exp” in your working directory, with ui.R and server.R in the “App-Health-Exp” folder. Have the dataset healthexp.Rds in your working directory.
Then run this code:

if (!require(devtools))
  install.packages("devtools")
devtools::install_github("jcheng5/googleCharts")

install.packages("dplyr")
install.packages("shiny")

library(shiny)
library(googleCharts)
library(dplyr)

data = readRDS("healthexp.Rds")
head(data)

# Problem isn't the data, it seems that Switzerland is in Europe 
# in both 2001 and 2002:
data[data$Year == 2001 & data$Country == "Switzerland",]
data[data$Year == 2002 & data$Country == "Switzerland",]

runApp("App-Health-Exp")

Anyone know what is happening?

5 Comments

  1. Eamon says:

    Could be something that is happening while rejigging the data from a dataframe to the format that Google Maps expects?

  2. Andrew says:

    It looks to me like CH doesn’t move from Europe to the Middle East: you can mouse-over some of the more orange bubbles to the left of CH such as the UAE to see they’re still coded as Middle East and in a slightly different color. I think the problem is just that between the colors get re-assigned to the regions between 2000 and 2001. Looks like this is happening the googleCharts packages, which I haven’t looked into, but it might do it based on the counts within the regions, which do shift year to year somewhat

    Maybe this is a lesson the dangers of having both peach and salmon in the same palette.

  3. Elin says:

    I’ve built a couple of shiny applications recently, one using the Google Sheets api. The biggest trick is to deal with making it work is to make sure that everything that should be reactive actually updates. I think that what is happening here is that the legend is not specified in the function and is probably cached, hence not updating when the year is updating. It might be that adding the legend explicitly would help.

  4. Joe Cheng says:

    Hi, I’m the author of this example–thanks for pointing out the issue. The bug was essentially due to the fact that these two commands return the regions in different order:

    unique(data[df$Year == 2001,”Region”])
    unique(data[df$Year == 2002,”Region”])

    And the reason for that is that the underlying data is sorted by year and then country name. In 2002, Afghanistan starts appearing in the data, so that beats out Albania as the first country to appear, hence South Asia jumps to the top of the list.

    I thought of two fixes: sort the data by region before returning, or explicitly provide the colors in R rather than letting Google Charts decide. I’ve pushed both fixes to the repo (was originally just going to provide the coloring explicitly, but it appears that Google Charts still wants to *order* the legend according to the appearance, so the sorting is still valuable).

    You can run the example slightly more easily with this command:

    shiny::runGitHub(“jcheng5/googleCharts”, subdir=”inst/examples/bubble”)

Leave a Reply