File: googleVis_intro.Rmd

package info (click to toggle)
r-cran-googlevis 0.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 5
file content (1000 lines) | stat: -rw-r--r-- 35,306 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Introduction to googleVis"
author: "Markus Gesmann and Diego de Castillo"
abstract: "The googleVis package provides an interface between R and the Google Charts API. Google Charts offer interactive charts which can be embedded into web pages. The functions of the googleVis package allow the user to visualise data stored in R data frames with Google Charts without uploading the data to Google. The output of a googleVis function is HTML code that contains the data and references to JavaScript functions hosted by Google. googleVis makes use of the internal R HTTP server to display the output locally. A modern browser with an Internet connection."
date: "`r Sys.Date()`"
output:
  html_document:
    self_contained: false 
    toc: true
    toc_depth: 2
    toc_float: true
    number_sections: true
bibliography: googleVis.bib
---

<!--
%\VignetteIndexEntry{Introduction to googleVis}
%\VignetteDepends{googleVis}
%\VignetteKeywords{google, visualisation, api}
%\VignettePackage{googleVis}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
-->

```{js, echo=FALSE}
$('.title').css('color', 'darkred')
```
  
```{r options, echo=FALSE, warning=FALSE, message=FALSE}
library(knitr)
Rpackage <- function (pkg) {sprintf("*%s*", pkg)}
CRANpkg <- function (pkg){
    cran <- "https://CRAN.R-project.org/package"
    fmt <- "[%s](%s=%s)"
    Rpackage(sprintf(fmt, pkg, cran, pkg))
}

library(googleVis)
options(prompt = "R> ", digits = 4, 
        show.signif.stars = FALSE,
        gvis.plot.tag='chart')
```


# Introduction

## Motivation

