Intro to the namer package

M. Salmon

2019-12-16

The goal of namer is to name the chunks of R Markdown files. It’s your safety net when you’ve (willingly) forgotten to name most chunks of all R Markdown files in a folder. namer does not give meaningful labels to your chunks, but it gives them labels that won’t change depending on their position like the automatic knitr:::unnamed_chunk function does when knitting. So you can e.g. shuffle your chunks and not loose their cache, or more easily debug over a whole folder!

For context about why you should name your R Markdown chunks, read this blog post.

Check out this real life example, result of running namer::name_dir_chunks("pres").

Use

This is a basic example which shows you how to solve a common problem. The “test” folder first contains R Markdown files with unnamed chunks. After running name_dir_chunks, they’re all named, with names using the filenames as basis.

temp_dir <- tempdir()
fs::dir_copy(system.file("examples", package = "namer"),
            temp_dir)
 # this is an example file that'd fail
fs::file_delete(file.path(temp_dir, "example4.Rmd"))
name_dir_chunks(temp_dir)
if(interactive()){
  file.edit(file.path(temp_dir, "example1.Rmd"))
}

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.

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.

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.