| Title: | Generates a structured R project with custom templates. |
|---|---|
| Description: | This package generates an R project with customized templates. It also packages bash scripts for blaserlab collaborative git workflow. |
| Authors: | Brad Blaser [aut, cre] (ORCID: <https://orcid.org/0000-0002-3168-5423>) |
| Maintainer: | Brad Blaser <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9223 |
| Built: | 2026-05-30 09:12:17 UTC |
| Source: | https://github.com/blaserlab/blaseRtemplates |
cache a single package
cache_fun( package, cache_loc = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "library"), permissions )cache_fun( package, cache_loc = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "library"), permissions )
Make sure you are in a properly-formatted blaseRtemplates project.
catch_blasertemplates_root()catch_blasertemplates_root()
This function sets up a directory structure that is designed to work well with blaseRtemplates functions. It will create directories where indicated, write a standard .Rprofile document, and modify the user .Renviron file to configure R properly. The function will not overwrite existing directories and will fail when an existing installation is already present in the same location. The only modification outside of the existing directory is to the .Renviron file. If you have existing configurations in your .Renviron file, they will only be changed if they are conflicting. If this is problematic, then you should archive your .Renviron file before running. The new .Renviron file will cause R to use the new .Rprofile in the cache directory and skip existing user- and project-level .Rprofiles.
For the individual user: Identify the location for your blaseRtemplates cache and your R projects. For convenience, these can be within the same parent directory. The cache will hold versioned and hashed binary package files in the library directory. These are the actual instructions used by R when you call up functions. The version/hash structure is tracked by your projects and allows reproducibility. The user_project directory holds symlinks to the specific version/hash packages being used. A new directory is created for each new project. The source directory is used by the package installer, pak, to archive the source files for each package. You should rarely need to look in there. The logs directory holds .Rhistory files. One is created for each R session you open. The .tsv files catalog the entire collection of packages in your cache with their dependencies. The .Rprofile is what sets up your R session to use the proper user_package directory, ensures you are working in a valid project, generates startup messages and sets some helpful options.
This function will create 1 project, called baseproject, in the projects directory which will ensure you are always operating in a project environment. Working outside of a project can have dangerous/unintended consequences.
For the system administrator: the directory structure will work equally well for a multiuser system (e.g. rstudio server) with minor modifications. The .Rprofile generated by the function can be used as Rprofile.site and will apply to all users of that R installation. Each user should have their own project directories with a baseproject on the system. The .Renviron file should be modified for each user to point to this directory and saved in the home directory. There should be a subdirectory in user_projects with each user's ID for their project libraries. Ownership should be given to the user/group and permissions set accordingly, e.g. 755.
Upgrading R versions: since package binaries don't work across minor version changes in R, e.g. 4.2 -> 4.3, you will have to create a new cache directory each time this changes.
establish_new_bt(cache_path, project_path)establish_new_bt(cache_path, project_path)
cache_path |
Path to the blaseRtemplates cache root. Should include the R major and minor version numbers in the final directory. This and all intermediate directories will be created. |
project_path |
Path to the R projects directory. For convenience, can put this in the same parent directory as cache_path, but not strictly necessary. |
cli_alert
create, copy, path_package, path, file_access, dir_ls, path_file
str_detect, str_replace
proj_utils
## Not run: if(interactive()){ establish_new_bt(cache_path = "<some_directory>/r_4_2_cache", project_path = "<some_directory>/projects") } ## End(Not run)## Not run: if(interactive()){ establish_new_bt(cache_path = "<some_directory>/r_4_2_cache", project_path = "<some_directory>/projects") } ## End(Not run)
Find Unlinked Packages
find_unlinked_packages(lib_path)find_unlinked_packages(lib_path)
Sometimes, the user_project library can break. This can happen if there are failures in the upstream install functions. Or if the links are deleted for some reason. If everything is broken, you may need to repair the user project library. To do this, exit the project and enter a working project. Delete the links in the offending user project library. Then run this function to relink. Use either the library catalog file from the project itself, or if this is also corrupted with bad information, run an older version that worked or a version from another project that worked. If you can identify the problematic package in the course of these fixes, then you should probably delete it from your cache entirely.
fix_another_library(file, dir)fix_another_library(file, dir)
file |
The library catalog tsv file to read from. |
dir |
The user project library to repair. |
Nothing
path_math, create, path, path_file
read_delim
pull
get all package dependencies
get_all_deps(package)get_all_deps(package)
Use this to replace the current symlinked library with a new version. By default, the function will link to the newest version of all packages available in the cache. Alternatively, identify another project library catalog to replace the current version.
get_new_library(newest_or_file = "newest")get_new_library(newest_or_file = "newest")
newest_or_file |
Which set of packages to symlink, Default: 'newest' |
Uninstalled packages hashes.
Supply this function with a branch name. If the branch exists it will switch to the branch. If not, it will pull any changes from remote and then create the branch. Any uncommitted work will be carried over to the new branch in the same state. Avoid repeatedly switching branches with work in different states of completion since this may cause conflicts
git_easy_branch(branch)git_easy_branch(branch)
branch |
A character string with the branch name to create or switch to. |
nothing
git_branch
This uses gert to look up all active remotes and then runs gert::git_push() to each.
git_push_all()git_push_all()
nothing
This function uses git revert to rewind history to a prior commit. First make sure all of your changes have been committed. Then run gert::git_log() to identify the "good" commit you want to rewind to. Supply this as the argument to this function. A new commit will be made with a helpful message. Commit history is not changed so you can always rewind the rewind etc....
git_rewind_to(commit)git_rewind_to(commit)
commit |
Hash of the commit you want to rewind the state of your repository to. Requires a minimum of 7 characters. |
a tibble with the new git commit log after rewinding
This function updates default branch (usually "main") from remote. This pulls in any changes from other contributors. Then it merges the working branch into the upstream branch.
git_safe_merge(branch = NULL, upstream = "main")git_safe_merge(branch = NULL, upstream = "main")
branch |
The working branch you wish to merge, Default: NULL |
upstream |
The default upstream branch you wish to merge into, Default: NULL |
nothing
git-default-branch
git_branch,git_commit
This function updates a git branch via rebase from a default upstream branch (default is "main"). You can explicitly provide the names of your working branch and the default upstream branch.
git_update_branch(branch = NULL, upstream = "main")git_update_branch(branch = NULL, upstream = "main")
branch |
The working branch you wish to update, Default: NULL |
upstream |
The default upstream branch you wish to update from, Default: NULL |
nothing
git-default-branch
git_branch,git_stash
This wraps gitcreds::gitcreds_set() and adds a system call to edit the user global git config to set the cache timeout to 1 billion seconds if running a Linux system.
gitcreds_set()gitcreds_set()
nothing
hash one or more functions and then cache them and update the catalogs. Default permissions are set to 777.
hash_n_cache( lib_loc = .libPaths()[1], cache_loc = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "library"), verbose = TRUE, permissions = "777" )hash_n_cache( lib_loc = .libPaths()[1], cache_loc = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "library"), verbose = TRUE, permissions = "777" )
This function wraps usethis::create_from_github, making some useful default choices. Because this function forks the project, git will set up the originator as an upstream remote. Using blaseRtemplates::git_push_all will push to both the originator and the collaborator's github.
initialize_github(repo, dest = NULL, open = TRUE)initialize_github(repo, dest = NULL, open = TRUE)
repo |
The repo to clone. Must be in the form of github_user/repo_name. If private, you must be a collaborator and have permission to fork the repo from the owner. |
dest |
Destination directory. This directory will become the parent directory for the project you are forking. If NULL, the default, it will put the project in the directory defined by the usethis.destdir option. Set this in ~/.Rprofile. |
open |
Whether to open the forked project, Default: TRUE |
nothing
This wraps usethis::create_package() and adds a few additional templates.
initialize_package( path, fields = list(), roxygen = TRUE, check_name = TRUE, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fresh_install = FALSE, path_to_cache_root = Sys.getenv("BLASERTEMPLATES_CACHE_ROOT") )initialize_package( path, fields = list(), roxygen = TRUE, check_name = TRUE, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fresh_install = FALSE, path_to_cache_root = Sys.getenv("BLASERTEMPLATES_CACHE_ROOT") )
path |
path/name for the new package. It should include letters and "." only to be CRAN-compliant. |
fields |
named list of fields in addition to/overriding defaults for the DESCRIPTION file, Default: list() |
roxygen |
do you plan to use roxygen2 to document package?, Default: TRUE |
check_name |
check if name is CRAN-compliant, Default: TRUE |
rstudio |
makes an Rstudio project, default is true |
open |
to open or not, Default: rlang::is_interactive() |
isAvailable
is_interactive
use_template
defer
These functions create an R project:
create_project() creates a non-package project, i.e. a data analysis
project
Both functions can be called on an existing project; you will be asked before any existing files are changed.
This function is a modification of usethis::create_project
initialize_project( path, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fresh_install = FALSE, path_to_cache_root = Sys.getenv("BLASERTEMPLATES_CACHE_ROOT") )initialize_project( path, rstudio = rstudioapi::isAvailable(), open = rlang::is_interactive(), fresh_install = FALSE, path_to_cache_root = Sys.getenv("BLASERTEMPLATES_CACHE_ROOT") )
path |
A path. If it exists, it is used. If it does not exist, it is created, provided that the parent path exists. |
rstudio |
If |
open |
If
|
Path to the newly created project or package, invisibly.
Use this to install a new package. Choosing "new_or_update" will go to the package repository, get the latest version of the software, install into your cache and link to your project library. Choosing "link_from_cache" will get you the latest version in the cache Also, use this option with either "which_version" or "which_hash" to install specific versions.
install_one_package( package, how = c("link_from_cache", "new_or_update", "tarball"), which_version = NULL, which_hash = NULL )install_one_package( package, how = c("link_from_cache", "new_or_update", "tarball"), which_version = NULL, which_hash = NULL )
package |
Package name or path to tarball. Prefix with "repo\/" for github source packages and "bioc::" for bioconductor. |
how |
How to install the package, Default: c("link_from_cache", "new_or_update", "tarball") |
which_version |
Package version to install, Default: NULL |
which_hash |
Package hash to install, Default: NULL |
nothing
link package dependencies
link_deps(package)link_deps(package)
link one new package
link_one_new_package(package, version = NULL, hash = NULL)link_one_new_package(package, version = NULL, hash = NULL)
Calling function to get recursive dependencies
rec_get_deps( needed, checked = character(0), deps = character(0), catalog = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "dependency_catalog.tsv") )rec_get_deps( needed, checked = character(0), deps = character(0), catalog = fs::path(Sys.getenv("BLASERTEMPLATES_CACHE_ROOT"), "dependency_catalog.tsv") )
recursively get package dependencies
rec_get_deps_internal(needed, checked, deps, catalog)rec_get_deps_internal(needed, checked, deps, catalog)
If you have deleted or otherwise broken your .Rprofile or .Renviron files, you may have difficulty connecting to the package cache. This function will regenerate both for you. The existing .Rprofile must be deleted manually. You can choose to archive the old version if you wish. It will be replaced with the standard .Rprofile from blaseRtemplates. The .Renviron file will be modified by removing the damaged lines and replacing them with the correct ones. You must supply the correct file path locations to your cache directory and project directory, otherwise your R installation will be configured incorrectly.
regenerate_bt_configs(cache_path, project_path)regenerate_bt_configs(cache_path, project_path)
cache_path |
path to the cache directory |
project_path |
path to the projects directory |
nothing
file_access, path, copy, path_package
cli_abort, cli_alert
str_detect, str_replace
Since this file is ignored by git, you will have to regenerate it when forking a repository. This function writes the template file to your R directory as "regenerated_git_commands.R".
regenerate_git_commands()regenerate_git_commands()
nothing
Shortcut function to save the blaseRtemplates report template in Rmd format within an Rmd folder.
report_template_rmd(report_name = NULL)report_template_rmd(report_name = NULL)
report_name |
Name for the Rmd file, Default: NULL |
Makes a new Rmd file with the supplied file name
Returns nothing
str_detect
cli_alert
create, path
use_template
The prompt package adds a nice feature but has some limitations, namely, that it does not respect changing git branches and has to be manually re-called. This defeats the purpose. Blasertemplates git functions automoatically call prompt to change the prompt label when switching branches, but this will not happen if you change branches using the terminal, the git panel or other git branching functions. Therefore this function is provided to manually reset your prompt to the current branch.
reset_prompt()reset_prompt()
nothing
update the dependency catalog
update_dependency_catalog()update_dependency_catalog()
rewrite/update the package catalog
update_package_catalog()update_package_catalog()
In the current version of blaseRtemplates, the package library is cached at the location designated by the environment variable "BLASERTEMPLATES_CACHE_ROOT". There is a single cache for all users and projects. The cache holds the binary software used by each package. The packages for each project are connected to the cache by symlinks. The cache is versioned so that different projects can use different versions if desired. Use this function to write a tab-delimited file listing the packages used by each project.
This file will be written to the "library_catalogs" directory within each project. The filename incorporates the user name so everyone working on the project will have their own. Use get_new_library() to adopt a new version of all packages
write_project_library_catalog()write_project_library_catalog()
returns nothing