{fusen} is now available on CRAN!

Pictures of multiple origamis boxes and animals
Tags : development, package, Rmarkdown
Date :

With first version of {fusen} 0.2.4 now available on CRAN, you do not have any excuses not to try it in your daily workflows.

install.packages("fusen")

Add new documented and tested functionalities in an existing package

origami box in blue with eyes

If you already develop packages, {fusen} is for you.

Developing a package requires to write some code and examples in multiple places: “R/”, “tests/”, “vignettes/”. With {fusen}, you can write everything in the same place.

  • Open a project for one of your already existing package
    • Commit your previous state if you are afraid of {fusen}
    • If you are not sure enough to do it on an existing package, then go down to “Build a package from a reproducible example”
  • Run in the Console : fusen::add_dev_history(name = "additional")
    • A Rmd file appears in “dev/dev_history.Rmd”. Open it.
  • Fill it as if each chunk was independant from each other. As if you were writing in different files.
  • Run command of the last chunk: fusen::inflate(rmd = "dev/dev_history.Rmd", name = "My Vignette Name")

That’s it!
You added a new function in your package, along with example, test and a new vignette.

Create your package as easily as writing a Rmarkdown file

origami box in yellow

If you want to try to develop your first packages, {fusen} is for you.

You may think that writing packages is for advanced developers who want to share their work on CRAN. You’re wrong!
Packages is for R users who use the same script in multiple project. Maybe you already created a R file that you source each time you need it. But is this code tested and documented? How can you share it with your colleagues each time there is a new feature? How can other user propose some modifications without breaking all your already existing workflows?
Create a package.

With {fusen}, the step between writing a Rmd file with complementary script, and writing a package is smaller than what you think. Go to “Build a package from a reproducible example” section and try it. You will build a package from a single Rmarkdown file.

Use the robustness of R packages for your analyses

origami box in pink

If you want to try writing data analyses in a package, {fusen} is for you.

There are multiple more or less recommended way of developing a data analysis in R so that your code is reproducible and shareable. At ThinkR, we think that everything should be a package. Why? Because there are multiple nice tools developed around packages that will ensure the robustness of your work. This will also encourage you to document your work so that other users will be able to understand, adapat and reproduce it on their own data. Isn’t it what science is about? Sharing work and ideas with others?

If you already use Rmarkdown in your work because you care about reproducibility, {fusen} will help you structure your Rmd and inflate it as a package in one command. Go to “Build a package from a reproducible example” section and try it!

Build a package from a reproducible example

hex log of fusen package

This is the example of the {fusen} README on GitHub

  • Create and open a new directory or a new project
  • Add the templated Rmd inside the project and open the file
dev_file <- fusen::add_dev_history(open = TRUE)
  • Run the first chunk 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 functionnalities
    • 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!

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)
  • Check that the documentation is correct by creating locally the dedicated website of the package.
# 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")

To go further

I highly recommend to combine your packages with git. Use usethis::use_git() in a development chunk and commit your work to keep track of your modifications.


About the author

Sébastien Rochette

Sébastien Rochette

Modélisateur, formateur R, joueur de cartographies


Leave a Reply


Also read