- Notifications
You must be signed in to change notification settings - Fork13
Tools to deal with dependencies in scripts, Rmd and packages
License
ThinkR-open/attachment
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The goal of attachment is to help to deal with package dependenciesduring package development. It also gives useful tools to install orlist missing packages used inside Rscripts or Rmds.
When building a package, we have to add@importFrom in ourdocumentation orpkg::fun in the R code. The most important is not toforget to add the list of dependencies in the “Imports” or “Suggests”package lists in the DESCRIPTION file.
Why do you have to repeat twice the same thing ?
And what happens when you remove a dependency for one of your functions? Do you really want to run a “Find in files” to verify that you do notneed this package anymore ?
Let {attachment} help you ! This reads your NAMESPACE, your functions inR directory and your vignettes, then update the DESCRIPTION fileaccordingly. Are you ready to be lazy ?
See full documentation realized using {pkgdown} athttps://thinkr-open.github.io/attachment/
CRAN version
install.packages("attachment")Development version
install.packages('attachment',repos= c('https://thinkr-open.r-universe.dev','https://cloud.r-project.org'))
What you really want is to fill and update your description file alongwith the modifications of your documentation. Indeed, only the followingfunction will really be called. Use and abuse during the development ofyour package !
attachment::att_amend_desc()
{attachment} detects all calls tolibrary(pkg),@importFrom pkg fun,pkg::fun() in the different classical directories of your R package,then list them in the correct “Imports” or “Suggests” category in theDESCRIPTION file, according to their position in the package.
If you want to add extra packages like {pkgdown} or {covr} that are notlisted in any script in your package, a call for your developmentpackages would be:
attachment::att_amend_desc(extra.suggests= c("pkgdown","covr"),update.config=TRUE)
Note theupdate.config = TRUE parameter that will save the parametersused in the call ofatt_amend_desc() to the package configurationfile: “dev/config_attachment.yaml”.
If you runatt_amend_desc() a second time afterwards, directly fromthe console, it will use the last set of parameters extracted from theconfiguration file.
Indeed, we recommend to store the complete command line in a“dev/dev_history.R” file to update and run it when needed. If theparameters do not change, you can runattachment::att_amend_desc()directly in the console, wherever you are, it will use the configurationfile.
If you would like to detect the sources of your installations so thatyou can add dependencies in the “Remotes” field of your DESCRIPTIONfile, to mimic your local installation, you will use:
attachment::set_remotes_to_desc()
# Copy example package in a temporary directorytmpdir<- tempfile(pattern="fakepkg")dir.create(tmpdir)file.copy(system.file("dummypackage",package="attachment"),tmpdir,recursive=TRUE)#> [1] TRUEdummypackage<- file.path(tmpdir,"dummypackage")# browseURL(dummypackage)# Fill the DESCRIPTION file automatically# `inside_rmd` is specifically designed here to allow to run this command line in the "Readme.Rmd" filedesc_file<-attachment::att_amend_desc(path=dummypackage,inside_rmd=TRUE,update.config=TRUE)#> 'update.config' was set to TRUE, hence, 'use.config' was forced to FALSE#> Saving attachment parameters to yaml config file#> Updating dummypackage documentation#> Setting `RoxygenNote` to "7.3.2"#> Writing 'NAMESPACE'#> ℹ Loading dummypackage#> Writing 'NAMESPACE'#> ℹ Loading dummypackage#> Package(s) Rcpp is(are) in category 'LinkingTo'. Check your Description file to be sure it is really what you want.# Add Remotes if you have some installedattachment::set_remotes_to_desc(path.d=desc_file)#> There are no remote packages installed on your computer to add to description#> NULL# Clean stateunlink(tmpdir,recursive=TRUE)
Find packages installed out of CRAN. This helps fill the “Remotes” fieldin DESCRIPTION file withset_remotes_to_desc().
Behind the scene, it usesfind_remotes().
- See the examples below if {fusen} is installed from GitHub
- Also works for GitLab, Bioconductor, Git, Local installations
# From GitHubremotes::install_github("ThinkR-open/fusen",quiet=TRUE,upgrade="never")attachment::find_remotes("fusen")#> $fusen#> [1] "ThinkR-open/fusen"
To quickly install missing packages from a DESCRIPTION file, use:
attachment::install_from_description()#> All required packages are installed
To quickly install missing packages needed to compile Rmd files or run Rscripts, use:
attachment::att_from_rmds(path=".") %>%attachment::install_if_missing()attachment::att_from_rscripts(path=".") %>%attachment::install_if_missing()
Functionattachment::create_dependencies_file() will create adependencies.R file ininst/ directory. This R script contains theprocedure to quickly install missing dependencies:
# Remotes ----# remotes::install_github("ThinkR-open/fcuk")# Attachments ----to_install<- c("covr","desc","devtools","glue","knitr","magrittr","rmarkdown","stats","stringr","testthat","utils")for (iinto_install) { message(paste("looking for",i))if (!requireNamespace(i)) { message(paste(" installing",i)) install.packages(i) }}
If you write a {bookdown} and want to publish it on Github using GitHubActions or GitLab CI for instance, you will need a DESCRIPTION file withlist of dependencies just like for a package. In this case, you can usethe function to description from import/suggest:att_to_desc_from_is().
usethis::use_description()# bookdown Imports are in Rmdsimports<- c("bookdown",attachment::att_from_rmds("."))attachment::att_to_desc_from_is(path.d="DESCRIPTION",imports=imports,suggests=NULL)
Then, install dependencies with
remotes::install_deps()
Of course, you can also use {attachment} out of a package to list allpackage dependencies of R scripts usingatt_from_rscripts() or Rmd/qmdfiles usingatt_from_rmds().
If you are running this inside a Rmd, you may need parameterinside_rmd = TRUE.
library(attachment)dummypackage<- system.file("dummypackage",package="attachment")att_from_rscripts(path=dummypackage)#> [1] "stats" "testthat" "dummypackage"att_from_rmds(path= file.path(dummypackage,"vignettes"),inside_rmd=TRUE)#> [1] "knitr" "rmarkdown" "glue"
Package {attachment} has vignettes to present the different functionsavailable. There is also a recommendation to have adev_history.R inthe root directory of your package. (Have a look atdev_history.Rin the present package)
vignette("a-fill-pkg-description",package="attachment")vignette("b-bookdown-and-scripts",package="attachment")vignette("use_renv",package="attachment")vignette("create-dependencies-file",package="attachment")
The vignettes are available on the {pkgdown} page, in the “Articles”menu:https://thinkr-open.github.io/attachment/
Please note that the attachment project is released with aContributorCode ofConduct.By contributing to this project, you agree to abide by its terms
About
Tools to deal with dependencies in scripts, Rmd and packages
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.
