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') ```
Yihui xie
source share