4

Outside of Rmarkdown the stand alone googleVis chart works fine, but when I plug it in the Rmarkdown file I am receiving just the Rmarkdown Code:

Viewer Output:

enter image description here

> TEST H 4/13/2016 require(googleVis) Loading required package:
> googleVis Welcome to googleVis version 0.5.10 Please read the Google
> API Terms of Use before you start using the package:
> https://developers.google.com/terms/
> 
> Note, the plot method of googleVis will by default use the standard
> browser to display its output.   See the googleVis package vignettes
> for more details, or visit http://github.com/mages/googleVis.   To
> suppress this message use:
> suppressPackageStartupMessages(library(googleVis))
> 
> dttm = data.frame(DT_ENTRY=Sys.Date()-1:20,variable="x",value=1:20)
> g1=gvisAnnotationChart(dttm,datevar="DT_ENTRY",numvar="value",idvar="variable")
> plot(g1) starting httpd help server ... done

Rmarkdown Code Below:

---
title: "test"
author: "H"
date: "4/13/2016"
output: html_document
highlight: tango
number_sections: yes
---

```{r}
require(googleVis)
dttm = data.frame(DT_ENTRY=Sys.Date()-1:20,variable="x",value=1:20)
g1=gvisAnnotationChart(dttm,datevar="DT_ENTRY",numvar="value",idvar="variable")
plot(g1)
```
zx8754
  • 52,746
  • 12
  • 114
  • 209
Shaka
  • 41
  • 4

2 Answers2

2

The r chunk has to be declared as:

```{r plotg0,  results='asis', tidy=FALSE, echo=FALSE}

The "asis" is important because it returns raw HTML

Chocksmith
  • 1,188
  • 2
  • 12
  • 40
1

I am not sure if you still need an answer. However, I faced the same problem while trying to embed a sankey plot from gvisSankey onto a section on Rmarkdown. Thankfully, I found the solution on github.com/mages/googleVis (weirdly not recommended on Google Search).

Below is a reproducible example.

First, to create a sankey plot, I do this:


    # install.packages("googleVis")
    library(googleVis)
    
    # Create a data.frame that is gvisSankey-conform
    # i.e., has these columns: FROM - TO - WEIGHT
    df <- data.frame(FROM = rep(c("A","B","C"), each = 3),
                     TO = rep(c("D","E","F"), times = 3),
                     WEIGHT = sample(1:10, 9, replace = TRUE))
    
    sankeyplot <- gvisSankey(data = df,
                             from = "FROM",
                             to = "TO",
                             weight = "WEIGHT")
    
    plot(sankeyplot)

plot(sankeyplot) will open a new tab containing the sankeyplot. Now, how do I embed the sankeyplot onto my Rmarkdown?

There are two important points:

  • To set self_contained:false in the YAML header
  • To set results='asis' in the R code chunk where the sankeyplot is embedded
    ---
    title: "SankeyPlot_Example"
    output: 
      html_document:
        self_contained: false
    date: '2022-10-21'
    ---
    
    ## Embed googleVis object (sankey plot) onto Rmarkdown
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(googleVis)
    
    # Create a reproducible data.frame
    df <- data.frame(FROM = rep(c("A","B","C"), each = 3),
                     TO = rep(c("D","E","F"), times = 3),
                     WEIGHT = sample(1:10, 9, replace = TRUE))
    ```
    
    ```{r results='asis'}
    sankeyplot <- gvisSankey(data = df,
                             from = "FROM",
                             to = "TO",
                             weight = "WEIGHT")
    
    print(sankeyplot, 'chart')
    ```
Kpamungkas
  • 41
  • 4