31

In my documents in r parts I use long codes like:

```{r}
output <- "very long query for example url to some website............................................."
output
```

Is there any way not to make R wrap the code automatically, let's say after 60 characters? I tried tidy=TRUE, tidy.opts=list(width.cutoff=60) option but it doesn't work.

And I want the same with output, because the output of my query is some text. I want to display the whole content in many lines, not just the begining in one line. How can I do it?

dshkol
  • 1,208
  • 7
  • 23
jjankowiak
  • 3,010
  • 6
  • 28
  • 45
  • Did you already look here: http://stackoverflow.com/questions/12176296/knitr-how-to-prevent-text-wrapping-in-output and here http://stackoverflow.com/questions/26210656/in-r-markdown-in-rstudio-how-can-i-prevent-the-source-code-from-running-off-a-p ? – symbolrush Nov 03 '15 at 14:38

2 Answers2

19

I had this same issue until I realized that one needs to install the R package formatR. Once you install and load this package, use tidy=TRUE, tidy.opts=list(width.cutoff=60) in your chunk, or use the following line of code to set it globally:

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
ecbiz1
  • 301
  • 2
  • 3
  • Yes! Thank you so much @ecbiz1. I was at my wits end trying to understand why the tidy addition wasn't helping any of my markdown files. This is super important to realize that you need to install the package. (Especially because there isn't any error message telling the user that the function being called is not found in the current libraries. – Ari Sep 14 '21 at 02:20
  • Are there other necessary packages for this? I've got formatR installed and called, and I tried setting the tidy options both globally and in the specific chunk, and it isn't working for me. – Michael Clauss Mar 23 '23 at 16:05
  • Could you put your code in here? – ecbiz1 Mar 27 '23 at 19:49
5

Things have changed since 2015, but FWIW given that you are now using rmarkdown_1.8 and knitr_1.20:

  1. it is handled properly in the default html output;
  2. if you are looking for a pdf output, what you can do is

    • breaking the line to control the code chunk length (if you want to display it) but note that in a character string a \n will be added (in which case you may benefit from using two different code chunks: one for displaying the code another for the outputs);
    • regarding the output, you could set the code chunk option results to 'asis' so the output is handled as if it were a regular piece of text, in which case you can also use paste() or paste0() to use latex tags to tweak how the text is displayed.

So:

1- display the code

{r, eval = F}
output <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
output

2- get the output

{r, results = 'asis', echo = F}
output <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
output

Hope this could help.

Kevin Cazelles
  • 1,255
  • 9
  • 20
  • 3
    +1 this seems to be the only answer on SO that actually work. It's suboptimal - not only do we repeat ourselves, but the output is ugly (it's not _supposed_ to look like ordinary text). But it does work. – Vorac May 30 '18 at 18:33
  • 2
    [Datacamp](https://campus.datacamp.com/courses/reporting-with-r-markdown/chapter-two-embedding-code?ex=7) explains how to avoid code duplication in this situation via the use of code chunk labels. I haven't tested it. – Vorac Jun 01 '18 at 21:44