How to wrap text in R source with tidy and knitr

后端 未结 3 1492
说谎
说谎 2021-02-20 06:05

I\'m working with knitr lately and while most aspects of that have gone quite smoothly, there\'s one formatting issue with including R code in the finished document that I haven

相关标签:
3条回答
  • 2021-02-20 06:06

    This answer is a bit late to the party, but I have found that even when I use tidy.opts = list(width.cutoff = 60) in an early chunk (using RStudio and a .Rnw script) and then in each chunk option list I include tidy = TRUE, the overflow of lines still happens. My overflow lines are in sections of code that create ggplot2 plots. Trial and error discovered that if I add a carriage return after the + at the end of a line, I have no overflow problems. The extra line does not show up in the PDF that LaTeX creates.

    0 讨论(0)
  • 2021-02-20 06:10

    The other solution is to use strwrap.

    > longstr <- "This string will flow off the right side of the page, because tidy doesn't know how to wrap it."
    > strwrap(longstr, 70)
    [1] "This string will flow off the right side of the page, because tidy" "doesn't know how to wrap it."                                      
    > str(strwrap(longstr, 70))
    chr [1:2] "This string will flow off the right side of the page, because tidy" "doesn't know how to wrap it."
    

    Unfortunately, I do not know whether this will work with tidy, but it works extremely well with knitr's HTML output.

    0 讨论(0)
  • 2021-02-20 06:13

    This is an extremely manual solution, but one which I have used.

    You build the string up, using paste0 and that gives tidy a chance to split it.

    longstr <- paste0("This string will flow off the right side"," of the page, because tidy doesn't know how to wrap it.")
    
    0 讨论(0)
提交回复
热议问题