Search code examples
rgoogle-visualizationgooglevis

How to set a "formatted value" in googleVis?


I am using googleVis and shiny to (automatically) create a Organizational Chart. Similar to this question: Google Visualization: Organizational Chart with fields named the same, I want to use formatted values in googleVis to be able to create fields in an organizational chart, which have the same name. I suspect it has something to do with roles but I cannot figure the correct syntax out.

The help page for gvisOrgChart mentiones formatted values but does not say how to set them: "You can specify a formatted value to show on the chart instead, but the unformatted value is still used as the ID."

## modified example from help page
library(googleVis)
Regions[7,1] = Regions[8,1] # artificially create duplicated name in another parent node

Org <- gvisOrgChart(Regions)

plot(Org)

In the above example the duplicated name (Mexico) is only shown once in the chart. I want both of them to be drawn (One in the Europe and one in the America parent node).

Thank you for your help

cateraner


Solution

  • After talking to one of the developers of the googleVis package I got the solution to the problem now. The formatted value contains extra speak marks, which have to be removed before the text is usable as HTML.

    ## modified example from help page
    library(googleVis)
    
    # add new entry 
    levels(Regions$Region) = c(levels(Regions$Region), "{v: 'Germany.2', f: 'Germany'}")
    Regions[8,1] = "{v: 'Germany.2', f: 'Germany'}"
    
    Org <- gvisOrgChart(Regions)
    
    # remove extra speak marks
    Org$html$chart <- gsub("\"\\{v", "\\{v", Org$html$chart)
    Org$html$chart <- gsub("\\}\"", "\\}", Org$html$chart)
    
    plot(Org)
    

    In the resulting graph you have two times "Germany", one under node "America" and one under "Europe". The same way you could add HTML formations to your text (color, font, etc.).

    Thanks too Markus Gesmann for helping me on that.