Skip to content

Commit

Permalink
add chunk addin
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Oct 10, 2020
1 parent b3054f2 commit b8a7dd3
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ S3method(mask_rx,default)
S3method(mask_rx,with_flair)
S3method(print,with_flair)
export("%>%")
export(chunk_addin)
export(decorate)
export(decorate_chunk)
export(decorate_code)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- `txt_style()` now accepts a `class` argument specifying a vector of classes to
be applied to the `<span>` of the decorated text (@gadenbuie, #18).

- `chunk_addin()` RStudio addin have been added. add `decorate()` chunk after
selected chunk when used.

## Bugs and fixes

* NULL document types are now treated as default html
Expand Down
75 changes: 75 additions & 0 deletions R/chunk_addin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' Add decorate chunk after chunk
#'
#' Call this function as an addin to add a decorate chunk after a selected
#' chunk.
#'
#' @export
chunk_addin <- function() {

# Gets The active Documeent
ctx <- rstudioapi::getActiveDocumentContext()

# Checks that a document is active
if (!is.null(ctx)) {

# Extracts selection as a string
selected_text <- ctx$selection[[1]]$text

# modify string
selected_text <- add_flair_chunk(selected_text)

# replaces selection with string
rstudioapi::modifyRange(ctx$selection[[1]]$range, selected_text)
}
}

add_flair_chunk <- function(x) {
x <- stringr::str_split(x, "\n")[[1]]
header_loc <- stringr::str_detect(x, "```\\{r")
end_loc <- stringr::str_detect(x, "^```$")

if (!any(header_loc) || !any(end_loc)) {
stop("No chunk detected")
}

chunk_header <- x[header_loc][1]

chunk_header <- stringr::str_remove(chunk_header, "```\\{r *,{0,1} *")

chunk_header <- stringr::str_remove(chunk_header, "\\}")

chunk_params <- stringr::str_split(chunk_header, ", *")[[1]]

if (all(stringr::str_detect(chunk_params, "=")) || chunk_params == "") {
stop("Chunk must be named")
}

chunk_name <- chunk_params[!stringr::str_detect(chunk_params, "=")]

flair_chunk <- c(
'',
glue::glue('```{r [chunk_name]_flair, echo = FALSE}',
.open = "[", .close = "]"),
glue::glue('decorate("{chunk_name}")'),
'```'
)

chunk_params <- chunk_params[!stringr::str_detect(chunk_params, "include")]

chunk_params <- c(chunk_params, "include = FALSE")

x[which(header_loc)[1]] <- paste0("```{r ",
paste(chunk_params, collapse = ", "),
"}")

res <- c(
x[seq(1, which(end_loc)[1])],
flair_chunk
)

if (which(end_loc)[1] < length(x)) {
res <- c(res, x[seq(which(end_loc)[1] + 1, length(x))])
}

stringr::str_c(res, collapse = "\n")
}
5 changes: 5 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name: Add decorate flair chunk
Description: Add decorate flair chunk
Binding: chunk_addin
Interactive: false

12 changes: 12 additions & 0 deletions man/chunk_addin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 144 additions & 0 deletions tests/testthat/test-chunk_addin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
test_that("add_flair_chunk works", {
expect_equal(
add_flair_chunk(
'
```{r name}
mean(x)
```
'
),
'
```{r name, include = FALSE}
mean(x)
```
```{r name_flair, echo = FALSE}
decorate("name")
```
'
)

expect_equal(
add_flair_chunk(
'
```{r name}
mean(x)
```
'
),
'
```{r name, include = FALSE}
mean(x)
```
```{r name_flair, echo = FALSE}
decorate("name")
```
'
)

})

test_that("add_flair_chunk only affects first chunk", {
expect_equal(
add_flair_chunk(
'
```{r name}
mean(x)
```
```{r sum}
sum(x)
```
'
),
'
```{r name, include = FALSE}
mean(x)
```
```{r name_flair, echo = FALSE}
decorate("name")
```
```{r sum}
sum(x)
```
'
)

})

test_that("add_flair_chunk flips include = TRUE", {
expect_equal(
add_flair_chunk(
'
```{r name, include = TRUE}
mean(x)
```
```{r sum}
sum(x)
```
'
),
'
```{r name, include = FALSE}
mean(x)
```
```{r name_flair, echo = FALSE}
decorate("name")
```
```{r sum}
sum(x)
```
'
)

})

test_that("add_flair_chunk throws errors", {
expect_error(
add_flair_chunk(
'
```{r name}
mean(x)
'
)
)

expect_error(
add_flair_chunk(
'
```{r}
sum(x)
```
'
)
)

expect_error(
add_flair_chunk(
'
```{r, eval=FALSE}
sum(x)
```
'
)
)

})

0 comments on commit b8a7dd3

Please sign in to comment.