Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tools to deal with dependencies in scripts, Rmd and packages

License

NotificationsYou must be signed in to change notification settings

ThinkR-open/attachment

Repository files navigation

Coverage statusCRAN statusdownloadsR-CMD-checkCodecov test coverage

attachment

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/

Installation

CRAN version

install.packages("attachment")

Development version

install.packages('attachment',repos= c('https://thinkr-open.r-universe.dev','https://cloud.r-project.org'))

Declare all dependencies in DESCRIPTION during package development

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.

Declare extra dependencies for extra uses

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.

Automatically fill the “Remotes” field

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()

Example on a fake package

# 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)

More on finding Remotes repositories (non installed from CRAN)

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"

Find and install missing dependencies required for your R scripts

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)  }}

Allow the CI to install all dependencies required for your bookdown, pagedown, quarto, …

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()

List packages required in any script or notebook

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"

Vignettes included

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/

Code of Conduct

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

Stars

Watchers

Forks

Packages

No packages published

Contributors11

Languages


[8]ページ先頭

©2009-2025 Movatter.jp