r-markdownknitr

Strange bug with line wraps somewhere in knitr, rmarkdown, sth else


I am encountering a really strange bug in rendering plain lists in Rmarkdown. Interestingy, I can only reproduce if R (or Rscript) is running non-interactively.

Consider this file (save as test_rmarkdown.Rmd):

---
title: Rmarkdown Test
---

```{r, message = FALSE}
library("dplyr")
library("report")
```

```{r, results = "asis"}
iris %>%
    dplyr::select(Petal.Length, Species, everything()) %>%
    dplyr::rename(Spe = Species) %>%
    report::report(iris) %>%
    summary()
```



```{r, comment = '', results = "markup"}
sessionInfo()
```

When I render this file using

rmarkdown::render('test_rmarkdown.Rmd', 'html_document')

The output is as expected.

However, when I render this file using

system("Rscript -e \"require(rmarkdown); rmarkdown::render('test_rmarkdown.Rmd', 'html_document')\"")
# or directly from the shell, and with the same result using `R` instead of `Rscript`

I get a broken list, as in the test_rmarkdown.md there is a hard wrapped line break:

The data contains 150 observations of the following 5 variables:

  - Petal.Length: n = 150, Mean = 3.76, SD = 1.77, range: [1, 6.90]
  - Spe: 3 levels, namely setosa (n = 50), versicolor (n = 50) and virginica (n =
50)
  - Sepal.Length: n = 150, Mean = 5.84, SD = 0.83, range: [4.30, 7.90]
  - Sepal.Width: n = 150, Mean = 3.06, SD = 0.44, range: [2, 4.40]
  - Petal.Width: n = 150, Mean = 1.20, SD = 0.76, range: [0.10, 2.50]

I do not even know, who is to blame. knitr ?

Can someone confirm? And could someone explain what goes wrong here?

Thanks in advance!

PS: There is a work-around: Using cat works even when compiling non-interactively:

```{r, results = "asis"}
iris %>%
    dplyr::select(Petal.Length, Species, everything()) %>%
    dplyr::rename(Spe = Species) %>%
    report::report(iris) %>%
    summary() %>%
    paste0("\n") %>%
    cat
```

PPS: Here is the sessionInfo:

R version 4.5.0 (2025-04-11)
Platform: x86_64-pc-linux-gnu
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /opt/intel/oneapi/mkl/2023.0.0/lib/intel64/libmkl_gf_lp64.so.2;  LAPACK version 3.10.1

locale:
 [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8    
 [5] LC_MONETARY=en_US.utf8    LC_MESSAGES=en_US.utf8   
 [7] LC_PAPER=en_US.utf8       LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C      

time zone: Europe/Berlin
tzcode source: system (glibc)

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

other attached packages:
[1] report_0.6.1   dplyr_1.1.4    rmarkdown_2.29

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.5         knitr_1.50        rlang_1.1.6      
 [5] xfun_0.52         renv_1.1.4        generics_0.1.4    jsonlite_2.0.0   
 [9] glue_1.8.0        htmltools_0.5.8.1 sass_0.4.10       datawizard_1.1.0 
[13] evaluate_1.0.4    jquerylib_0.1.4   tibble_3.3.0      fastmap_1.2.0    
[17] yaml_2.3.10       lifecycle_1.0.4   insight_1.3.0     compiler_4.5.0   
[21] pkgconfig_2.0.3   digest_0.6.37     R6_2.6.1          tidyselect_1.2.1 
[25] pillar_1.10.2     magrittr_2.0.3    bslib_0.9.0       withr_3.0.2      
[29] tools_4.5.0       cachem_1.1.0

Solution

  • Set the line width before rendering

    system("Rscript -e \"require(rmarkdown); options('width'=100); rmarkdown::render('test_rmarkdown.Rmd', 'html_document')\"")
    

    There's nothing to blame. knitr is doing a nice thing and avoiding overriding width on its own.

    Perhaps the blame should be on the default value of width in R. It is 72.