Search code examples
rr-markdownknitrpdflatexbookdown

`bookdown`/`rmarkdown`/`knitr`: Line wrapping in code chunks


I'm using

```{r}
library(magrittr)
session_info <- devtools::session_info(include_base = TRUE)
session_info[[2]] <- session_info %>%
  magrittr::extract2(2) %>%
  magrittr::inset2("date", value = NULL)
session_info
```

at the end of my bookdown document. As I am using git tracked in-house packages the version strings get a bit longer. This results in R/knitr/rmarkdown/bookdown line-breaking the resulting data.frame (session_info[[2]]), which disconnects names and versions visually and is thus suboptimal.

I have played with this solution to reduce code chunk font size, but that just renders it clear that the root of the problem is line breaking happening somewhere prior to pdflatexing.

Where could I influence when R/knitr/rmarkdown/bookdown line-breaks code output?


Solution

  • The final session_info call transparently invokes print on the object (through print.data.frame in the case of session_info[[2]]).

    print offers a width argument, which defaults to getOption("width") and for which ?options states

    width:

    controls the maximum number of columns on a line used in printing vectors, matrices and arrays, and when filling by cat.

    Columns are normally the same as characters except in East Asian languages.

    Accordingly and as getOption("width") produces 136 in my environment, above code may be augmented to:

    ```{r}
    library(magrittr)
    session_info <- devtools::session_info(include_base = TRUE)
    session_info[[2]] <- session_info %>%
      magrittr::extract2(2) %>%
      magrittr::inset2("date", value = NULL)
    print(session_info, width = 200)
    ```