5

Google has some nifty charts. I'm sure there's nice R wrapper packages to do what I want but I want to learn how to include a google chart myself as it will likely be more flexible than an R wrapper for including a google chart.

Below is an .Rmd that has a donut function to produce the donut from Google chart gallery. The function outputs a code as expected seen below but to actually knit this results in the following pandoc conversion error. How do I include the google chart code properly in an Rmarkdown .Rmd file?

Error

output file: ttt.knit.md

pandoc.exe: Could not fetch https://www.gstatic.com/charts/loader.js
HttpExceptionRequest Request {
  host                 = "www.gstatic.com"
  port                 = 443
  secure               = True
  requestHeaders       = []
  path                 = "/charts/loader.js"
  queryString          = ""
  method               = "GET"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
}
 (InternalException (HandshakeFailed Error_EOF))
Error: pandoc document conversion failed with error 67
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS ttt.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output ttt.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\R\R-3.3.2\library\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\trinker\AppData\Local\Temp\RtmpWI5M7o\rmarkdown-str5910399322d6.html" --mathjax --variable "mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"' had status 67 
Execution halted

Rmd MWE

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

donut <- function(){

    out <- c("<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>", 
    "<script type=\"text/javascript\">", "  google.charts.load(\"current\", {packages:[\"corechart\"]});", 
    "  google.charts.setOnLoadCallback(drawChart);", "  function drawChart() {", 
    "var data = google.visualization.arrayToDataTable([", "  ['Task', 'Hours per Day'],", 
    "  ['Work',     11],", "  ['Eat',      2],", "  ['Commute',  2],", 
    "  ['Watch TV', 2],", "  ['Sleep',    7]", "]);", "", "var options = {", 
    "  title: 'My Daily Activities',", "  pieHole: 0.4,", "};", "", 
    "var chart = new google.visualization.PieChart(document.getElementById('donutchart'));", 
    "chart.draw(data, options);", "  }", "</script>", "", "<div id=\"donutchart\" style=\"width: 900px; height: 500px;\"></div>"
    )

    cat(paste(out, collapse = "\n"))

}

```


```{r results='asis'}
donut()
```

donut output sans rmarkdown knitting

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load("current", {packages:["corechart"]});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
var data = google.visualization.arrayToDataTable([
  ['Task', 'Hours per Day'],
  ['Work',     11],
  ['Eat',      2],
  ['Commute',  2],
  ['Watch TV', 2],
  ['Sleep',    7]
]);

var options = {
  title: 'My Daily Activities',
  pieHole: 0.4,
};

var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
  }
</script>

<div id="donutchart" style="width: 900px; height: 500px;"></div>
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • the knit works fine on my system (macOS Sierra, R 3.4.0, rstudio daily) – hrbrmstr May 23 '17 at 13:51
  • @hrbrmstr I am Windows 10. using R v. 3.4.0 with Rstudio v. 1.0.143 with current **rmarkdown** and pandoc v. 1.17.2 I still get the same error – Tyler Rinker May 23 '17 at 14:29
  • Hmm it must be something to do with connectivity. If I use `self_contained: no` as seen here: https://stackoverflow.com/a/27492347/1000343 and then knit... it will knit but I have to open the RStudio pane in the browser – Tyler Rinker May 23 '17 at 14:35
  • I'm devel rmarkdown as I submitted a PR into it. pandoc is 1.19.2.1 – hrbrmstr May 23 '17 at 17:00
  • @hrbrmstr Odd I updated pandoc to 1.19.2.1 and still get the issue though the error looks slightly different. I'll give a test on the same machine on a different network – Tyler Rinker May 23 '17 at 17:08
  • The same error `(InternalException (HandshakeFailed Error_EOF))` occurred in this question posted yesterday : https://stackoverflow.com/questions/44120395/error-when-knitting-github-document-pandoc-document-conversion-failed-with-erro – scoa May 23 '17 at 20:19
  • Pandoc > 2.1 is out and seems to resolve this issue. – SymbolixAU Feb 20 '18 at 22:55

1 Answers1

6

This sounds suspiciously like the bug the hs-tls library had recently, which pandoc depends on to download images etc. over https.

Most likely, using a more recent pandoc nightly (or compiling from source) and making sure it uses tls >= 1.3.9 will fix your problem.

mb21
  • 34,845
  • 8
  • 116
  • 142