my-project >
::init()
renv
::install("languageserver")
renv::install("jsonlite")
renv::install("nx10/httpgd") renv
Matthew DeHaven
March 31, 2024
The definitive guide to writing R packages: R Packages (2e)
Why would we want to do this?
Open a new folder in VS Code, I will call it “my-project”
Launch an R terminal, initialize renv
and install our VS Code extension packages
I am going to try to distinguish between what working directory you should use by labelling “my-project >” for the project directory.
devtools
packageNow we can create our package uisng devtools
You should see a new folder in your project called “myPackage”.
In addition, your terminal will have now changed to “my-project/myPackage>”
R pacakges have strict naming requirements. You can’t use any “_” or “-” or most other special characters. You can use numbers and “.”, but you can’t start with those symbols. I’d suggest sticking to Camel Case for package names.
Once you save it, open a new terminal with a working directory at your project level.
myfunc()
in your project.It didn’t work!
That’s because we haven’t listed myfunc()
to be an exported function of our package. The easiest way to do that is using roxygen2
’s comment headers.
This will update the documentation of your package. It generates a help file for the function and adds the function to the list of exported functions in the “NAMESPACE” file.
You should get a popup help file just like you would for a usual package.
If you call instead help(package = "myPackage")
you’ll get an index file of all the exported functions from your package.
check()
Whenever you are writing a package you should periodically run
This checks a lot of different things about your package