Textwrapping long string in knitr output (RStudio)

I have a long vector string (DNA sequence) up to several thousand consecutive characters, which I want to add to my knitr release report. RStudio handles text wrapping fine in the console, but when I create the knitr html output, I see only one line of text, and it just runs away from the page.

RStudio Output

knife exit

Any way to configure knitr output to wrap text?

Thanks.

+8
r rstudio knitr textwrapping r-markdown
source share
1 answer

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

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

Another solution is to manually split the long string using the str_break() function, which 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') ``` 
+5
source share

All Articles