通过 knitr + pandoc R 降价到 html:错误 137
R markdown to html via knitr + pandoc: error 137
我有以下问题:我有一个 .Rmd 文件,我使用 Rstudio 按钮通过 knitr + pandoc 将其编译为 html。
在这个 .Rmd 文件中,我按照此处描述的方法将 json 数据传递给 js 层:
http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/
这是因为我想将数据用于一些自定义的 d3 视觉效果。
这似乎适用于少量数据,但是当我尝试传递更大的数据时,我在从 .Rmd 编译到 html 时遇到问题;问题似乎出在 pandoc 上,因为我得到:
pandoc document conversion failed with error 137
我试图在网上到处寻找对此错误消息的解释,但没有成功。有谁知道这个错误是什么意思?
研究错误后,我仍然无法修复它。但是,我有一个解决方法,允许将任意 json 数据注入到使用 R markdown 生成的 html 报告中,而无需通过 pandoc;后者似乎不喜欢使用例如注入很多 json
中描述的方法
http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/
因为这个 137 错误在我看来与 pandoc 终止到 html 的转换过程有关,因为它需要很长时间才能终止。
解决方法非常简单:而不是在 knitr 编译步骤中注入 json 数据,方法是将其包含在带有 'asis' 选项的块中(如上文所述 link),最好在knitr和pandoc编译生成的html文件的末尾追加json数据。换句话说,在 kitr + pandoc 步骤的输出 html 文件中注入 json 数据。
假设要编译成 html 的 rmd 文件驻留在已安装的包中,并遵循
中的建议
Best practice for embedding arbitrary JSON in the DOM?
为了在 DOM 中嵌入 json 数据,可以使用具有以下功能的 xml2 包实现这一目标:
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)
}
在上面的函数中,data_arr应该是一个命名列表,其元素是json个字符串,例如,通过使用jsonlite::toJSON转换R对象,其名称是用于在 javascript 层中存储数据的变量名称。
通过这种方式,可以将任意大小的 json 注入到生成的 html 的 R markdown 中,以供 javascript.
操作。
我有以下问题:我有一个 .Rmd 文件,我使用 Rstudio 按钮通过 knitr + pandoc 将其编译为 html。 在这个 .Rmd 文件中,我按照此处描述的方法将 json 数据传递给 js 层: http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/
这是因为我想将数据用于一些自定义的 d3 视觉效果。 这似乎适用于少量数据,但是当我尝试传递更大的数据时,我在从 .Rmd 编译到 html 时遇到问题;问题似乎出在 pandoc 上,因为我得到:
pandoc document conversion failed with error 137
我试图在网上到处寻找对此错误消息的解释,但没有成功。有谁知道这个错误是什么意思?
研究错误后,我仍然无法修复它。但是,我有一个解决方法,允许将任意 json 数据注入到使用 R markdown 生成的 html 报告中,而无需通过 pandoc;后者似乎不喜欢使用例如注入很多 json
中描述的方法http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/
因为这个 137 错误在我看来与 pandoc 终止到 html 的转换过程有关,因为它需要很长时间才能终止。
解决方法非常简单:而不是在 knitr 编译步骤中注入 json 数据,方法是将其包含在带有 'asis' 选项的块中(如上文所述 link),最好在knitr和pandoc编译生成的html文件的末尾追加json数据。换句话说,在 kitr + pandoc 步骤的输出 html 文件中注入 json 数据。
假设要编译成 html 的 rmd 文件驻留在已安装的包中,并遵循
中的建议Best practice for embedding arbitrary JSON in the DOM?
为了在 DOM 中嵌入 json 数据,可以使用具有以下功能的 xml2 包实现这一目标:
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)
}
在上面的函数中,data_arr应该是一个命名列表,其元素是json个字符串,例如,通过使用jsonlite::toJSON转换R对象,其名称是用于在 javascript 层中存储数据的变量名称。 通过这种方式,可以将任意大小的 json 注入到生成的 html 的 R markdown 中,以供 javascript.
操作。