Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument to **unname_all_chunks** specifying which chunks to unname #22

Merged
merged 9 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ Authors@R:
family = "Salmon",
role = "aut",
email = "maelle.salmon@yahoo.se",
comment = c(ORCID = "0000-0002-2815-0399")),
comment = structure("0000-0002-2815-0399", .Names = "ORCID")),
person(given = "Ellis",
family = "Valentiner",
role = "ctb"),
person(given = "Martin",
family = "Hadley",
role = "ctb",
comment = c(ORCID = "0000-0002-3039-6849")),
comment = structure("0000-0002-3039-6849", .Names = "ORCID")),
person(given = "Locke Data",
role = "fnd",
comment = "https://itsalocke.com"))
comment = "https://itsalocke.com"),
person(given = "Han",
family = "Oostdijk",
role = "ctb",
comment = structure("0000-0001-6710-4566", .Names = "ORCID"))
)
Description: It names the 'R Markdown' chunks of files based on the filename.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay ORCID 🎉

License: GPL-3
Encoding: UTF-8
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# namer (development version)
* unname_all_chunks now accepts argument `chunk_name_prefix` with the prefix of the chunknames to be unnamed (@HanOostdijk, #22)

# namer 0.1.4

Expand Down
32 changes: 27 additions & 5 deletions R/unname_all_chunks.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
#' @title Unname chunks in a single file
#'
#' @description Unname all chunks except the setup chunk, in a single file
#' @description Unname in a single file all chunks,
#' or alternatively only unname the chunknames with a given prefix.
#' In both cases, the chunk name "setup" is preserved, that chunk is never unnamed.
#'
#' @inherit name_chunks details
#'
#' @param path Path to file
#' @param chunk_name_prefix Character string with prefix of chunknames that will be removed. Default: NULL (indicating all chunknames will be removed except the one named `setup`)
#'
#' @export
#'
#' @examples
#' temp_file_path <- file.path(tempdir(), "test.Rmd")
#' # remove all chunklabels except the one named 'setup'
#' temp_file_path <- file.path(tempdir(), "test1.Rmd")
#' file.copy(system.file("examples", "example4.Rmd", package = "namer"),
#' temp_file_path,
#' overwrite = TRUE)
#' unname_all_chunks(temp_file_path)
#' if(interactive()){
#' file.edit(temp_file_path)
#' }
unname_all_chunks <- function(path){
#' # remove all chunk labels starting with 'example4'
#' temp_file_path <- file.path(tempdir(), "test2.Rmd")
#' file.copy(system.file("examples", "example4.Rmd", package = "namer"),
#' temp_file_path,
#' overwrite = TRUE)
#' unname_all_chunks(temp_file_path,chunk_name_prefix='example4')
#' if(interactive()){
#' file.edit(temp_file_path)
#' }
unname_all_chunks <- function(path,chunk_name_prefix=NULL){
# read the whole file
lines <- readLines(path)

Expand All @@ -29,8 +42,17 @@ unname_all_chunks <- function(path){
return(invisible("TRUE"))
}

# preserve the setup label, delete the others
chunk_headers_info$name[chunk_headers_info$name != "setup"] <- ""
if ( is.null(chunk_name_prefix)){
# preserve the setup label, delete the others
chunk_headers_info$name[chunk_headers_info$name != "setup"] <- ""
} else {
# preserve labels not starting with chunk_name_prefix
del_labels <- strtrim(chunk_headers_info$name,nchar(chunk_name_prefix)) %in%
chunk_name_prefix
setup_label <- !(chunk_headers_info$name %in% 'setup')
del_labels <- del_labels & setup_label
chunk_headers_info$name[del_labels] <- ""
}

newlines <- re_write_headers(chunk_headers_info)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ file.edit("test/example1.Rmd")

There's also `name_chunks` for use on a single R Markdown file; and `unname_all_chunks` to unname all chunks of a single R Markdown file which can be useful when cleaning your chunk labels.

By default `unname_all_chunks` unnames all chunks with exception of the 'setup' chunk. By using the argument `chunk_name_prefix` one can indicate the prefix of the labels that will be unnamed. Useful when one refers to a label by using chunk option `ref.label` so that it is inconvenient when that labeel is unnamed. By setting `chunk_name_prefix` equal to 'the filename with extension stripped' followed with a '-' (dash) only the labels generated by `name_chunks` will be unnamed.

If you're working with RStudio, installing the package will have installed an addin for labelling chunks of *any R Markdown document you select*.

**When using `namer`, please check the edits before pushing them to your code base. Such automatic chunk labelling is best paired with [version control](http://happygitwithr.com/).**
Expand Down
4 changes: 4 additions & 0 deletions inst/examples/example4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ plot(pressure)


Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

```{r sessioninfo}
sessionInfo()
```
20 changes: 17 additions & 3 deletions man/unname_all_chunks.Rd

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

67 changes: 66 additions & 1 deletion tests/testthat/test-unname_all_chunks.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context("test-unname_all_chunks")

test_that("unname_all_chunks works", {
test_that("unname_all_chunks works in case is.null(chunk_name_prefix) == TRUE", {
# check arg of tempdir
R_version <- paste(R.version$major,
R.version$minor,
Expand All @@ -17,6 +17,38 @@ test_that("unname_all_chunks works", {
lines <- readLines(temp_file_path)
chunk_info <- get_chunk_info(lines)

testthat::expect_identical(chunk_info$name[1],'setup')
testthat::expect_true(all(is.na(chunk_info$name[-1])))

rendering <- rmarkdown::render(temp_file_path)
testthat::expect_is(rendering, "character")

file.remove(temp_file_path)

basename <- fs::path_ext_remove(temp_file_path)

file.remove(paste0(basename, ".html"))
})

test_that("unname_all_chunks works in case is.null(chunk_name_prefix) == FALSE", {
# check arg of tempdir
R_version <- paste(R.version$major,
R.version$minor,
sep = ".")

skip_if_not(R_version >= "3.5.0")

temp_file_path <- file.path(tempdir(check = TRUE), "example4.Rmd")

file.copy(system.file("examples", "example4.Rmd", package = "namer"),
temp_file_path)
unname_all_chunks(temp_file_path,chunk_name_prefix='example4')

lines <- readLines(temp_file_path)
chunk_info <- get_chunk_info(lines)

testthat::expect_identical(chunk_info$name[1],'setup')
testthat::expect_identical(chunk_info$name[6],'sessioninfo')
testthat::expect_true(all(is.na(chunk_info$name[2:5])))

rendering <- rmarkdown::render(temp_file_path)
Expand All @@ -28,3 +60,36 @@ test_that("unname_all_chunks works", {

file.remove(paste0(basename, ".html"))
})

test_that("unname_all_chunks works in case chunk_name_prefix == 'setup' ", {
# check arg of tempdir
R_version <- paste(R.version$major,
R.version$minor,
sep = ".")

skip_if_not(R_version >= "3.5.0")

temp_file_path <- file.path(tempdir(check = TRUE), "example4.Rmd")

file.copy(system.file("examples", "example4.Rmd", package = "namer"),
temp_file_path)
unname_all_chunks(temp_file_path,chunk_name_prefix='setup')

lines <- readLines(temp_file_path)
chunk_info <- get_chunk_info(lines)

testthat::expect_identical(chunk_info$name[1],'setup')
testthat::expect_identical(chunk_info$name[3],'example4-1')
testthat::expect_identical(chunk_info$name[4],'example4-1-bis')
testthat::expect_identical(chunk_info$name[6],'sessioninfo')
testthat::expect_true(all(is.na(chunk_info$name[c(2,5)])))

rendering <- rmarkdown::render(temp_file_path)
testthat::expect_is(rendering, "character")

file.remove(temp_file_path)

basename <- fs::path_ext_remove(temp_file_path)

file.remove(paste0(basename, ".html"))
})
17 changes: 16 additions & 1 deletion vignettes/namer.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ if(interactive()){
fs::dir_delete(temp_dir)
```

There's also `name_chunks` for use on a single R Markdown file; and `unname_all_chunks` to unname all chunks of a single R Markdown file which can be useful when cleaning your chunk labels.
There's also `name_chunks` for use on a single R Markdown file; and `unname_all_chunks` to unname all chunks of a single R Markdown file which can be useful when cleaning your chunk labels.

By default `unname_all_chunks` unnames all chunks with exception of the 'setup' chunk. By using the argument `chunk_name_prefix` one can indicate the prefix of the labels that will be unnamed. Useful when one refers to a label by using chunk option `ref.label` so that it is inconvenient when that label is unnamed. By setting `chunk_name_prefix` equal to 'the filename with extension stripped' followed with a '-' (dash) only the labels generated by `name_chunks` will be unnamed. In the following example the chunks labeled 'setup' and 'sessioninfo' will not be unnamed.

```r
temp_file_path <- file.path(tempdir(check = TRUE), "example4.Rmd")

file.copy(system.file("examples", "example4.Rmd", package = "namer"),
temp_file_path)
unname_all_chunks(temp_file_path,chunk_name_prefix='example4-')
if(interactive()){
file.edit(temp_file_path)
}

file.remove(temp_file_path)
```

**When using `namer`, please check the edits before pushing them to your code base. Such automatic chunk labelling is best paired with [version control](http://happygitwithr.com/).**