9 R Package Development in VSCode
R package development, VSCode for R packages, Roxygen2 in VSCode, devtools for R packages
9.1 Introduction
Developing R packages is a key aspect of sharing reusable R code and improving your programming skills. Visual Studio Code (VSCode) provides a streamlined environment that makes package development efficient and intuitive. With the right extensions and settings, VSCode can support every aspect of R package creation, from writing functions to documentation and testing. In this chapter, we will explore how to use VSCode for developing R packages.
9.2 Prerequisites
Install essential tools for R programming in VSCode as outlined in the previous chapters (Chapter 2 and Chapter 3).
Ensure you have the
devtools
androxygen2
packages installed in R for package development. Otherwise, install them using:install.packages("devtools") install.packages("roxygen2")
Note that devtools
is a development package that simplifies package creation, documentation, and testing, while roxygen2
is used for documenting functions.
9.3 Creating a New R Package
Follow these steps:
Create a Package Directory: Use the
create()
function fromdevtools
to set up a new package structure. Run the following in an R terminal:::create("path/to/your/package") devtools
This function will create a directory with the necessary structure for an R package, including folders like
R/
for functions andman/
for documentation.Open the Directory in VSCode: In VSCode, click on
File > Open Folder...
and select the package directory. This allows you to edit your package files, add functions, and write documentation.
9.4 Writing Functions and Documentation
Once your package structure is ready, you can start writing functions in the R/
folder. VSCode makes this process efficient with features like IntelliSense and linting.
Function Development: Create a new
.R
file in theR/
folder. Write your functions as you would in any R script. VSCode’s IntelliSense will provide suggestions for function names and arguments, helping you avoid errors.Documentation with Roxygen2: Document your functions using
roxygen2
comments. Add comments starting with#'
above each function to describe its purpose, parameters, and return values. For example:#' Calculate the Mean of a Vector #' #' This function calculates the arithmetic mean of a given numeric vector. #' #' @param x A numeric vector. #' @return The mean of the vector. #' @export <- function(x) { calculate_mean mean(x, na.rm = TRUE) }
After documenting, run
devtools::document()
to generate.Rd
files in theman/
folder.
9.5 Testing Your Package
Testing is crucial for ensuring your package works as expected. VSCode and R provide tools to facilitate unit testing:
Testthat Package: Use the
testthat
package for writing unit tests. Install it with:install.packages("testthat")
Create a
tests/
folder in your package directory and add test files using the following structure:test_that("calculate_mean works correctly", { expect_equal(calculate_mean(c(1, 2, 3)), 2) expect_equal(calculate_mean(c(NA, 2, 3)), 2.5) })
Running Tests: Run tests using
devtools::test()
. VSCode’s integrated terminal makes it easy to execute commands without leaving the editor.
9.6 Building and Installing the Package
To build your package and create a compressed file (.tar.gz
), run:
::build() devtools
This file can be shared and installed by others using install.packages("yourpackage.tar.gz", repos = NULL, type = "source")
.
For installing and testing your package locally:
::install() devtools
This command will install your package, making it available for use on your system.
9.7 Debugging and Troubleshooting
VSCode provides several features that make debugging your R package easier:
R Terminal Integration: You can test functions interactively in the R terminal by loading your package with
devtools::load_all()
. This loads your package into the R environment without the need for reinstallation, making it easier to test changes.Breakpoints and Debugging: While VSCode’s debugging tools are not as integrated with R as they are with other languages, you can use R’s native debugging functions (
browser()
,trace()
, etc.) to investigate issues in your code.
9.8 Version Control with Git
Using Git for version control is an essential part of modern software development. VSCode has built-in Git support, allowing you to:
- Track Changes: Monitor changes in your R package files directly from VSCode’s Source Control tab.
- Commit and Push: Commit your changes and push them to a remote repository (e.g., GitHub) for collaboration or backup.
- Branching: Create and switch between branches to work on different features or fixes independently.
9.9 Example Package Development Workflow
- Create the Package Structure: Use
devtools::create()
to generate a new package folder. - Write Functions: Add your R scripts to the
R/
directory and document them usingroxygen2
. - Test Your Code: Write unit tests using
testthat
and store them intests/testthat/
. - Document and Build: Run
devtools::document()
to create documentation, anddevtools::build()
to build the package. - Install and Test Locally: Install your package with
devtools::install()
, then test the functions interactively.
Read more about R package development in the official documentation.
9.10 Conclusion
Developing R packages in VSCode is a smooth and rewarding experience with the right tools. By leveraging extensions like vscode-R
and packages such as devtools
and roxygen2
, you can create well-documented, thoroughly tested R packages that are easy to share and maintain. VSCode’s integrated terminal, version control, and rich editing features make it an excellent environment for both novice and experienced R developers.
Ready to take your R programming skills further? In the next chapter, we will explore using Git for effective version control, ensuring that your codebase remains organized and collaborative.