Skip to content

Commit

Permalink
Merge 800cc76 into eb5768d
Browse files Browse the repository at this point in the history
  • Loading branch information
statnmap committed Apr 5, 2021
2 parents eb5768d + 800cc76 commit 0e709bd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Authors@R:
role = "ctb",
email = "patrick.schratz@gmail.com",
comment = c(ORCID = "0000-0003-0748-6624")))
Description: It names the 'R Markdown' chunks of files based on
the filename.
Description: It names the 'R Markdown' chunks of files based on the
filename.
License: GPL-3
URL: https://github.com/lockedata/namer, https://itsalocke.com/namer/
BugReports: https://github.com/lockedata/namer/issues
Expand All @@ -53,4 +53,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
RoxygenNote: 7.1.1
15 changes: 9 additions & 6 deletions R/name_chunks.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#'
#' @template path
#' @template unname
#' @template prefix
#'
#' @export
#'
Expand All @@ -21,7 +22,7 @@
#' file.edit(temp_file_path)
#' }
#' file.remove(temp_file_path)
name_chunks <- function(path, unname = FALSE){
name_chunks <- function(path, unname = FALSE, prefix){
# read the whole file
lines <- readLines(path)

Expand All @@ -48,13 +49,15 @@ name_chunks <- function(path, unname = FALSE){

# act only if needed!
if(no_unnamed > 0){
# create new chunk names
filename <- fs::path_ext_remove(path)
filename <- fs::path_file(filename)
if (missing(prefix)) {
# create new chunk names
filename <- fs::path_ext_remove(path)
prefix <- fs::path_file(filename)
}
# support for bookdown text references by removing underscores
# https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#fnref5
filename <- clean_latex_special_characters(filename)
new_chunk_names <- glue::glue("{filename}-{1:no_unnamed}")
prefix <- clean_latex_special_characters(prefix)
new_chunk_names <- glue::glue("{prefix}-{1:no_unnamed}")
new_chunk_names <- as.character(new_chunk_names)
# and write to which line they correspond
names(new_chunk_names) <- unique(unnamed$index)
Expand Down
16 changes: 15 additions & 1 deletion R/name_chunks_addin.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
name_chunks_addin <- function(){
name_chunks_file_addin <- function(){
name_chunks(path = rstudioapi::selectFile(filter = "R Markdown Files (*.Rmd)"))
}

name_chunks_addin <- function(){
prefix <- rstudioapi::showPrompt(title = "Chunk names prefix",
message = "Leave empty to use filename (Default)", default = "")
if (prefix == "") {
name_chunks(path = rstudioapi::getActiveDocumentContext()$path)
} else {
name_chunks(path = rstudioapi::getActiveDocumentContext()$path, prefix = prefix)
}
}

name_chunks_directory_addin <- function(){
namer::name_dir_chunks(rstudioapi::selectDirectory())
}
12 changes: 11 additions & 1 deletion inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
Name: Label Rmd Chunks
Name: Label Current Rmd Chunks
Description: labels unnamed chunk of the active R Markdown document, based on the filename.
Binding: name_chunks_addin
Interactive: false

Name: Label Rmd Chunks from File
Description: labels unnamed chunk of a chosen R Markdown document, based on the filename.
Binding: name_chunks_file_addin
Interactive: true

Name: Label Rmd Chunks from Directory
Description: labels unnamed chunks of all R Markdown document in a Directory.
Binding: name_chunks_directory_addin
Interactive: true
1 change: 1 addition & 0 deletions man-roxygen/prefix.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#' @param prefix prefix to use to name chunks. Default to cleaned name of the Rmd.
4 changes: 3 additions & 1 deletion man/name_chunks.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-name_chunks.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ test_that("renaming works", {
file.remove(temp_file_path)
})

test_that("renaming with prefix works", {
temp_file_path <- file.path(tmp, "test.Rmd")

file.copy(system.file("examples", "example1.Rmd", package = "namer"),
temp_file_path)
name_chunks(temp_file_path, prefix = "my-prefix")
lines <- readLines(temp_file_path)
chunk_info <- get_chunk_info(lines)
expect_true(all(grepl("my-prefix", chunk_info$name[-1])))
file.remove(temp_file_path)
})

test_that("unnaming is advised when needed", {
temp_file_path <- file.path(tmp, "example4.Rmd")

Expand Down

0 comments on commit 0e709bd

Please sign in to comment.