After researching the error I am still not able to fix it. However, I have a workaround that allows one to inject arbitrary json data into an html report produced with R markdown without going through pandoc at all; the latter does not seem to like the injection of a lot of json using e.g. the method described in
http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/
since this 137 error appears to me to be related to pandoc killing the conversion process to html as it takes to long to terminate.
The workaround is pretty simple: rather than injecting the json data during the knitr compilation step by including it in a chunk with the 'asis' option (as described in the above link), it is better to append the json data at the end of the in the html file that is produced by the compilation through knitr and pandoc. In other words, inject the json data in the output html file of the kitr + pandoc steps.
Assuming that the rmd file to be compiled into html resides in an installed package, and following the suggestions at
Best practice for embedding arbitrary JSON in the DOM?
for embedding json data in the DOM, one can achieve this goal using the xml2 package with a function such as:
create_report <- function(file,
subdir,
package,
parameters,
output_file,
data_arr = NULL){
#check file exists
if (system.file(package = package, subdir, file) == ''){
stop('Cannot find the .rmd file')
}
#first generate report
address <- rmarkdown::render(system.file(package = package,
subdir,
file),
output_dir = getwd(),
intermediates_dir = getwd(),
output_file = output_file,
params = parameters,
clean = FALSE)
#then append the data in the json files located in the named list
#data_arr directly into the
#html after compilation if needed:
if (!is.null(data_arr)){
report <- xml2::read_html(address)
report_body <- xml2::xml_find_first(report, "body")
#adding the data
for (i in 1:length(data_arr)){
xml2::xml_add_child(report_body, "script", type = 'application/json', id =
names(data_arr[i]), data_arr[[i]])
}
#Then add a script that takes the data from the script tags added above and
#initializes the variables.
varnames <- paste0("var ", names(data_arr), " =
JSON.parse(document.getElementById('", names(data_arr), "').innerHTML);",
collapse = " ")
xml2::xml_add_child(report_body, "script", type = 'text/javascript', id =
'external_json_init', varnames)
xml2::write_html(report, address)
}
return(address)
}
In the above function, data_arr is supposed to be a named list whose elements are json strings obtained, e.g., by converting R objects using jsonlite::toJSON, and whose names are the variable names to be used to store the data in the javascript layer.
In this manner, arbitrary sized json can be injected into an R markdown generated html, to be manipulated by javascript.