Title: | Install, Update, Load Packages from CRAN, 'GitHub', and 'Bioconductor' in One Step |
---|---|
Description: | Automatically install, update, and load 'CRAN', 'GitHub', and 'Bioconductor' packages in a single function call. By accepting bare unquoted names for packages, it's easy to add or remove packages from the list. |
Authors: | Desi Quintans [aut, cre] |
Maintainer: | Desi Quintans <[email protected]> |
License: | GPL-3 |
Version: | 1.8.1 |
Built: | 2024-10-26 02:54:53 UTC |
Source: | https://github.com/desiquintans/librarian |
Inspired by my mysterious inability to remember what the RColorBrewer package is actually called. Lets you find relevant CRAN packages right from your terminal.
browse_cran(query, fuzzy = FALSE, echo = TRUE, ignore.case = TRUE)
browse_cran(query, fuzzy = FALSE, echo = TRUE, ignore.case = TRUE)
query |
(Character) A string to |
fuzzy |
(Logical) If |
echo |
(Logical) If |
ignore.case |
(Logical) If |
When browse_cran()
is run for the first time in a new session, it will
take about 6-12 seconds to download and cache CRAN data. This only happens once
per session; subsequent calls will use the cached copy.
Invisibly returns a dataframe of the packages that matched the query together with their descriptions. Prints results to the console.
browse_cran("colorbrewer") # Search by keyword #> RColorBrewer #> Provides color schemes for maps (and other graphics) designed by Cynthia #> Brewer as described at http://colorbrewer2.org #> #> Redmonder #> Provide color schemes for maps (and other graphics) based on the color #> palettes of several Microsoft(r) products. browse_cran("zero-inflat.*?(abund|count)") # Search by regular expression #> hurdlr #> When considering count data, it is often the case that many more zero #> counts than would be expected of some given distribution are observed. # And five other matches... browse_cran("network twitter api", fuzzy = TRUE) # Order-agnostic (fuzzy) search #> RKlout #> An interface of R to Klout API v2.
browse_cran("colorbrewer") # Search by keyword #> RColorBrewer #> Provides color schemes for maps (and other graphics) designed by Cynthia #> Brewer as described at http://colorbrewer2.org #> #> Redmonder #> Provide color schemes for maps (and other graphics) based on the color #> palettes of several Microsoft(r) products. browse_cran("zero-inflat.*?(abund|count)") # Search by regular expression #> hurdlr #> When considering count data, it is often the case that many more zero #> counts than would be expected of some given distribution are observed. # And five other matches... browse_cran("network twitter api", fuzzy = TRUE) # Order-agnostic (fuzzy) search #> RKlout #> An interface of R to Klout API v2.
Check if packages are attached
check_attached(...)
check_attached(...)
... |
(Dots) Package names as bare names, strings, or a character vector. If left empty, lists all attached packages. |
If dots
is empty, a character vector of all attached packages.
Otherwise, return a named logical vector where TRUE
means the package
is attached
## Not run: check_attached() #> [1] "librarian" "testthat" "magrittr" "stats" ... check_attached(c("utils", "stats")) #> utils stats #> TRUE TRUE check_attached("datasets", "base", fakepkg) #> datasets base fakepkg #> TRUE TRUE FALSE ## End(Not run)
## Not run: check_attached() #> [1] "librarian" "testthat" "magrittr" "stats" ... check_attached(c("utils", "stats")) #> utils stats #> TRUE TRUE check_attached("datasets", "base", fakepkg) #> datasets base fakepkg #> TRUE TRUE FALSE ## End(Not run)
Check if packages are installed
check_installed(...)
check_installed(...)
... |
(Dots) Package names as bare names, strings, or a character vector. If left empty, lists all installed packages. |
If dots
is empty, a character vector of all installed packages.
Otherwise, return a named logical vector where TRUE
means the package
is installed.
## Not run: check_installed() #> [1] "addinslist" "antiword" " ape" "assertthat" ... check_installed(c("utils", "stats")) #> utils stats #> TRUE TRUE check_installed("datasets", "base", fakepkg) #> datasets base fakepkg #> TRUE TRUE FALSE ## End(Not run)
## Not run: check_installed() #> [1] "addinslist" "antiword" " ape" "assertthat" ... check_installed(c("utils", "stats")) #> utils stats #> TRUE TRUE check_installed("datasets", "base", fakepkg) #> datasets base fakepkg #> TRUE TRUE FALSE ## End(Not run)
Check if packages are installed or attached
check_pkg_status(..., status, use_list = FALSE)
check_pkg_status(..., status, use_list = FALSE)
... |
(Dots) Package names as bare names, strings, or a vector of strings.
If left blank, returns a list of all packages that are installed/attached
depending on the value of |
status |
(Character) |
use_list |
(Logical) If |
If dots
is empty, a character vector of package names. Otherwise,
return a named logical vector where TRUE
means the package is installed
or attached, depending on the value of status
.
I use this internally for turning a vector of package names into a string.
collapse_vec(..., wrap = "'", collapse = ", ", unique = TRUE)
collapse_vec(..., wrap = "'", collapse = ", ", unique = TRUE)
... |
(...) Vectors that will be concatenated and coerced to Character. |
wrap |
(Character) Placed at the left and right sides of each vector element. |
collapse |
(Character) Placed between each element of the original vector(s). |
unique |
(Logical) If |
A string.
## Not run: collapse_vec(month.abb) #> [1] "'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'" ## End(Not run)
## Not run: collapse_vec(month.abb) #> [1] "'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'" ## End(Not run)
Did the user pass arguments inside dots?
dots_is_empty(...)
dots_is_empty(...)
... |
(Dots) |
TRUE
(dots is empty) or FALSE
(dots is not empty).
## Not run: is_dots_empty(package, names, here) #> [1] FALSE ## End(Not run)
## Not run: is_dots_empty(package, names, here) #> [1] FALSE ## End(Not run)
How many items are in dots?
dots_length(...)
dots_length(...)
... |
(Dots) |
An integer
## Not run: dots_length(package, names, here) #> [1] 3 ## End(Not run)
## Not run: dots_length(package, names, here) #> [1] 3 ## End(Not run)
Is the 1st 'dots' arg a character vector with length > 1?
dots1_is_pkglist(...)
dots1_is_pkglist(...)
... |
(Dots) |
TRUE
if ..1
is a vector or list with length > 1.
## Not run: dots1_is_pkglist() #> [1] FALSE dots1_is_pkglist("hello", "hey", "hi") #> [1] FALSE dots1_is_pkglist(c("hello", "hey"), "hi") #> [1] TRUE dots1_is_pkglist(c(hello, hey), "hi") #> [1] FALSE # A common programming scenario: pkg_list <- c("only_one_package") dots1_is_pkglist(pkg_list) #> [1] TRUE ## End(Not run)
## Not run: dots1_is_pkglist() #> [1] FALSE dots1_is_pkglist("hello", "hey", "hi") #> [1] FALSE dots1_is_pkglist(c("hello", "hey"), "hi") #> [1] TRUE dots1_is_pkglist(c(hello, hey), "hi") #> [1] FALSE # A common programming scenario: pkg_list <- c("only_one_package") dots1_is_pkglist(pkg_list) #> [1] TRUE ## End(Not run)
A fuzzy regex is one that will match search terms in any order by using PERL lookaround. This is very slow, but often worth the cost to get more complete results.
fuzzy_needle(vec)
fuzzy_needle(vec)
vec |
(Character) A string containing space-separated keywords to search for. |
A string where each word has been wrapped as a lookaround term.
## Not run: fuzzy_needle("network centrality") #> [1] "(?=.*network)(?=.*centrality)" ## End(Not run)
## Not run: fuzzy_needle("network centrality") #> [1] "(?=.*network)(?=.*centrality)" ## End(Not run)
Assert that a URL is complete and valid
is_valid_url(string)
is_valid_url(string)
string |
(Character) A URL to check. |
The regex I use is "@stephenhay" from https://mathiasbynens.be/demo/url-regex because it's the shortest regex that matches every CRAN mirror at https://cran.r-project.org/mirrors.html.
A logical value, TRUE
if the URL is valid, FALSE
if otherwise.
## Not run: is_valid_url("http://rstudio.com") ## End(Not run)
## Not run: is_valid_url("http://rstudio.com") ## End(Not run)
View and edit the list of folders that R will look inside when trying to find a package. Add an existing folder, create and add a new folder, or shuffle a folder to the front of the list so that it is used as the default installation location for new packages in the current session.
lib_paths(path, make_path = TRUE, ask = TRUE)
lib_paths(path, make_path = TRUE, ask = TRUE)
path |
(Character, or omit) A path to add to the library search path. Can be an
absolute or relative path. If |
make_path |
(Logical) If |
ask |
(Logical) If |
A character vector of the folders on the library search path. If path
was not
omitted, it will be the first element.
lib_paths() #> [1] "D:/R/R-3.5.2/library" lib_paths(file.path(tempdir(), "newlibraryfolder"), ask = FALSE) #> [1] "C:/Users/.../Temp/Rtmp0Qbvgo/newlibraryfolder" #> [2] "D:/R/R-3.5.2/library"
lib_paths() #> [1] "D:/R/R-3.5.2/library" lib_paths(file.path(tempdir(), "newlibraryfolder"), ask = FALSE) #> [1] "C:/Users/.../Temp/Rtmp0Qbvgo/newlibraryfolder" #> [2] "D:/R/R-3.5.2/library"
This function tells R to load packages and library folders at the start of every session (or on a per-project basis). It's best to keep this auto-load list to a minimum so that you don't forget to explicitly install/attach packages in scripts that need them.
lib_startup(..., lib = lib_paths(), global = TRUE)
lib_startup(..., lib = lib_paths(), global = TRUE)
... |
(Names) Packages as bare names. For packages that come from GitHub, you can
keep the username/package format, or omit the username and provide just the package
name. If you leave |
lib |
(Character) The path where packages are installed. Can be an
absolute or relative path. If |
global |
(Logical) If |
R's startup order is mentioned in ?Startup
, but briefly:
R tries to load the environmental variables file (Renviron.site)
R tries to load the site-wide profile (Rprofile.site)
R tries to load the user profile (.Rprofile), first in the current directory, and then in the user's home directory (on Windows, the My Documents folder). Only one of these files is sourced into the workspace.
Omitting ...
makes R load only its default packages. If these are not set in an
environmental variable (R_DEFAULT_PACKAGES
), then R will default to loading these
packages: datasets, utils, grDevices, graphics, stats, and methods.
A message listing the values that were written to the .Rprofile file.
#> lib_startup(librarian, magrittr, lib = "C:/Dropbox/My R Library")
#> lib_startup(librarian, magrittr, lib = "C:/Dropbox/My R Library")
List the dependencies of selected packages
list_dependencies(of_pkgs, which = c("Depends", "Imports"))
list_dependencies(of_pkgs, which = c("Depends", "Imports"))
of_pkgs |
(Character) Packages whose dependencies will be found. |
which |
(Character) The types of dependencies to find. |
A character vector of package names. Note that all dependencies of all requested packages will be placed into the one vector.
Whereas base::file.path()
only concatenates strings to build a path, make_dirs()
also makes sure those folders exist.
make_dirs(...)
make_dirs(...)
... |
(Character) Arguments to send to |
(Character) A file path. Automatically adds trailing slashes if required.
Desi Quintans (http://www.desiquintans.com)
Desiderata package (https://github.com/DesiQuintans/desiderata)
## Not run: make_dirs(tempdir(), "newfolder") #> [1] "C:/Users/.../Temp/RtmpSwZA8X/newfolder" ## End(Not run)
## Not run: make_dirs(tempdir(), "newfolder") #> [1] "C:/Users/.../Temp/RtmpSwZA8X/newfolder" ## End(Not run)
Convert dots to package names
nse_dots(..., keep_user = FALSE)
nse_dots(..., keep_user = FALSE)
... |
(Dots) Package names provided as bare names or strings (of length 1). If a character vector is provided as the first argument, it will be used and all other arguments in dots will be ignored. |
keep_user |
(Logical) If |
A character vector.
## Not run: nse_dots(dplyr, DesiQuintans/desiderata, keep_user = FALSE) #> [1] "dplyr" "desiderata" nse_dots(dplyr, DesiQuintans/desiderata, keep_user = TRUE) #> [1] "dplyr" "DesiQuintans/desiderata" ## End(Not run)
## Not run: nse_dots(dplyr, DesiQuintans/desiderata, keep_user = FALSE) #> [1] "dplyr" "desiderata" nse_dots(dplyr, DesiQuintans/desiderata, keep_user = TRUE) #> [1] "dplyr" "DesiQuintans/desiderata" ## End(Not run)
Convenience shortcut for force-unshelf
ing packages and then shelf
ing them again.
reshelf(...)
reshelf(...)
... |
(Names) Packages as bare names. For packages that come from GitHub, you can keep the username/package format, or omit the username and provide just the package name. |
Invisibly returns a named logical vector, where the names are the packages
requested in ...
and TRUE
means that the package was successfully attached.
reshelf(datasets) # reshelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> datasets #> TRUE
reshelf(datasets) # reshelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> datasets #> TRUE
Keep the first sentence of a string.
sentence(string)
sentence(string)
string |
(Character) A string. |
The string with only the first sentence.
## Not run: sentence("This is a sentence. And this is another sentence.") #> [1] "This is a sentence." sentence("This is just one sentence.") #> [1] "This is just one sentence." sentence("Is this a sentence? Or is this one. Maybe this one! What if there are lots of sentences?") #> [1] "Is this a sentence? ## End(Not run)
## Not run: sentence("This is a sentence. And this is another sentence.") #> [1] "This is a sentence." sentence("This is just one sentence.") #> [1] "This is just one sentence." sentence("Is this a sentence? Or is this one. Maybe this one! What if there are lots of sentences?") #> [1] "Is this a sentence? ## End(Not run)
Attach packages to the search path, installing them from CRAN, GitHub, or Bioconductor if needed
shelf( ..., lib = NULL, update_all = FALSE, quiet = FALSE, ask = TRUE, cran_repo = getOption("repos"), bioc_repo = character() )
shelf( ..., lib = NULL, update_all = FALSE, quiet = FALSE, ask = TRUE, cran_repo = getOption("repos"), bioc_repo = character() )
... |
(Names) Packages as bare names. If the package is from GitHub, include both the username and package name as UserName/package (see examples). |
lib |
(Character) By R convention, packages are installed to the first
folder in your library search path ( |
update_all |
(Logical) If |
quiet |
(Logical) If |
ask |
(Logical) If |
cran_repo |
(Character) In RStudio, a default CRAN repo can be set via Options > Packages > Default CRAN Mirror). Otherwise, provide the URL to CRAN or one of its mirrors. If an invalid URL is given, defaults to https://cran.r-project.org. |
bioc_repo |
(Character) If you use Bioconductor, you can set the repo URLs for it here.
Defaults to Bioconductor's defaults (view them with |
You may choose to organise your library into folders to hold packages for different
tasks or projects. If you specify a lib
folder, it will be created (if needed) and
attached to the package search path. R will look for packages by working through the
package search path in order. You can view the folders that are on this path by
calling lib_paths()
with no arguments.
If you specify a new lib
and use the argument update_all = TRUE
to force an
already-installed package to reinstall, a new copy of that package will be made in
lib
and then loaded from there. This means that you can potentially have several
copies of the same package across many folders on your machine, each a different
version. This allows you to maintain a different library folder for different projects,
so that updated packages in Project B will not affect the package versions you rely on
for Project A.
Invisibly returns a named logical vector, where the names are the packages
requested in ...
and TRUE
means that the package was successfully installed
and attached.
shelf(fortunes, DesiQuintans/emptyRpackage, cowsay, lib = tempdir(), update_all = TRUE) # shelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> fortunes emptyRpackage cowsay #> TRUE TRUE TRUE
shelf(fortunes, DesiQuintans/emptyRpackage, cowsay, lib = tempdir(), update_all = TRUE) # shelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> fortunes emptyRpackage cowsay #> TRUE TRUE TRUE
This is copied from my personal package, desiderata
.
shhh(expr)
shhh(expr)
expr |
(Expression) An expression to evaluate. |
Evaluates expr
.
Install packages from CRAN, GitHub, or Bioconductor if needed/wanted
stock( ..., lib = NULL, update_all = FALSE, quiet = FALSE, ask = TRUE, cran_repo = getOption("repos"), bioc_repo = character() )
stock( ..., lib = NULL, update_all = FALSE, quiet = FALSE, ask = TRUE, cran_repo = getOption("repos"), bioc_repo = character() )
... |
(Names) Packages as bare names. If the package is from GitHub, include both the username and package name as UserName/package (see examples). |
lib |
(Character) By R convention, packages are installed to the first
folder in your library search path ( |
update_all |
(Logical) If |
quiet |
(Logical) If |
ask |
(Logical) If |
cran_repo |
(Character) In RStudio, a default CRAN repo can be set via Options > Packages > Default CRAN Mirror). Otherwise, provide the URL to CRAN or one of its mirrors. If an invalid URL is given, defaults to https://cran.r-project.org. |
bioc_repo |
(Character) If you use Bioconductor, you can set the repo URLs for it here.
Defaults to Bioconductor's defaults (view them with |
You may choose to organise your library into folders to hold packages for different
tasks or projects. If you specify a lib
folder, it will be created (if needed) and
attached to the package search path. R will look for packages by working through the
package search path in order. You can view the folders that are on this path by
calling lib_paths()
with no arguments.
If you specify a new lib
and use the argument update_all = TRUE
to force an
already-installed package to reinstall, a new copy of that package will be made in
lib
and then loaded from there. This means that you can potentially have several
copies of the same package across many folders on your machine, each a different
version. This allows you to maintain a different library folder for different projects,
so that updated packages in Project B will not affect the package versions you rely on
for Project A.
Invisibly returns a named logical vector, where the names are the packages
requested in ...
and TRUE
means that the package was successfully installed.
stock(fortunes, DesiQuintans/emptyRpackage, cowsay, lib = tempdir(), update_all = TRUE) # shelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> fortunes emptyRpackage cowsay #> TRUE TRUE TRUE # And to confirm that they are installed but not attached: check_attached(fortunes, DesiQuintans/emptyRpackage, cowsay) #> fortunes emptyRpackage cowsay #> FALSE FALSE FALSE
stock(fortunes, DesiQuintans/emptyRpackage, cowsay, lib = tempdir(), update_all = TRUE) # shelf() returns invisibly; bind its output to a variable or access the .Last.value. print(.Last.value) #> fortunes emptyRpackage cowsay #> TRUE TRUE TRUE # And to confirm that they are installed but not attached: check_attached(fortunes, DesiQuintans/emptyRpackage, cowsay) #> fortunes emptyRpackage cowsay #> FALSE FALSE FALSE
Messages for the user
tell_user(message, ...)
tell_user(message, ...)
message |
(Character) An identifier string for a message. |
... |
(Dots) Data to pass into the message for |
A string.
## Not run: message(tell_user("not allowed to make path", "C:/fakefolder")) ## End(Not run)
## Not run: message(tell_user("not allowed to make path", "C:/fakefolder")) ## End(Not run)
Packages can be detached by themselves, with their dependencies safely (i.e. as long as those dependencies are not being used by other packages), or with their dependencies unsafely (regardless of whether those dependencies are still needed). All non-default packages can be detached at once too, including Librarian itself.
unshelf( ..., everything = FALSE, also_depends = FALSE, safe = TRUE, quiet = TRUE )
unshelf( ..., everything = FALSE, also_depends = FALSE, safe = TRUE, quiet = TRUE )
... |
(Names) Packages as bare names. For packages that come from GitHub, you can keep the username/package format, or omit the username and provide just the package name. |
everything |
(Logical) If |
also_depends |
(Logical) If |
safe |
(Logical) If |
quiet |
(Logical) If |
Invisibly returns a named logical vector, where the names are the packages
and TRUE
means that the package was successfully detached.
# These are the same: #> unshelf(janitor, desiderata, purrr) #> unshelf(janitor, DesiQuintans/desiderata, purrr) # unshelf() returns invisibly; bind its output to a variable or access the .Last.value. #> print(.Last.value) #> desiderata janitor purrr #> TRUE TRUE TRUE #> unshelf(everything = TRUE) #> print(.Last.value) #> librarian testthat #> TRUE TRUE
# These are the same: #> unshelf(janitor, desiderata, purrr) #> unshelf(janitor, DesiQuintans/desiderata, purrr) # unshelf() returns invisibly; bind its output to a variable or access the .Last.value. #> print(.Last.value) #> desiderata janitor purrr #> TRUE TRUE TRUE #> unshelf(everything = TRUE) #> print(.Last.value) #> librarian testthat #> TRUE TRUE
Wrapping text needs to be done separately from actually printing it with
stop
or warning
or message
. This is because these functions typically
also print some information about the environment where they were called.
wrap_text(...)
wrap_text(...)
... |
Vectors to be coerced to Character. |
The text in ...
will be collapsed and wrapped.
## Not run: wrapped <- wrap_text( "Lorem ipsum dolor sit amet, ornare justo condimentum", "et sit lorem! Himenaeos, vel et sodales sit.", "Eu nulla. Magna ullamcorper nascetur placerat platea.\n\n", "Eleifend semper velit sed aliquam, ut ligula non commodo.") cat(wrapped) #> Lorem ipsum dolor sit amet, ornare justo condimentum et sit lorem! #> Himenaeos, vel et sodales sit. Eu nulla. Magna ullamcorper #> nascetur placerat platea. #> #> Eleifend semper velit sed aliquam, ut ligula non commodo. ## End(Not run)
## Not run: wrapped <- wrap_text( "Lorem ipsum dolor sit amet, ornare justo condimentum", "et sit lorem! Himenaeos, vel et sodales sit.", "Eu nulla. Magna ullamcorper nascetur placerat platea.\n\n", "Eleifend semper velit sed aliquam, ut ligula non commodo.") cat(wrapped) #> Lorem ipsum dolor sit amet, ornare justo condimentum et sit lorem! #> Himenaeos, vel et sodales sit. Eu nulla. Magna ullamcorper #> nascetur placerat platea. #> #> Eleifend semper velit sed aliquam, ut ligula non commodo. ## End(Not run)