簡體   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