繁体   English   中英

R降价通过Knitr + Pandoc到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文档转换失败,错误137

我试图在网上到处寻找该错误消息的解释,但没有任何运气。 有谁知道这个错误是什么意思?

研究错误后,我仍然无法修复它。 但是,我有一种解决方法,可以将任意json数据注入使用R markdown生成的html报表中,而无需完全通过pandoc; 后者似乎不喜欢使用例如中所述的方法注入大量json

http://livefreeordichotomize.com/2017/01/24/custom-javascript-visualizations-in-rmarkdown/

因为这137错误对我来说似乎与pandoc有关,因为它需要很长时间才能终止,因此终止了对html的转换过程。

解决方法非常简单:与其在knitr编译步骤中通过使用asis选项(如上面的链接所述)将其包含在块中来注入json数据,不如将json数据附加到末尾。在通过knitr和pandoc编译生成的html文件中。 换句话说,将json数据注入kitr + pandoc步骤的输出html文件中。

假设要编译为html的rmd文件驻留在已安装的程序包中,并遵循以下建议:

在DOM中嵌入任意JSON的最佳实践?

为了在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注入R markdown生成的html中,以供javascript处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM