You know how to build a Rmardown, you were told 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? Follow the “Rmd-first” method and let {fusen} guide you to build a documented and tested package, to ensure the sustainability and sharing of your work.
You are one RMarkdown away from building a package!
Table of Contents
remotes::install_github("ThinkR-open/fusen")
- Create and open a new directory or a new RStudio project
- Choose your directory/package name like
my.package
for instance
- Choose your directory/package name like
- Run the following code directly in the console to add the templated Rmd in the project and open it:
dev_file <- fusen::add_dev_history(open = TRUE)
- In the Rmd template, run the first chunks asking to describe your package and license it
- They look like these lines of code:
fill_description(fields = list(Title = "My Awesome Package"))
usethis::use_mit_license("Sébastien Rochette")
- Write your analysis and functionnalities following the Rmd template
- 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
- Run the following code to transform the templated Rmd as a package
- This will open the vignette created
fusen::inflate(rmd = dev_file, name = "my-functionnality", check = TRUE)
That’s it! You built a package!
Let’s test it now:
- Install your package locally
remotes::install_local()
- Restart your R session to clean environment
- You can restart your RStudio session to let appear the “Build” tab panel
- Test functions of your package
my.package::my_median(1:12)
- Test the correct documentation of the package with its dedicated website
# Build {pkgdown} to test it
pkgdown::build_site()
# > See references and articles
# Hide output from package and git
usethis::use_build_ignore("docs")
usethis::use_git_ignore("docs")
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.
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