Coder Social home page Coder Social logo

wraprmd's Introduction

WrapRmd

An RStudio Addin to wrap paragraphs of RMarkdown text without inserting line breaks into inline R code.

Installation

You can install the plain version WrapRmd from GitHub with:

# install.packages("devtools")
devtools::install_github("tjmahr/WrapRmd")

This package used the commonmark package to wrap and reformat to markdown text. Using commonmark means that it can wrap links and markdown lists. The package does some additional work to handle inline R Markdown.

Overview

Here is some nice looking RMarkdown:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum a `r max(iris$Sepal.Length)`, viverra nisl at, luctus ante = `r length(letters) * 2 + 100`.

You highlight the text, and hit Ctrl/Cmd + Shift + / to wrap the text and get:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum a `r
max(iris$Sepal.Length)`, viverra nisl at, luctus ante = `r length(letters) * 2 +
100`.

This RStudio Addin wraps text, but doesn't insert line breaks into inline R code, yielding:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
a `r max(iris$Sepal.Length)`, viverra nisl at, luctus ante =
`r length(letters) * 2 + 100`.

An animation of the above

Notes

Then go to Tools > Addins in RStudio to select and configure addins. I've mapped this one addin to the shortcut Ctrl + Shift + Alt + /.

The package wraps lines using a maximum width set by options("WrapRmd.width") which currently defaults to 80 characters.

It should work on multiple paragraphs:

Animation of wrapping paragraphs separately

wraprmd's People

Contributors

shirdekel avatar tjmahr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wraprmd's Issues

Support links?

Loving this package! It does, however, break links and occasionally formatting when knitting to html (e.g. bold move can end up with the asterisks staying in, and no formatting— sorry, I actually can't recreate it with GitHub md). Let me know if you want me to reprex anything!

Again, awesome package!

Avoid wrapping cross-references

Would you consider adding support for avoiding inserting line breaks into cross-references, as with inline R code?

I'm referring to lines such as Figure\ \@ref(fig:plot-name) or Table\ \@ref(tab:table-name).

Adding a line break betweenFigure\ and \@ref(fig:plot-name) leads to a forced line break in the output.

Happy to attempt a PR.

Support roxygen-style comment paragraphs

It is possible to embed Markdown text within a standard .R file (instead of embedding code in chunks in a .Rmd file) and compile the .R file with knitr::spin(), as long as the text is prefaced with roxygen-style #' comments. Regular inline R code can be included as well, just like in an .Rmd file.

When wrapping with WrapRmd (to maintain inline R code), however, the #' line prefixes are not maintained. For example, this:

#' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod `r rnorm(10)` tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Wraps to this, which keeps the inline R code intact, but doesn't continue the #' prefixes:

#' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
`r rnorm(10)` tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

RStudio continues the #' prefixes when wrapping:

#' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod `r
#' rnorm(10)` tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
#' minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
#' ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
#' velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
#' cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
#' est laborum.

But it obviously doesn't preserve inline R code (which is why this addin exists in the first place).

Would it be possible to add #' to rewrapped roxygen-style prose chunks in .R files and maintain inline R code?

LaTeX commands and references escaped with backslash

I have an issue using the issue in combination with LaTeX commands and references, because using the wrapper leads to certain characters being escaped.
For context, I am writing a document in Rmd, using occasional latex commands, and using a LaTeX template to knit into a pdf.

For instance, the text
Someone said something [@bibref-to-article_2024], and I am doing something in \experiment{1} to test it.

(where \experiment{1} refers to a macro in the form of \providecommand{\experiment}[1]{experiment #1\@\xspace} in the latex template I am using)

turns into:
Someone said something [@bibref-to-article\_2024], and I am doing something in \\experiment{1} to test it.

These escape \ break my references and macros.

Is there a way to prevent them from being added?
Thanks!

Option to rewrap one sentence per line?

Different from the current behavior, yet it could fit the scope of the package.

I prefer each sentence of a document on a separate line. We need to search for dots, ignore those within R expressions, and wrap dots that are followed by a space and preceded by a lowercase letter.

Argument width is ignored and documentation is not consistent

Currently the documentation says that the addin uses a fixed line length of 80. However, in fact the code uses the (undocumented) option WrapRmd.width. The workhorse function str_rmd_wrap basically ignores the width parameter (as it does not pass it down to underlying functions).

library(WrapRmd)
text <- paste(rep(LETTERS, 3), collapse=" ")
str_rmd_wrap(text, 10)
# [1] "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N\nO P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
op <- options(WrapRmd = 10)
str_rmd_wrap(text)
# [1] "A B C D E\nF G H I J\nK L M N O\nP Q R S T\nU V W X Y\nZ A B C D\nE F G H I\nJ K L M N\nO P Q R S\nT U V W X\nY Z A B C\nD E F G H\nI J K L M\nN O P Q R\nS T U V W\nX Y Z"
options(op)

This should not be too hard to fix and I will issue an PR soon.

experimental version unescapes things

This breaks bookdown references.

WrapRmd::str_rmd_wrap("Table \\@ref(tab:screening-counts) shows the numbers")
#> [1] "Table @ref(tab:screening-counts) shows the numbers"

Setting WrapRmd.width

`> options$WrapRmd.width = 150
Error in options$WrapRmd.width = 150 :
object of type 'closure' is not subsettable

wrap_rmdaddin(options(WrapRmd.width) = 120)
Error: unexpected '=' in "wrap_rmdaddin(options(WrapRmd.width) ="
options(WrapRmd.width) = 120
Error in options(WrapRmd.width) = 120 : object 'WrapRmd.width' not found
WrapRmd::options(WrapRmd.width) = 120
Error in WrapRmd::options(WrapRmd.width) = 120 :
object 'WrapRmd.width' not found
options(WrapRmd.width) = 120
Error in options(WrapRmd.width) = 120 : object 'WrapRmd.width' not found
WrapRmd::options(WrapRmd.width) = 120
Error in WrapRmd::options(WrapRmd.width) = 120 :
object 'WrapRmd.width' not found`

I assume the error is with me, where to set this option?

EDIT: I apologize if this is not appropriate use of issue, I am otherwise not aware where I should ask this, but I would assume I am not the only one for which this is not clear.

Ignore code blocks and yaml

It seems that code blocks and yaml are being wrapped, for instance:

```{r}
layout(
   subplot(p2),
   yaxis2 = list(title = ""), 
  yaxis3 = list(title = "")
)
```

becomes

```{r} layout( subplot(p2), yaxis2 = list(title = ""), yaxis3 = list(title =
"") ) ```

Would it be possible to ignore code chunks and yaml?

Unwanted breaks in code when comma

This lines is not properly wrapped:

The result is presented in the `r fig_nums("GS.DiaMoyEchvskME", display="cite")`.

It cuts it after the comma.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.