Skip to content

Commit fb82b7d

Browse files
committed
create the package
1 parent 6f2b886 commit fb82b7d

14 files changed

+226
-674
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
demo.gif

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ vignettes/*.pdf
1717

1818
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
1919
.httr-oauth
20+
.Rproj.user

DESCRIPTION

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Package: WrapRmd
2+
Title: RStudio Addin for Wrapping RMarkdown Paragraphs
3+
Version: 0.0.0.9000
4+
Authors@R: person("Tristan", "Mahr", email = "[email protected]", role = c("aut", "cre"))
5+
Description: Provides an RStudio addin for wrapping paragraphs in an RMarkdown document without inserting linebreaks into spans of inline R code.
6+
Depends:
7+
R (>= 3.2.3)
8+
License: MIT + file LICENSE
9+
LazyData: true
10+
Imports:
11+
stringr,
12+
rstudioapi,
13+
stringi
14+
RoxygenNote: 5.0.1

LICENSE

+2-674
Large diffs are not rendered by default.

NAMESPACE

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(str_rmd_wrap)
4+
export(wrap_rmd_addin)
5+
import(stringr)

R/WrapRmd-package.r

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#' WrapRmd
2+
#'
3+
#' Provides an RStudio addin for wrapping paragraphs in an RMarkdown document
4+
#' without inserting linebreaks into spans of inline R code.
5+
#'
6+
#' @name WrapRmd
7+
#' @docType package
8+
#' @import stringr
9+
NULL
10+
11+
12+
13+
#' Wrap text but don't insert lines breaks into inline R code
14+
#' @param string a string to wrap
15+
#' @param width,indent,exdent other arguments for stringr::str_wrap
16+
#' @return a wrapped copy of the string
17+
#'
18+
#' @details This function finds all inline R code spans in a string, replaces
19+
#' all non-word characters and spaces the inline R spans with underscores,
20+
#' wraps the string and restores the original inline R spans.It's not going to
21+
#' handle any inline code that uses backticks.
22+
#' @export
23+
str_rmd_wrap <- function(string, width = 80, indent = 0, exdent = 0) {
24+
stopifnot(length(string) == 1)
25+
output <- string
26+
27+
inline_code <- str_extract_all(string, "(`r)( )([^`]+`)") %>% unlist
28+
29+
# Just wrap if no code
30+
if (length(inline_code) == 0) {
31+
return(str_wrap(string, width, indent, exdent))
32+
}
33+
34+
# Make R code chunks into to long words
35+
spaceless_code <- inline_code %>% str_replace_all("\\W| ", "_")
36+
37+
for (i in seq_along(inline_code)) {
38+
output <- str_replace(output, coll(inline_code[i]), spaceless_code[i])
39+
}
40+
41+
# Wrap
42+
output <- str_wrap(output, width, indent, exdent)
43+
44+
# Put original code chunks back
45+
for (i in seq_along(inline_code)) {
46+
output <- stringi::stri_replace_first_coll(
47+
str = output,
48+
pattern = spaceless_code[i],
49+
replacement = inline_code[i])
50+
}
51+
52+
output
53+
}
54+
55+
56+
#' Wrap text but don't insert lines breaks into inline R code
57+
#'
58+
#' Call this addin to wrap paragraphs in an R Markdown document
59+
#' @export
60+
wrap_rmd_addin <- function() {
61+
context <- rstudioapi::getActiveDocumentContext()
62+
selection <- context$selection
63+
text <- unlist(selection)["text"]
64+
rstudioapi::insertText(str_rmd_wrap(text))
65+
}

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
11
# WrapRmd
2+
3+
An [RStudio Addin](https://rstudio.github.io/rstudioaddins/) to wrap paragraphs
4+
of RMarkdown text.
5+
6+
## Overview
7+
8+
Here is some nice looking R Markdown:
9+
10+
```
11+
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`.
12+
```
13+
14+
You highlight the text, and hit ctrl/cmd + shift + / to wrap the text and get:
15+
16+
```
17+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum a `r
18+
max(iris$Sepal.Length)`, viverra nisl at, luctus ante = `r length(letters) * 2 +
19+
100`.
20+
```
21+
22+
This RStudio Addin wraps text, but doesn't insert line breaks into inline R
23+
code, yielding:
24+
25+
```
26+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
27+
a `r max(iris$Sepal.Length)`, viverra nisl at, luctus ante =
28+
`r length(letters) * 2 + 100`.
29+
```
30+
31+
![An animation of the above](demo.gif)
32+
33+
34+
## Notes
35+
36+
Install with `devtools::install_github("tjmahr/WrapRmd")`
37+
38+
Currently, the package wraps lines using a maximum line width of 80 characters.

WrapRmd.Rproj

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: No
4+
SaveWorkspace: No
5+
AlwaysSaveHistory: No
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: knitr
13+
LaTeX: XeLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageUseDevtools: Yes
20+
PackageInstallArgs: --no-multiarch --with-keep.source
21+
PackageRoxygenize: rd,collate,namespace,vignette

demo.gif

25.5 KB
Loading

inst/rstudio/addins.dcf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name: Wrap Rmd
2+
Description: Wrap selected R Markdown text but don't insert lines breaks into inline R code
3+
Binding: wrap_rmd_addin
4+
Interactive: false

inst/test_strings.R

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library("stringr")
2+
3+
no_code <- "regular words on a line"
4+
str_rmd_wrap(no_code) %>% cat
5+
6+
# Simple case
7+
text <- "
8+
`r hello` and `r 1 + 1` and `r 1 + b + b + c` and drop a line right here `r maybe_here` `r goodbye`
9+
"
10+
str_rmd_wrap(text) %>% cat
11+
12+
# Paragraph I got frustrated manually re-wrapping
13+
original_test <- "
14+
Ignoring the quadratic and cubic features of the growth curve, the linear time^1^ term estimated an increase of `r ms_delta_bin` logits per 50 ms. At 0 logits (.5 proportion units), where the logistic function is steepest, an increase of `r ms_delta_bin` logits corresponds to an increase of `r ms_prop_change_50` proportion units. At chance performance (.25 proportion units), this effect corresponds to an increase of `r ms_prop_change_25` proportion units.
15+
"
16+
17+
str_rmd_wrap(original_test) %>% cat
18+
19+
20+
gif_lines <- "
21+
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`.
22+
"
23+
24+
str_rmd_wrap(gif_lines) %>% cat

man/WrapRmd.Rd

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/str_rmd_wrap.Rd

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wrap_rmd_addin.Rd

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)