Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown file knits to PDF successfully, but error when called from R

I have written an Rmarkdown file that loads some data and generates a cool kableExtra table with some symbols and custom colors and lines, printing it to pdf.

It works great when I open the Rmarkdown file and knit, and produces the exact output that I want.

I would now like to use an R script that loops through some different subsets of the data, producing a new table (and separate output pdf) for each. So, I need to call my .Rmd file from within an .R file.

However, when I do this (using the rmarkdown::render function), I get errors that don't occur when knitted directly from within the .Rmd file.

Below is a fully functioning .Rmd file that is similar to the one I'm using with my real data. I'm calling it from a second R script, with the line rmarkdown::render("table_creator.Rmd", pdf_document(latex_engine = "xelatex")).

The first error I encounter is Error: Functions that produce HTML output found in document targeting latex output.. I add always_allow_html: yes to the header of the .Rmd file and try again. Directly knitting the .Rmd file again works fine. Calling it from the .R script now yields the error: ! Undefined control sequence. l.94 ...oup\fontsize{16}{18}\selectfont \textcolor [HTML]{ffde71}{$\bullet$}\...

At this point I think the rendering is happening with different settings of some sort than what are being used when I knit the .Rmd file directly. It's almost like it's not parsing the full header from the .Rmd file, but I'm not sure. I'm at a loss for how to proceed at this point and would appreciate any help.

---
title: ""
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
- \usepackage[T1]{fontenc}
- \setmainfont{Helvetica}
- \DeclareTextCommand{\nobreakspace}{TU}{\leavevmode\nobreak\ }
---

```{r, echo=FALSE, message = FALSE, warning = FALSE}
# Packages
suppressMessages(library(kableExtra))
suppressMessages(library(dplyr))

table_info <- data.frame(Symbol = c("$\\bullet$", "$\\blacksquare$",
                                    "$\\blacklozenge$", "x", "+", "$\\bullet$"),
                         Name = letters[1:6],
                         Results = sample(c("good", "bad"), 6, replace = TRUE),
                         FontSize = c(16, 10, 11, 16, 16, 18),
                         Color = c("#ffde71", "#0c0000", "#0c0000", "#0c0000",
                                   "#0c0000", "#cb6f86"))

 ktable <- table_info %>%
     mutate(Symbol = cell_spec(Symbol, color = Color, 
                              font_size = FontSize,
                              escape = FALSE,
                              format = "latex")) %>%
    select(-Color, -FontSize) %>%
    kable(escape = FALSE, align = c("l", "l", "l"),
          booktabs = TRUE) %>%
    row_spec(1:5, hline_after = TRUE) %>%
    row_spec(1:6, color = "gray") %>%
    column_spec(1, "1.5em")

```

```{r, echo=FALSE}
ktable
```

EDIT: Additional context info Output of sessionInfo():

sessionInfo() R version 3.4.3 (2017-11-30) Platform: x86_64-apple-darwin17.3.0 (64-bit) Running under: macOS High Sierra 10.13.1

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] bindrcpp_0.2 dplyr_0.7.4 kableExtra_0.9.0 rmarkdown_1.9

loaded via a namespace (and not attached): [1] Rcpp_0.12.15 rstudioapi_0.7 bindr_0.1 knitr_1.20 xml2_1.2.0
[6] magrittr_1.5 hms_0.4.2 rvest_0.3.2 munsell_0.4.3 viridisLite_0.3.0 [11] colorspace_1.3-2 R6_2.2.2 rlang_0.1.6 plyr_1.8.4 stringr_1.2.0
[16] httr_1.3.1 tools_3.4.3 htmltools_0.3.6 yaml_2.1.18 assertthat_0.2.0 [21] rprojroot_1.3-2 digest_0.6.15 tibble_1.4.2 readr_1.1.1 glue_1.2.0
[26] evaluate_0.10.1 stringi_1.1.6 compiler_3.4.3 pillar_1.1.0 scales_0.5.0
[31] backports_1.1.2 pkgconfig_2.0.1

like image 595
bogenton Avatar asked Nov 17 '22 10:11

bogenton


1 Answers

I'm returning to this as I also had a very similar problem. The issue was that I was able to manually knit a PDF but once I did it via a script in R, I was receiving a Error in xml_children(x)[[search]] : subscript out of bounds error.

I was able to pinpoint that this was stemming from a row_spec() all within the kable production but upon removing the row_spec() call, I was then met with a error: Functions that produce HTML output found in document targeting latex output. error.

The odd thing was that this only happened on a particular branch of my project - not the main branch. But, switching back to the main branch did not rectify the issue. Restarting R did. Further compounding the issue was that if I started R in the main branch and then switched to the development branch, but didn't re-source everything automatically, the error did not appear. This indicated that the error lied somewhere in the development branch's new scripts.

So I did a sessionInfo() call on loading R via the main branch and then the development branch and found that in one of my newer scripts, I had an additional library(kableExtra) that was being loaded after all the other packages were brought in (including a kableExtra call already). For some strange reason, this additional kableExtra call was also bringing in virisdisLite and webshot, two packages that were not originally present with the initial kableExtra call.

Once the extra library(kableExtra) was removed, the problem was solved and the two additional packages virisdisLite and webshot were no longer called and there were no longer any issues with render() to pdf.

like image 68
Justin Cocco Avatar answered Jan 08 '23 15:01

Justin Cocco