Skip to content

Commit

Permalink
Add prefix and new Addins (#40)
Browse files Browse the repository at this point in the history
* Allow to choose prefix for chunk names

issue #2

* Add new addins

- Name current Rmd file
- Name file chosen in a directory
- Name chunks from a folder
- issue #13

- Allow to use 'prefix' in the Addin for current file in relation to
issue #2

* Update Rproj

* Rebasing

---------

Co-authored-by: statnmap <[email protected]>
  • Loading branch information
csgillespie and statnmap authored Jan 23, 2025
1 parent 627b585 commit 16b692b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ VignetteBuilder:
Encoding: UTF-8
Language: en-GB
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
15 changes: 9 additions & 6 deletions R/name_chunks.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'
#' @template path
#' @template unname
#' @template prefix
#'
#' @export
#'
Expand All @@ -23,7 +24,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 @@ -50,13 +51,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.

1 change: 1 addition & 0 deletions namer.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 0a211baf-95cc-4ba1-a993-ce5368b7e03f

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
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 16b692b

Please sign in to comment.