Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
You can read the original post in its original format on Rtask website by ThinkR here: {fusen}: Create a package from a simple RMarkdown file
You know how to build a Rmd, you were said or would like to put your work in a R package, but you think this is too much work? You do not understand where to put what and when? What if writing a Rmd was the same as writing a package? Let {fusen} help you with this.
You are one RMarkdown away from building a package!
- Install {fusen} from GitHub
remotes::install_github("ThinkR-open/fusen")
- Create and open a new directory or a new project
- Add the Rmd template inside the project and open the file
dev_file <- fusen::add_dev_history(open = TRUE)
- Run the first chunks asking to describe your package and license it
fill_description(fields = list(Title = "My Awesome Package")) usethis::use_mit_license("Sébastien Rochette")
- Follow the guide to write your analysis and functionalities
- You probably develop them with a few examples and tests
- For the first time, you can leave the code of the Rmd as it is, this is already the content of a package that works
- Transform this Rmd as a package
fusen::inflate(rmd = dev_file, name = "my-functionnality", check = TRUE)
That’s it! You built a package!
Who is {fusen} for?
When you write a RMarkdown file (or a vignette), you create a documentation for your analysis (or package). Inside, you write some functions, you test your functions with examples and you maybe write some unit tests to verify the outputs. This is even more true if you follow this guide : ‘Rmd first’: When development starts with documentation.
After that, you need to move your functions and scripts in the correct place. Well, let {fusen} do that for you!
{fusen} is first addressed to people who never wrote a package before but know how to write a RMarkdown file. Understanding package infrastructure and correctly settling it can be frightening. {fusen} helps you do the first steps.
{fusen} is also addressed to more advanced developers who are fed up with switching between R files, tests files, vignettes. In particular, when changing arguments of a function, we need to change examples, unit tests in multiple places. Here, you can do it in one place. No risk to forget one.
Why is this package named {fusen} ?
A fusen is an origami. It is a piece of paper that you fold in a specific way so that at the end, you can magically inflate it to let a nice box appear.
Similarly, the {fusen} package uses one page of RMarkdown, that you fill in a specific way so that at the end, you can magically inflate()
it to let a nice package appear.
The RMarkdown template
-
- Follow the
"dev_history.Rmd"
template to write your documentation, build your functions and test your examples.- Chunk named
function
gets the code of a function - Chunk named
example
gets the code for examples of using the function. This will be used for function@examples
and will be kept for the vignette - Chunk named
tests
gets the code for unit testing - Chunk named
development
gets the code for development purposes, usually only used once like {usethis} functions
- Chunk named
- Follow the
- Inflate the template to transform it as a package with functions, unit tests and the current Rmd transformed as a vignette. And check.
Note that the "dev_history.Rmd"
template is indeed a working example.
Note also that {fusen} is itself created from the "dev_history.Rmd"
template available in its GitHub repository.
A reproducible example
- Build a package from Rmd template in a temporary directory
# Create a new project tmpdir <- tempdir() dummypackage <- file.path(tmpdir, "dummypackage") dir.create(dummypackage) # {fusen} steps # Add the template in your package dev_file <- fusen::add_dev_history(pkg = dummypackage, overwrite = TRUE) # Description fusen::fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) # Define License with use_*_license() usethis::use_mit_license("Sébastien Rochette") # You may need to execute inflate() in the console directly fusen::inflate(pkg = dummypackage, rmd = dev_file, name = "exploration")
- Test the correct documentation of the package by exploring its dedicated website: https://thinkr-open.github.io/fusen
# Build {pkgdown} to test it pkgdown::build_site(pkg = dummypackage) # > See references and articles # Hide output from package and git usethis::use_build_ignore("docs") usethis::use_git_ignore("docs")
Aknowledgments
- Thanks to Deemah who asked me to go further ‘Rmd first’ after my presentation at use’R 2019 in Toulouse: ‘The “Rmd first” method: when projects start with documentation’ (Video on Youtube: https://youtu.be/cB1BCxFbhtk).
- Thanks to @rundel and its package {parsermd} who helped me get back in this project with ease : https://github.com/rundel/parsermd
This post is better presented on its original ThinkR website here: {fusen}: Create a package from a simple RMarkdown file
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.