Rmarkdown PDF with kableExtra in RStudio Cloud - r

I'm trying to compile a PDF document from RStudio Cloud, using rmarkdown as the following minimal (not) working example.
First, to be able to install some latex packages (not included in MWE) I had to install tinytex with option force = TRUE. Now if I run the command pdflatex --version I see this result:
pdfTeX 3.14159265-2.6-1.40.19 (TeX Live 2018)
MWE is just below and if I try to knit it from my RStudio Cloud session I get this error:
! Use of \#startpbox doesn't match its definition.
\#startpbox {l>{\raggedright \arraybackslash }p{12cm}l}
l.101 ...{l>{\raggedright\arraybackslash}p{12cm}l}
Error: Failed to compile MWE.tex. See MWE.log for more info.
Execution halted
---
output:
bookdown::pdf_book:
toc: false
keep_tex: true
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r start, include = FALSE}
library(tibble)
library(magrittr)
```
```{r table, results = "asis"}
tbl <- tribble(~number, ~descrip, ~perc,
"8415", "long long very long description of something. More text and stuff here.", "10%")
kableExtra::kable(tbl,
booktabs = TRUE,
longtable = TRUE,
col.names = c("NCM", "Description", "IPI%")) %>%
kableExtra::kable_styling(full_width = FALSE) %>%
kableExtra::column_spec(2, width = "12cm")
```
The funny thing is, in my local R session, I can compile this without a problem, although I'm using TeX Live 2017 installed manually.
Does anyone know what's happening here? I know it's a Latex problem, but somehow it's the interaction of knitr (or pandoc?) and kableExtra that is causing some sort of conflict.
Thank you very much!

Related

Why R Markdown Caption cannot take "&"

I am gradually building an R Markdown (.RMD) file, learning by doing. I was able to insert a couple of tables, but I had a problem with one of them. The initial setup is:
---
title: "Untitled"
author: "Me"
date: "5/10/2021"
output: bookdown::pdf_book
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE,
fig_align = "left",
fig_width = 7,
fig_height = 7,
dev = "png",
cache = FALSE)
```
The original code that generated an error was
```{r sphistperf}
kable(stock_index_stats,
format="latex",
caption="S&P Historical Performance Statistics")
```
The error message is:
output file: TestCenter.knit.md
! Misplaced alignment tab character &.
<argument> ...}{\relax }}\label {tab:sphistperf}S&
P Historical Performance S...
l.202 ...rf}S&P Historical Performance Statistics}
Error: LaTeX failed to compile TestCenter.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See TestCenter.log for more info.
Error during wrapup:
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
The problem is fixed if I remove the "&" from the caption, which becomes
caption="SP Historical Performance Statistics"
Still, I want the "&" in my caption. Is there a way to keep it? I tried putting an escape character "" before it and that did not work. Any suggestions on how to keep the "&"?
According to wiki, there are some characters that needs escaping
Here, is a tested version of the markdown code
---
title: "testing"
author: "akrun"
date: "10/05/2021"
output: bookdown::pdf_book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
library(kableExtra)
kable(summary(cars), format = 'latex', caption="Dummy S\\&P Performance")
```
-output

knitting a figure to word using papaja fails

I am able to knit the following code to PDF. However, it always fails when trying to knit the output to Word (win 10, Rstudio 1.2.5033, R 3.6.2, papaja 0.1.0.9942). I had to delete the papaja header for posting (too much code).
```{r setup, include = FALSE}
library("papaja")
```
# Methods
```{r figure}
plot(cars)
```
The error message reads
Error running filter D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:
[string "--[[..."]:227: Constructor for Emph failed: [string
"--[[..."]:258: attempt to index a nil value (local 'x')
stack traceback: [C]: in function 'error'
..."]:227: in field 'Emph' D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:14: in function 'Image'
Fehler: pandoc document conversion failed with error 83
Is there any way to correct this error? It is a papaja or a pandoc error?
This is a papaja error related to post-processing of the document (in this case styling figure captions) via the docx_fixes.lua-filter. I will try to fix this as soon as possible. For the time being, you should be able to resolve this problem by specifying a figure caption in the chunk options.
```{r setup, include = FALSE}
library("papaja")
```
# Methods
(ref:fig-cap) This is the figure caption.
```{r figure, fig.cap = "(ref:fig-cap)"}
plot(cars)
```

Knit error in RStudio: pandoc conversion failed with error 23. Extension ascii_identifiers is not supported for markdown

Trying to knit a doc into html, RStudio would hang on the conversion to html part. I looked around and it looked like this happened to other folks when they were using an out of date pandoc version. I updated pandoc to 2.8. I received the error in the title. Looking around again, it appeared that v 2.5 seemed stable. I installed that, restarted my computer, and still get the same error.
repex is the default example when you create an RMarkdown file in RStudio: file -> New file -> R Markdown -> html.
---
title: "repexpandocerror23"
author: "Adam Korejwa"
date: "11/22/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for
authoring HTML, PDF, and MS Word documents. For more details on using R
Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that
includes both content as well as the output of any embedded R code chunks
within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to
prevent printing of the R code that generated the plot.
I have fixed this issue two months ago. Please make sure your rmarkdown version is at least v1.16. Once again, when in doubt, consider updating your packages:
update.packages(ask = FALSE, checkBuilt = TRUE)

Rnotebook not showing code output for data frames

My code chunk output in Rnotebook is not appearing (as if not being run) when I try to view data frame results. I have to pass it through the pander() function to see the output print out. Is this something to do with knitr? I mention this because I set the options at the beginning to the following:
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = TRUE)
```
I have tried setting the options directly in the chunk but get the same unwanted result. Is there a setting I am not configuring correctly? I have to also mention that this is a behaviour that has been somehow inconsistent. That is, I may stop working on it and some time later the code output comes up somehow.
Here's an sample of the work code I am trying to run to copy paste into Rnotebook.
Setting the notebook workspace options
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = TRUE)
```
Loading the corresponding libraries and packages
```{r}
library(easypackages)
libraries("dplyr",
"ggplot2",
"caret",
"tidyverse",
"tidytext",
"ROCR",
"pander",
"knitr",
"broom")
```
Here's some sample data:
```{r}
library(readr)
attibm <- read_csv("https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/mtcars.csv",
col_types = cols(Attrition = col_character()))
```
Seeing the structure. (This output is shown as expected)
```{r}
glimpse(attibm)
```
Preview the first ten rows (this is the output that doesn't show. Nothing happens)
```{r}
head(attibm)
```
This output doesn't show either. (Nothing happens)
```{r}
attibm %>%
summarise_if(is.integer, mean)
```
When I pass the pander function THEN it is shown.
```{r}
attibm %>%
summarise_if(is.integer, mean) %>%
pander()
```
This one is shown too
```{r}
pander(head(attibm))
```
I have checked the question posted: Output of numbers in R notebook, but I wasn't able to see the connection with this case.
I hope this is clear enough and that you can reproduce the code shown here. Any help on this issue will be highly appreciated.
The newest version of markdown is no longer compatible with pandocv2.
You can check your version of pandoc using
library(rmarkdown); pandoc_version()
If it's pandoc version you need the development version of markdown that you can download there
library(devtools); install_github("rstudio/rmarkdown")
To narrow down the issue of whether this is a problem with the newest version of pandoc, try checking wether the .md produced is correct by adding
---
output:
html_notebook
keep_md: true
---
I had a similar problem which data.frame and DT:data.table wouldn't show any output.
this post helped me.
I found that the cause of problem was my mis-typing in .rmd file name including a non-ASCII character! as soon as i removed it the problem resolved.
Hope this helps others too

Include error messages from testthat in R markdown document

I want to include error messages in an R markdown pdf report. This works well:
---
output: pdf_document
---
This will be knitted and show the error message in the pdf.
```{r, error = TRUE}
stopifnot(2 == 3)
```
However, if I try the same approach with an error that comes from testthat, my document does not knit anymore.
---
output: pdf_document
---
This will not knit
```{r, error = TRUE}
library(testthat)
expect_equal(2, 3)
```
Why is that? And what can I do to include error messages from testthat's expect_something functions without wrapping them up in a test?
I think this must be possible since Hadley Wickham includes many error messages in his book R packages that come directly from expect_something-functions.
This is related, but not answered in Include errors in R markdown package vignette and How to skip error checking at Rmarkdown compiling?
Create a test:
```{r, error = TRUE}
library(testthat)
test_that("Test A", {
expect_equal(2, 3)
})
```
I don't understand the reason for the behavior (good question!), but this could be a workaround:
---
output: pdf_document
---
This will knit
```{r, error = TRUE}
library(testthat)
# expect_equal(2, 3)
# skip_if_not(2, 3)
assertthat::assert_that(2 == 3)
```

Resources