In 2006 Hans Rosling gave an inspiring talk at
TED [@HansRoslingTedTalk] about social and economic developments
in the world over the past 50 years, which challenged the views and
perceptions of many listeners. Rosling had used extensive data analysis
to reach his conclusions.  To visualise his [talk](https://embed.ted.com/talks/lang/en/hans_rosling_the_best_stats_you_ve_ever_seen), he and his team at
Gapminder [@Gapminder] had developed animated bubble charts, aka
motion charts. 

Rosling's [presentation](https://embed.ted.com/talks/lang/en/hans_rosling_the_best_stats_you_ve_ever_seen) popularised the idea and use of interactive
charts. One year later the software behind
Gapminder was bought by Google and integrated as motion charts into
their Google Charts API [@GoogleVisApi], formerly known as Google
Visualisation API.  

In 2010  Sebastián Pérez Saaibi [@Saaibi2010] presented at the 
R/Rmetrics Workshop on  Computational Finance and Financial
Engineering, the idea to use Google motion charts to visualise R
output with the `r CRANpkg("R.rsp")` package [@Rsp]. 

Inspired by those talks and the desire to use interactive data
visualisation tools to foster the dialogue between data analysts and
others the authors of this vignette started the development of the
`r CRANpkg("googleVis")` package 
[@RJournal:2011-2:GesmannCastillo] in August 2010. 

### Google Chart Tools

The Google Charts API [@GoogleVisApi]
allows users to create interactive charts as part of Google
documents, spreadsheets and web pages. In this text, we will focus 
on the usage of the API as part of web pages.

The Google Public Data Explorer [@GooglePublicData] provides a
good example, demonstrating the use of interactive charts and how they can
help to analyse data.

The charting data can either be embedded into the HTML file or read
dynamically. The key to the Google Charts is that the data is
structured in a DataTable [@DataTable], and this is where the googleVis
package helps, as it transforms R data frames into JSON [@json]
objects, using the `r CRANpkg("jsonlite")` package [@jsonlite], as
the basis for a DataTable. 

As an example we shall look at the html-code of a motion chart from
Google's visualisation gallery [@GoogleMotionChart].

```
 1 <html>
 2  <head>
 3   <script type="text/javascript" 
 4     src="https://www.google.com/jsapi">
 5   </script>
 6   <script type="text/javascript">
 7     google.load('visualization', '1', 
 8      {'packages':['motionchart']});
 9    google.setOnLoadCallback(drawChart);
10    function drawChart() {
11    var data=new google.visualization.DataTable();
12    data.addColumn('string', 'Fruit');
13    data.addColumn('date', 'Date');
14    data.addColumn('number', 'Sales');
15    data.addColumn('number', 'Expenses');
16    data.addColumn('string', 'Location');
17    data.addRows([
18    ['Apples',new Date(1988,0,1),1000,300,'East'],
19    ['Oranges',new Date(1988,0,1),1150,200,'West'],
20    ['Bananas',new Date(1988,0,1),300,250,'West'],
21    ['Apples',new Date(1989,6,1),1200,400,'East'],
22    ['Oranges',new Date(1989,6,1),750,150,'West'],
23    ['Bananas',new Date(1989,6,1),788,617,'West']
24    ]);
25    var chart=new google.visualization.MotionChart(
26      document.getElementById('chart_div'));
27    chart.draw(data, {width: 600, height:300});
28   }
29   </script>
30  </head>
31  <body>
32   <div id="chart_div" 
33        style="width:600px; height:300px;">
34   </div>
35  </body>
36 </html>
```

The code and data are processed and rendered by the browser and is
not submitted to any server^[[Data Policy](https://developers.google.com/chart/interactive/docs/gallery/linechart#data-policy)]. 

You will notice that the above HTML code has five generic
parts:

-  references to Google's AJAX (l. 4) and Visualisation
  API (ll. 7 - 8),
-  data to visualise as a `DataTable` (ll. 11 - 24),
-  an instance call to create the chart (ll. 25 - 26),
-  a method call to draw the chart including options, shown
  here as width and height (l. 27),
-  an HTML `<div>` element to add the chart to the page (ll.
  32 -- 34).

These principles hold true for most of the interactive charts of the
Google Chart Tools, see the examples in the next section.

However, before you use the API you should read the [Google Terms of Service](https://developers.google.com/terms/) [@GoogleTerms]. 

# The googleVis package 

The googleVis package provides an interface between R and the Google
Chart Tools. The functions of the package allow the user to
visualise data stored in R data frames with Google Charts.

The output of a googleVis function is HTML code that contains the
data and references to JavaScript functions hosted by Google. A
browser with an Internet connection is required to view
the output, and for a very few chart types, notably motion charts, also Flash. 
Examples of several chart types are shown below, which have been combined with 
the `gvisMerge` function.

```{r demos, eval=TRUE, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
## Code for screen shot
library(googleVis)
df <- data.frame(country=c("A", "B", "C"), 
                 val1=c(1,3,4), val2=c(23,12,32))

Line <- gvisLineChart(df, xvar="country", yvar=c("val1", "val2"),
                      options = list(
                        width=300, height=300,
                        legend = "top",
                        title="Hello World",
                        titleTextStyle = "{color:'red',fontName:'Courier',
            fontSize:16}",
            curveType='function'))

ATL <- gvisAnnotationChart(Stock, datevar="Date",
                           numvar="Value", idvar="Device",
                           titlevar="Title",
                           annotationvar="Annotation",
                           options=list(displayAnnotations=TRUE,
                            legendPosition='newRow',
                            width=300, height=300)
                           )
                          
Gauge <- gvisGauge(CityPopularity, 
                 options=list(min=0, max=800, greenFrom=500,
                              greenTo=800, yellowFrom=300, 
                              yellowTo=500,
                              redFrom=0, redTo=300,
                              width=300, height=200))


Geo <-  gvisGeoChart(Exports, locationvar='Country',
                     colorvar='Profit',
                   options=list(projection="kavrayskiy-vii", 
                                width=300,height=200)) 


# Datetime example 
dat <- data.frame(Room=c("Room 1","Room 2","Room 3"),
                  Language=c("English", "German", "French"),
                  start=as.POSIXct(c("2014-03-14 14:00", "2014-03-14 15:00", 
                                     "2014-03-14 14:30")),
                 end=as.POSIXct(c("2014-03-14 15:00", "2014-03-14 16:00", 
                                  "2014-03-14 15:30")))
TL <- gvisTimeline(data=dat, rowlabel="Language",
                   start="start", end="end",
                   options=list(width=300, height=200))
Tree <- gvisTreeMap(Regions,  "Region", "Parent", "Val", "Fac",
                    options=list(width=300, height=200,
                                 fontSize=16,
                                 minColor='#EDF8FB',
                                 midColor='#66C2A4',
                                 maxColor='#006D2C',
                                 headerHeight=20,
                                 fontColor='black',
                                 showScale=TRUE))

M <- gvisMerge(
  gvisMerge(gvisMerge(Line, ATL, horizontal = TRUE,
                      tableOptions="cellspacing=10"), 
            gvisMerge(Gauge, Geo, horizontal =TRUE, 
                      tableOptions="cellspacing=10")), 
  gvisMerge(TL, Tree, horizontal = TRUE, 
            tableOptions="cellspacing=10"))
plot(M)
``` 

## Installation{#sec:Installation}

You can install googleVis in the usual way from CRAN, e.g.:

```{r eval=FALSE}
install.packages('googleVis') 
```
The installation was successful if the
command `library(googleVis)` gives you the following message:

```{r echo=FALSE, quite=TRUE}
library(googleVis)
``` 
```{r eval=FALSE}
library(googleVis)
``` 
```{r echo=FALSE}
cat(googleVis:::gvisWelcomeMessage())
``` 

# Concepts of the googleVis package 

The individual functions of the googleVis package are documented in
the help pages. Here we will cover only the basic concepts of
the package.

As an example, we will show how to generate a geo chart. It works 
similarly for the other APIs. Further examples are covered in the demos^[See `demo(package="googleVis")` for a list of the available demos] of the googleVis
package.

The design of the visualisation functions is fairly generic. The name
of the visualisation function is  `'gvis' + ChartType`. So for
a geo chart we have: 

```{r GeoChart, eval = FALSE}
gchart <-  gvisGeoChart(data,
                     locationvar = "",
                     colorvar = "",
                     sizevar = "",
                     hovervar = "",
                     options = list(),
                     chartid)                     
```

Here `data` is the input `data.frame`, `locationvar`, 
`colorvar`, `sizevar` and `hovervar` specify the various columns 
used for the plot. Display options are set in an
optional list, which we discuss in more detail later. 
The options and data requirements
follow those of the Google Charts API  and are documented in the
help pages, see 

```{r eval=FALSE}
help('gvisGeoChart')
``` 

The argument `chartid` allows the user to set a chart ID of the
output chart manually. If the argument is missing a random ID using 
`tempfile(pattern='')`
will be generated. Unique chart  IDs are required to place more than
one chart on a web page. 

The output of a googleVis function is a list of lists (a nested list)
containing information about the chart type, chart ID and the HTML
code in a  sub-list with header, chart, caption and footer.

The idea behind this concept is that users can get a
complete web page, while at the same time offer a facility to extract 
specific parts, such as the chart itself. 
This is particularly helpful if the package functions
are used in solutions where the user wants to feed the visualisation
output into other sites.  

The output of a googleVis function will be of class `gvis`
and `list`. Generic print (`print.gvis`) and plot
(`plot.gvis`) functions exist to ease the handling of such objects. 

To illustrate the concept we shall create a Geo chart using the
`Exports` data set. 

## Geo Chart Example

Following the documentation of the Google Geo Chart API we need a
data set which has at least one column with the location
variable.

As an example we use the `Exports` data set:

```{r }
data(Exports)
Exports
```

Here we will use the columns `'Country'` and
`'Profit'` as location and colour variable respectively. 

```{r }
 gchart <-  gvisGeoChart(data = Exports, 
                      locationvar='Country',
                      colorvar='Profit',
                      options=list(projection="kavrayskiy-vii", 
                                   width=400, height=200))
```

The structural output of `gvisGeoChart` is a list of lists as
described below. 

```{r eval=FALSE}
 str(gchart)
``` 

```{r echo=FALSE}
## This statement avoids truncation
cat(paste(substring( capture.output( str(gchart) ) , 0, 66), sep="\n", collapse="\n"))
``` 

The first two items of the list contain information about the chart type
used and the individual chart ID: 

```{r }
gchart$type
gchart$chartid
``` 

The html output is a list with header, chart, caption and
footer. This allows the user to extract only certain parts
of the page, or to create a complete html page. 

The header part of the html page has only basic html and formatting tags: 

```{r }
print(gchart, tag='header')
``` 

Here we used the `print` statement with the tag
`'header'` instead of `gchart$html$header` to achieve
a formatted screen output. This is the same output as
`cat(gchart$html$chart)`. 

The actual Google visualisation code is stored with the data
as a named character vector in the `chart` item of the HTML
list. The chart is made up of several JavaScript and  HTML
statements. Please notice that the JavaScript functions are
uniquely named with the information of the chart  ID.
This concept allows the user get all the chart code directly or only
specific parts; see the examples in the help page of `print.gvis`
for more details.

```{r }
names(gchart$html$chart)
``` 

The complete chart can be displayed via:

```{r }
print(gchart, tag='chart')  ## or cat(gchart$html$chart)
```

Similarly you can also access specific components of the chart, e.g. (output truncated)

```{r eval=FALSE}
cat(gchart$html$chart['jsChart']) # or print(gchart, 'jsChart')
``` 

```{r echo=FALSE}
cat(paste(substring( capture.output( cat(gchart$html$chart['jsChart']) ) , 0, 66), sep="\n", collapse="\n"))
``` 

A basic chart caption and html footer are the final items of the html
list (output  truncated):

```{r eval=FALSE}
print(gchart, tag='caption')
``` 
```{r echo=FALSE}
cat(paste(substring( capture.output( cat(gchart$html$caption) ) , 0, 66), sep="\n", collapse="\n"))
``` 

```{r eval=FALSE}
print(gchart, tag='footer')
``` 
```{r echo=FALSE}
cat(paste(substring( capture.output( cat(gchart$html$footer) ) , 0, 66), sep="\n", collapse="\n"))
``` 

# Displaying gvis objects locally

To display the page locally, simply type:

```{r displaygchart, results='asis'}
plot(gchart)  # returns invisibly the file name
```


The plot method for `gvis`-objects creates HTML files in a temporary 
folder using the type and chart ID information of the object and it will display the output using the R built-in web server.   

Note that the chart caption provides a link to the chart code via
the chart ID for copy and paste.

The R command `tempdir()`  will show you the path of the
per-session temporary directory, in which the files were written.
You can write the chart into a local html file via the `print`
command with the file argument, e.g.

```{r eval=FALSE}
print(gchart, file="myGoogleVisChart.html")
``` 


Alternatively use the function `plot.gvis` explicitly,
e.g. suppose your html file is stored in
`/Users/JoeBloggs/myGoogleVisChart.html`. Using the
`plot.gvis` the file will be copied into a temporary directory
and displayed via the R HTTP help server with, in the same way as a
gvis-object: 

```{r eval=FALSE}
plot.gvis("/Users/JoeBloggs/myGoogleVisChart.html")
``` 


# Setting options

Setting the various options of a googleVis objects can be a bit
cumbersome at first. The options follow those of the Google
Charts API and can be set via a named list using the argument `options`.
In the following example, we create a line chart and set various
options:  

```{r LineChartWithOptions, results="asis"}
df <- data.frame(country=c("US", "GB", "BR"), 
                 val1=c(1,3,4), val2=c(23,12,32))

Line <-  gvisLineChart(df, xvar="country", yvar=c("val1","val2"),
                       options=list(
                         title="Hello World",
                         titleTextStyle="{color:'red', 
                                           fontName:'Courier', 
                                           fontSize:16}",                         
                         backgroundColor="#D3D3D3",                          
                         vAxis="{gridlines:{color:'red', count:3}}",
                         hAxis="{title:'Country', titleTextStyle:{color:'blue'}}",
                         series="[{color:'green', targetAxisIndex: 0},	
                                   {color: 'orange',targetAxisIndex:1}]",
                         vAxes="[{title:'val1'}, {title:'val2'}]",
                         legend="bottom",
                         curveType="function",
                         width=500,
                         height=300                         
                       ))
plot(Line)
```

As you can see from the example above, the simpler options can be set by `name=value`, e.g. `width=500`, while the more complex options with sub-components are
listed in curly brackets `{}` and arrays `[]`, e.g. to define the two
axes.

Generally, the following rules apply:


 - parameters with names that do not include a "." are set with a single 
 value, e.g. width and height. Those are set like one would do in R, that is
 `options=list(width=200, height=300)`. Boolean arguments are set to 
 either `TRUE` or `FALSE`, using the R syntax.
 - parameters with names that do not include a "." and are set with 
 multiple values, e.g. `color`, and are wrapped in `[ ]`, e.g. 
 `options=list(colors="['#cbb69d', '#603913', '#c69c6e']")`
 - parameters with names that do include a "." present parameters with 
 several sub-options and have to be set as a string wrapped in "\{ \}".
 The values of those sub-options are set via `parameter:value`. Boolean values
 have to be stated as `'true'` or `'false'`. 
 For example the Google documentation states the formatting options for the 
 vertical axis as `vAxis.format`. 
 Then this parameter can be set in R as:
 `options=list(vAxis="{format:'#,###%'}")`. 
 - If several sub-options have to be set, e.g. 
`titleTextStyle.color`, `titleTextStyle.fontName` and 
`titleTextStyle.fontSize`, then those can be combined in one list item 
such as: `options=list(titleTextStyle="{color:'red',fontName:'Courier', fontSize:16}")`
 - parameters that can have more than one value per sub-options are 
wrapped in `[ ]`. For example to set the labels for left and right axes use:
`options=list(vAxes="[{title:'val1'}, {title:'val2'}]")`


## Chart Editor

A special option for all charts is `gvis.editor`, which adds an
edit  button to the page, allowing the user to edit, change and
customise the chart on the fly. The content of the option `gvis.editor`
describes the label of the browser button.    
  
```{r chartEdit, results='asis'}
Editor <- gvisLineChart(df, options=list(gvis.editor='Edit me!'))
plot(Editor)
``` 




# Embedding googleVis in web sites

Suppose you have an existing web page and would like to integrate the
output of a googleVis function, such as `gvisLineChart`. 
In this case you only need the chart output from
`gvisLineChart`.  So you can either copy and paste the output
from the R console

```{r eval=FALSE}
print(gchart, 'chart')  ## or cat(gchart$html$chart) 
``` 

into your existing html page, or write the content directly into a file

```{r eval=FALSE}
print(gchart, 'chart', file='myfilename')
```

and process it from there.


# Using googleVis with shiny

[Shiny](https://shiny.posit.co/) is a package by
RStudio, which makes it incredibly easy to build interactive web
applications with R.  

With version 0.4.0 of googleVis support for shiny
apps was added. Joe Cheng contributed the `renderGvis`
function which allows users to use googleVis output in shiny in a
similar way to other plotting functions. 

The following example has been taken from the help file of
`renderGvis`. It displays a scatter chart where the user can
select the data set to be displayed. 

```{r eval=FALSE}
# server.R
library(googleVis)

shinyServer(function(input, output) {
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  
  output$view <- renderGvis({
    gvisScatterChart(datasetInput())
  })
})

# ui.R
shinyUI(pageWithSidebar(
  headerPanel("googleVis on Shiny"),
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "pressure", "cars"))
  ),
  mainPanel(
    htmlOutput("view")
  )
))
``` 

You can run the example locally with the following statement.

```{r eval=FALSE}
library(shiny)
runApp(system.file("shiny/", package="googleVis"))
``` 


# Setting default behaviour of `print.gvis` and `plot.gvis` 

In googleVis version 0.3.2 the function `plot.gvis` gained the
same argument as `print.gvis`: `tag`. By default the
`tag` argument is set to `NULL` in `plot.gvis`
and the plot function will display its output in a browser
window. However, if `tag` is not `NULL` the function
`plot.gvis` will behave exactly like `print.gvis`.

The default `tag` can be set for both functions globally via
the `options()` function. On package load googleVis sets
`options(gvis.print.tag='html')` and
`options(gvis.plot.tag=NULL)`. 

Suppose you would set `options(gvis.plot.tag='chart')` then all
following plot statements would print the chart part of the
gvis-object only, without opening a browser window. This might
seem a bit odd at first, yet it becomes helpful when you write R
Markdown files for `knitr` or files for other packages such as
`R.rsp`. 

While you draft your file you may want to see the output of googleVis
in an interactive way, so you set `options(gvis.plot.tag=NULL)`
at the top of the file and you change the setting to `'chart'`
before you parse the file, say with `knitr`. This will
ensure that all plot statements return the HTML code of the chart,
rather than opening browser windows. 


# Using googleVis with knitr

Using googleVis with [knitr](https://yihui.org/knitr/) [@knitr] is a
convenient way of creating interactive reproducible reports. The
approach taken by knitr is similar to Sweave, you can combine R
code with text and formatting tags. However, knitr can also export to
HTML, which is required to  embed googleVis charts.

To include googleVis output into a knitr document you have to set 
the `self_contained` option to `false` in the YAML header:

```
---
title: "My document"
output:
  html_document:
    self_contained: false
---
```

Furthermore, the chunk option `results` should be set to `'asis'`, 
so that the html output is written into the markdown file.

You can either use the `print` statement:

```{r echo=FALSE, class.output="r", comment=""}
cat(paste(sep = "\n",
   "```{r results='asis'}",
   "gchart <- gvisColumnChart(CityPopularity, 'City', 'Popularity',",
   "                        options=list(width=550, height=450,",
   "                                     legend='none'))",
   "print(gchart, 'chart')",
   "```"
))
```

or alternative change the behaviour of the `plot` function via setting
`options(gvis.plot.tag = 'chart')`. With this setting `plot(x)` will no longer open a browser window, but produce the same output as 
`print(x, tag='chart')`, if `x` is a gvis-object. 

Hence, setting the option `gvis.plot.tag` in a knitr markdown
Rmd-file to `'chart'` will automatically turn all following
plot statements into html output. 

Note that you can use the `options()` command in your knitr
file to switch between an interactive mode, where you are likely to
experiment, via copying and pasting R code into the console and
running `knit` on the whole file.

A more comprehensive example is given in the help file to `?plot.gvis`.

# Using googleVis in presentations

The Google Chart Tools are designed for web pages, so it should
be no surprise that it can be difficult or impossible to embed googleVis 
output in traditional presentation software like MS PowerPoint, Google Docs,
OpenOffice Impress or Apple Keynote. 

The easiest way is to include screen shots into the slide with links to
the live web pages. But this approach requires the presenter to switch
between applications during her talk. This can be fun, but quite often
it is not. 

An alternative would be to build the presentation as a web page itself.  

A popular approach here is the `slidify` package by Ramnath Vaidyanathan, 
[@slidify] that builds on the knitr Markdown approach of the previous 
section. An example of a `slidify` presentation is the [googleVis 
tutorial](https://decastillo.github.io/googleVis_Tutorial/) given at the useR! conference in 2013, @googleVisTutorial.


# Using Trendlines with googleVis

A trendline is a line superimposed on a chart revealing the overall 
direction of the data. Google Charts can automatically generate 
trendlines for Scatter Charts, Bar Charts, Column Charts and Line Charts.

For more details visit: 
https://developers.google.com/chart/interactive/docs/gallery/trendlines

```{r readTrendLines, message=FALSE, echo=FALSE}
read_demo('Trendlines', 'googleVis')
```

## Linear trend line
```{r LinearTrend, results='asis', tidy=FALSE}
```

## Exponential trend line with equation shown in legend
```{r ExponentialTrend, results='asis', tidy=FALSE}
```

## Add trend line to column chart
```{r ColumnChartWithTrendline, results='asis', tidy=FALSE}
```

## Changing labels in legend
```{r DifferentLabels, results='asis', tidy=FALSE}
```


# Using Roles via googleVis

Roles add the ability to pass columns for further processing downstream. 
Role columns must follow column they pertain to. Proper naming
conventions must be observed. For example, roles fulfilling tooltip
roles and must be called "foo.blah.tooltip". For more details see the [Google documentation](https://developers.google.com/chart/interactive/docs/roles).

The following examples should help to illustrate the concept. 

```{r readRoles, message=FALSE, echo=FALSE}
read_demo('Roles', 'googleVis')
```
The first example uses a data set that has the additional column
`pop.html.tooltip` with the first 11 letters of the Latin alphabet.
This column is mapped automatically as a tooltip when the user hovers 
over the chart point.
```{r Tooltip, results='asis', tidy=FALSE}
```
HTML code can be embedded into the tooltip as well, if the option 
`isHtml` is set to true.

Often it is helpful to highlight certain parts of the data. The Google API
distinguishes between certainty and emphasis.
In a similar way to above additional columns with boolean values
have to be added to the data.
```{r CertaintyScopeEmphasis, results='asis', tidy=FALSE}
```

Using roles with column or bar charts has some specifics.
Instead of 'emphasize' use 'style' to change the colours.
```{r ColumnChart, results='asis', tidy=FALSE}
```


```{r TwoLines, results='asis', tidy=FALSE}
```

Setting the annotations style to 'line' allows adding little reference lines
to the plot.
```{r VerticalReferenceLine, results='asis', tidy=FALSE}
```

Intervals help to add error bars, confidence levels, etc.
Note that the options are set either via `interval` or `intervals`, 
if set to all intervals. The examples below give an indication of what 
can be achieved with intervals. 
For more details visit the Google [documentation](https://developers.google.com/chart/interactive/docs/gallery/intervals).

```{r Interval, results='asis', tidy=FALSE}
```

The tool tips in geo charts need a little work around, a `Tooltip.header` 
variable has to be set to an empty string:

```{r GeoChartTooltip, results='asis', tidy=FALSE}
```


# Beyond R

In this section we present ideas which go beyond the usual coding in
R and are somewhat experimental.

## Registering to catch events

Google visualisations can fire and receive
[events](https://developers.google.com/chart/interactive/docs/events).
It exposes the following two JavaScript methods: 

 - `google.visualization.events.trigger()` fires an event,
 - `google.visualization.events.addListener()` listens for events.

Here is an example of registering to receive the selection event from
the Google documentation:

```
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, options);
google.visualization.events.addListener(table, 'select', selectHandler);

function selectHandler() {
  alert('A table row was selected');
}
```

We will only deal with this special case of a 'select' event of the
'addListner' method. This event is available for most visualisations
and acts on user interactions, e.g. user selection clicks.

The 'addListener' method expects JavaScript code, which can be embedded
into a gvis-object via `options` as (undocumented) parameter
`gvis.listener.jscode`. 

Here are some examples:

Look up the selected item in Wikipedia:

```{r listener, results="asis"}
jscode <- "window.open('https://en.wikipedia.org/wiki/' 
                  + data.getValue(chart.getSelection()[0].row,0)); "

J1 <- gvisGeoChart(Exports, locationvar='Country', sizevar = 'Profit',
                 options=list(dataMode="regions", 
                              gvis.listener.jscode=jscode))
plot(J1)
``` 

In the same way we can use the code in other charts, e.g. org- or line charts:

```{r eval=FALSE}
plot(gvisOrgChart(Regions,  options=list(gvis.listener.jscode=jscode)))
plot(gvisLineChart(Regions[,c(1,3)], options=list(gvis.listener.jscode=jscode)))
```

In the following more advanced example the selected value of a table is
displayed in a message box:

```{r message, results="asis"}
jscode <- "
       var sel = chart.getSelection();
       var row = sel[0].row;
       var text = data.getValue(row,1);
       alert(text);
"
J2 <- gvisTable(Population[1:5,], 
                options=list(gvis.listener.jscode=jscode))
plot(J2)
``` 

For more details see the `demo(EventListener)` and
[Google Charts Reference](https://developers.google.com/chart/interactive/docs/reference).

# Frequent ask questions -- FAQ

## Is there an alternative for the Flash based motion chart

Other R packages provide alternatives to the Flash based
Google motion chart, such as [plotly](https://plotly.com/r/animations/#mulitple-trace-animations), 
[gganimate](https://gganimate.com)


## Can I use googleVis output in PDF files?

No, not directly. The Google Charts API is designed for dynamic web
output on your screen and not on paper.

For further details see Google's online documentation on
[printing PNG charts](https://developers.google.com/chart/interactive/docs/printing).

## Can I change the colour of the bubbles in motion charts?

No, unfortunately not. The colours are set by the Google Charts API and
cannot be changed by the user.

## Why can't I see motion charts when I open them from a local directory?

Note that Flash charts may not work when loaded as a local file due to
security settings, and therefore require to be displayed via a web
server. However, you can overcome this issue by changing your Flash
security settings. Tony Breyal posted the following solution on
stackoverflow.com:

 1. Go to https://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html
 2. Click on the dropbox which says ’Edit location’ and choose ’add location’
 3. Click ’browse for folder’
 4. Choose the folder in which you saved your HTML file
 5. Click OK

## Why can't I see motion charts on my iPad/iPhone?

Motion charts (also geo maps and annotated time lines) are rendered in
your browser using Flash, unlike most other charts which use
HTML5. Unfortunately, Flash is not directly supported on iOS devices
such as iPads and iPhones. 

## How can I set axes limits with googleVis?

Unfortunately, there are no arguments such as `ylim` and `xlim`. 
Instead, the Google Charts axes options are set via `hAxes` and
`vAxes`, with 'h' and 'v' indicating the horizontal and vertical axis. 
More precisely, we have to set `viewWindowMode:'explicit'` and set the `viewWindow` to the desired `min` and `max` values. Additionally, we 
have to wrap all of this in `[{}]` brackets as those settings are sub 
options of `h/vAxes`. There are also options `minValue` and 
`maxValue`, but they only allow you to extend the axes ranges.

Here is a minimal example, setting the y-axis limits from 0 to 10:

```{r setlimit, results="asis"}
dat <- data.frame(x=LETTERS[1:10], 
                  y=c(0, 4, -2, 2, 4, 3, 8, 15, 10, 4))
area1 <- gvisAreaChart(xvar="x", yvar="y", data=dat,
    	options=list(vAxes="[{viewWindowMode:'explicit',
			viewWindow:{min:0, max:10}}]",
                        width=500, height=400, 
                        title="y-limits set from 0 to 10"),
			chartid="area1ylim")
