Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R markdown in RStudio, how can I prevent the source code from running off a pdf page?

Tags:

r

rstudio

I currently have some code that looks like so:

```{r, tidy=TRUE} plot(DT$age, DT$height, xlab = "Age of participant in Trials", ylab = "Height of participant in       Trials") ``` 

Now, it was my understanding that setting tidy to TRUE would make it so that when I knit the code together, the code would not go running off the page and would wrap by itself. However, I sporadically still get run off source code displays when I do commands like the one above. Is there another function that would guarantee the wrapping of code? Thanks!

like image 646
user1398057 Avatar asked Oct 06 '14 05:10

user1398057


People also ask

How do I stop the code from running in R markdown?

include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.

How do I stop a chunk from running in R?

However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.

How do I embed an R code in Markdown?

To insert a code chunk, press Ctrl + Alt + I in the source pane (top left pane in the default settings of RStudio). A code chunk will appear: Inside the code chunk you can write and run R-code.

How do you stop knitting in R?

To exit early from the knitting process, you may use the function knitr::knit_exit() anywhere in the source document (in a code chunk or inline expression). Once knit_exit() is called, knitr will ignore all the rest of the document and write out the results it has collected so far.


1 Answers

Use the width.cutoff argument inside tidy.opts knitr options to specify the output width :

```{r, tidy=TRUE, tidy.opts=list(width.cutoff=60)} plot(DT$age, DT$height, xlab = "Age of participant in Trials", ylab = "Height of participant in trials") ``` 

You can define this option globally for your whole file with a chunk like this :

```{r} library(knitr) opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE) ``` 

The tidy.opts options are passed to the formatR package which does the tidying (if I understand correctly). In-depth informations about formatR can be found here :

http://yihui.name/formatR/

like image 63
juba Avatar answered Sep 29 '22 07:09

juba