2

When I try to knit my RMarkdown file to a PDF, for some reason I can't get the r code chunks to fit inside the margins.

I have tried multiple solutions including

  1. opts_chunk$set(tidy.opts=list(width.cutoff=40),tidy=TRUE)

I got this solution from the link below.

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

But as you can see below, even when set this, the code still runs off the page.

  1. Putting out.width='50%' in my r chunck headers

  2. Just trying to space out my code so there are no long lines.

But nothing has worked.

Here is what the problem looks like

And here is the corresponding RMarkdown code

```{r message = FALSE , tidy=TRUE, out.width='50%'}
## bunch of code

poverty_status_table <-
    create_table(df, "POVERTY STATUS IN THE PAST 12 MONTHS", year)  %>%
    mutate(
      POVERTY.STATUS.IN.THE.PAST.12.MONTHS = 
        gsub(
        "^Workers 16 years and over for whom poverty status is determined - ",
        "",
        POVERTY.STATUS.IN.THE.PAST.12.MONTHS
      )
    ) %>% 
    mutate(
      POVERTY.STATUS.IN.THE.PAST.12.MONTHS = gsub(
        " of the poverty level",
        "",
        POVERTY.STATUS.IN.THE.PAST.12.MONTHS
      )
    ) %>% 
    rename(Poverty.status = 
             POVERTY.STATUS.IN.THE.PAST.12.MONTHS) %>% 
    filter(Poverty.status != 
             "Workers 16 years and over for whom poverty status is determined")

## more code

As you can see in the image, my goal is to have the code stay inside the margins.

  • 1
    Possible duplicate of [In R markdown in RStudio, how can I prevent the source code from running off a pdf page?](https://stackoverflow.com/questions/26210656/in-r-markdown-in-rstudio-how-can-i-prevent-the-source-code-from-running-off-a-p) Sorry, just noticed this is actually a duplicate. I'll be deleting my answer below in a minute or two. – duckmayr Jun 03 '19 at 20:16
  • That question is similar but it has only one answer and that answer doesn't appear to solve the issue. – Joseph Kahadze Jun 03 '19 at 20:41
  • Do you mean that you tried using `tidy.opts = list(width.cutoff = 60)` and it did not work for you? – duckmayr Jun 03 '19 at 20:43
  • 2
    Yes, that's what I mean. It only appears to affect comments and some lines of r code, but other lines of r code keep going off the margin, as shown in the image in my post. The picture of the output above was take with ```width.cutoff = 20``` and even then the r code doesn't fit. – Joseph Kahadze Jun 03 '19 at 20:52
  • And this was using the code you have posted in your question? Because that code using `tidy.opts` does not cause this issue on my machine. If you're getting the problem with code that differs from what you have posted, edit your question to include the actual problematic code. – duckmayr Jun 03 '19 at 20:57

0 Answers0