plot(area1)
```



## How do I deal with apostrophes in column names?

The googleVis package converts data frames into JSON objects.
The column names of the resulting JSON tables are encapsulated with
single speech marks. 

Hence apostrophes in column names of your input data frame have
to be encapsulated by a double backslash.

Here is a little example:

```{r columnchart, results='asis'}
df <- data.frame("Year"=c('2009', '2010'), 
                 "Alice\\'s salary"=c(86.1, 93.3), 
                 "Bob\\'s salary"=c(95.3, 100.5),
                 check.names=FALSE) 
gchart <- gvisColumnChart(df, options=list(vAxis='{baseline:0}', 
                            title="Salary",
                            legend="{position:'bottom'}"))
plot(gchart)
```


## How can I change the look and feel of the charts?

The charts have a lot of options which allow you to change the look
and feel of the output, see the help files for more details. However,
googleVis provides only an interface to the Google Charts API. If
you have specific questions to the charts then please ask the [Google
Visualisation API newsgroup](https://groups.google.com/d/forum/google-visualization-api).

For frequent ask questions regarding the API check: 
https://developers.google.com/chart/interactive/faq.

## Is it possible to use googleVis in corporate work?

Review the [Google Terms of Service](https://developers.google.com/terms/) 
and get in touch with your colleagues in IT / Legal. If in doubt contact Google
directly. 



# Bugs and issues

Should you find any issues or bugs with googleVis, then please drop
us a line or add them to our issues list: 
(https://github.com/mages/googleVis/issues)


# Citation

Please cite googleVis if you use it in your work or publications:

```{r citation}
citation("googleVis")
```

# References

\bibliographystyle{alpha}
\bibliography{googleVis}