vio
vio

Reputation: 137

R Markdown Knitr to PDF Code Outside Margin

I want to attach the entirety of my code chunks at the end of my document. The code includes plots, inserted graphics, calculations and so forth. It's long. This is after I have run the relevant code in its respective chapters. At this point, I don't want the code to generate any results, but simply show the code so that others can check on what I have done.

Here is an example of part of the code:

shapefile1 <- readOGR("./Folder1/Folder2/Folder3", layer = "TM_WORLD_BORDERS-0.3") # Read in world shapefile

When I compile, it shows up outside the margin that I have set for my document in the YAML header like this

geometry: top=2.5cm, bottom=2cm, left=3cm, right=3.5cm

I have tried the following solutions:

Option 1:

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

```{r, eval=FALSE, tidy=TRUE, tidy.opts=list(width.cutoff=60)}
```

Option 2:

https://tex.stackexchange.com/questions/133810/knitr-plotting-outside-margin

```{r, eval=FALSE, out.width=".9\\paperwidth"}
```

Neither works for me. The only other thing I can think of is manually making the code lines shorter, but I'm sure that can't be the best option. How is this done?

Upvotes: 5

Views: 6308

Answers (1)

gustavobrp
gustavobrp

Reputation: 53

This happens to me when I use "pdflatex" as the Latex Engine in R Studio. When I change to "xelatex", this doesn't happen anymore. But I also need this code chunk in the beggining of the markdown:

library(knitr)
# Set so that long lines in R will be wrapped:
opts_chunk$set(tidy.opts=list(width.cutoff=40),tidy=TRUE)

Upvotes: 1

Related Questions