Knitr: How to Prevent Text Wrapping in Output

knitr: How to prevent text wrapping in output?

Adding something like options(width=120) to your document would allow you to override the default wrapping width.

Be careful about going too wide though; when converting to PDF or other formats, the default is pretty much just right!

As an example, I use Knitr from RStudio, and type my document as a R markdown document. My document "options" at the start might be something like this:

```{r set-options, echo=FALSE, cache=FALSE}
options(width=80)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, echo = TRUE, tidy = TRUE, size="small")
read_chunk("some/script/I/want/to/load.R")
```

Avoid text-wrapping of code in Rmarkdown html output

This can be done by adding style tags below the YAML header and overriding the existing CSS for pre and code:

pre {
overflow-x: scroll
}

pre code {
white-space: pre;
}

Prevent short comments from wrapping in R Markdown/knitr output

As suggested in the comments, I'll answer my own question.

If one looks at Yihui's documentation for formatR, one might notice that roxygen comments (which look like this: #') will not be wrapped in any case.

So using the code chunk

```{r, tidy = T}
myfun=function(a,b){
#' ^_^
#' {o,o}
#' |)__)
#'-----m-m-----
c=sum(a,b)
return(c)
}
```

will give me the desired output:

myfun = function(a, b) {
#' ^_^
#' {o,o}
#' |)__)
#'-----m-m-----
c = sum(a, b)
return(c)
}

R knitr::kable: avoid text wrap of one column

You could use kableExtra::column_spec:

library(kableExtra)
kable(testdata) %>% column_spec(column = 4, width = "100px")

Textwrapping long string in knitr output (RStudio)

I recommend you to try the R Markdown v2. The default HTML template does text wrapping for you. This is achieved by the CSS definitions for the HTML tags pre/code, e.g. word-wrap: break-word; word-break: break-all;. These definitions are actually from Bootstrap (currently rmarkdown uses Bootstrap 2.3.2).

You were still using the first version of R Markdown, namely the markdown package. You can certainly achieve the same goal using some custom CSS definitions, and it just requires you to learn more about HTML/CSS.

Another solution is to manually break the long string using the function str_break() I wrote below:

A helper function `str_break()`:

```{r setup}
str_break = function(x, width = 80L) {
n = nchar(x)
if (n <= width) return(x)
n1 = seq(1L, n, by = width)
n2 = seq(width, n, by = width)
if (n %% width != 0) n2 = c(n2, n)
substring(x, n1, n2)
}
```

See if it works:

```{r test}
x = paste(sample(c('A', 'C', 'T', 'G'), 1000, replace = TRUE), collapse = '')
str_break(x)
cat(str_break(x), sep = '\n')
```

RMarkdown: Wrap code in chunks but keep breaks after pipe

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

Sample Image

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
```


Related Topics



Leave a reply



Submit