7

I'm trying to achieve two things that might not be compatible: I would like my code that is in my RMarkdown chunks to be wrapped around in each each line when I create a PDF (in other words, the line below runs over the page edge).

enter image description here

After reading around a bit (including here and attempting the styler package briefly, I have found that using tidy=TRUE and tidy.opts = list(width.cutoff=60) works (see below)

knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)

NB: you might need to have formatR installed.

enter image description here

But sadly this ruins the format of the code with the pipe %>% keeping the format of the pipes and letting the code run off the page.

Is there anyway to do this properly? So that both the text stays on page and that the structure remains somewhat in place?

Thanks for your help!

To rework:

Header for both:

---
title: "R Notebook"
output: pdf_document
---

Opts chunk for second image

knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)

Example Code for both:

data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>% 
  mutate(mpg = ifelse(mpg>18,
                   yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
                   no = mpg)) %>% 
  select(mpg,disp) %>% 
  group_by(mpg) %>% 
  summarise(mean(mpg)) %>% 
  ungroup -> df

I'm also adding the SessionInfo() for the package versions etc.

R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

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

other attached packages:
[1] forcats_0.5.0   stringr_1.4.0   dplyr_1.0.0     purrr_0.3.4     readr_1.3.1     tidyr_1.0.2     tibble_3.0.0   
[8] ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0 xfun_0.15        haven_2.2.0      lattice_0.20-41  colorspace_1.4-1 vctrs_0.3.0     
 [7] generics_0.0.2   htmltools_0.4.0  yaml_2.2.1       rlang_0.4.6      pillar_1.4.3     glue_1.4.1      
[13] withr_2.2.0      DBI_1.1.0        dbplyr_1.4.3     modelr_0.1.6     readxl_1.3.1     lifecycle_0.2.0 
[19] munsell_0.5.0    gtable_0.3.0     cellranger_1.1.0 rvest_0.3.5      evaluate_0.14    knitr_1.28      
[25] fansi_0.4.1      broom_0.5.6      Rcpp_1.0.4.6     backports_1.1.6  scales_1.1.0     jsonlite_1.6.1  
[31] fs_1.4.1         hms_0.5.3        packrat_0.5.0    digest_0.6.25    stringi_1.4.6    grid_4.0.0      
[37] cli_2.0.2        tools_4.0.0      magrittr_1.5     crayon_1.3.4     pkgconfig_2.0.3  ellipsis_0.3.0  
[43] xml2_1.3.1       reprex_0.3.0     lubridate_1.7.8  assertthat_0.2.1 rmarkdown_2.3    httr_1.4.1      
[49] rstudioapi_0.11  R6_2.4.1         nlme_3.1-147     compiler_4.0.0  
Moritz Schwarz
  • 2,019
  • 2
  • 15
  • 33
  • I'm struggling to reproduce your findings. Can you tell me which package versions you're using? – Fred Boehm Aug 03 '20 at 00:51
  • Of course, thanks for pointing this out. I'll post the ````SessionInfo()```` above. Can you possibly tell me what is different when you try it on your computer? – Moritz Schwarz Aug 03 '20 at 00:52
  • When I use `knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)`, I get a pdf like that in your first image - the code is not fully captured on the page while the line breaks are preserved. – Fred Boehm Aug 03 '20 at 00:58
  • 1
    ok, just did a quick search for this. Could it be that you need to install `formatR` [link](https://cran.r-project.org/web/packages/formatR/index.html)? If that's it, I'll add it explicitly in the post above! – Moritz Schwarz Aug 03 '20 at 01:07
  • Yes, you're right. I mistakenly expected an error or warning if I didn't have `formatR` installed. Now I can reproduce your example. Unfortunately, I don't have any great ideas for solutions. I did see that instead of saying `tidy=TRUE` one can pass a function, like `tidy=myfunction`. – Fred Boehm Aug 03 '20 at 17:07
  • Great to know, it really should give you an error message imho - in any case, thanks for trying! I've now added the information above. I might need to look more closely into the `tidy=myfunction` setting - I know that the `styler` package uses this, but I have not managed to get this to work... – Moritz Schwarz Aug 03 '20 at 20:06

1 Answers1

2

Although I'm not sure whether the following image reflects what you want, you can wrap your lines as follows:

enter image description here

You can control the appearance of codes in your PDF files via LaTeX's fvextra package. You can call it and configure the settings of it via header-includes:

header-includes:
  - |
    ```{=latex}
    \usepackage{fvextra}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
      showspaces = false,
      showtabs = false,
      breaklines,
      commandchars=\\\{\}
    }
    ```

Add breaksymbolleft={} if you want to remove arrow symbols in the wrapped lines (See also the description of breaksymbolleft in the manual):

header-includes:
  - |
    ```{=latex}
    \usepackage{fvextra}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
      breaksymbolleft={},
      showspaces = false,
      showtabs = false,
      breaklines,
      commandchars=\\\{\}
    }
    ```

This can all be combined with the tidy = "stlyer" option to have both functionalities (breaking lines and using the tidy style).

MWE

---
title: "R Notebook"
output: pdf_document
header-includes:
  - |
    ```{=latex}
    \usepackage{fvextra}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
      breaksymbolleft={}, 
      showspaces = false,
      showtabs = false,
      breaklines,
      commandchars=\\\{\}
    }
    ```
---

```{r}
knitr::opts_chunk$set(tidy="styler")
```

```{r}
data(mtcars)
library(tidyverse)
variable_1 <- 10
variable_2 <- 50
variable_3 <- 30
variable_4 <- 5
variable_5 <- 100
variable_6 <- 25
variable_7 <- 600
mtcars %>% 
  mutate(mpg = ifelse(mpg>18,
                   yes = (variable_1*variable_2)*variable_3 + variable_4 + variable_5 + variable_6 + variable_7,
                   no = mpg)) %>% 
  select(mpg,disp) %>% 
  group_by(mpg) %>% 
  summarise(mean(mpg)) %>% 
  ungroup -> df
```
Moritz Schwarz
  • 2,019
  • 2
  • 15
  • 33
Carlos Luis Rivera
  • 3,108
  • 18
  • 45