├── .Rbuildignore ├── .github └── workflows │ ├── new-build-R.yml │ └── pkgdown.yml ├── .gitignore ├── CNAME ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── R ├── peptar.R ├── resource_projects.R ├── unitar.R └── utility.R ├── README.md ├── inst └── extdata │ ├── R │ └── functions.R │ ├── _targets.R │ ├── _targets │ ├── meta │ │ ├── meta │ │ └── progress │ └── objects │ │ ├── dhs112 │ │ ├── mean_ref2 │ │ └── raw_data │ ├── data │ └── raw_data.csv │ ├── metadata │ ├── project_config.yaml │ ├── project_config_resize.yaml │ ├── readRemoteData.R │ ├── readRemoteData_resize.R │ └── sample_table.csv │ └── target_projects │ ├── crossfactory │ └── _targets.R │ ├── crossproj │ └── _targets.R │ ├── refdata1 │ └── _targets.R │ └── refdata2 │ └── _targets.R ├── man ├── build_pep_resource_targets.Rd ├── build_pep_resource_targets_prj.Rd ├── build_pep_resource_targets_prj_old.Rd ├── get_tar_dirs.Rd ├── load_custom.Rd ├── load_custom_old.Rd ├── load_external_file.Rd ├── my_meta.Rd ├── peptar_dirs.Rd ├── peptar_exec.Rd ├── peptar_load.Rd ├── peptar_make.Rd ├── peptar_meta.Rd ├── peptar_path.Rd ├── peptar_read.Rd ├── source_target_functions.Rd ├── tar_list.Rd ├── tar_pep_foreach.Rd ├── track_external_target.Rd ├── unitar.Rd ├── unitar_exec.Rd ├── unitar_list.Rd ├── unitar_load.Rd ├── unitar_make.Rd ├── unitar_meta.Rd ├── unitar_path.Rd ├── unitar_read.Rd ├── unitar_read_from_path.Rd └── unitar_read_xprj.Rd ├── tests ├── testthat.R └── testthat │ └── testall.R └── vignettes └── unitar.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^doc$ 2 | ^Meta$ 3 | -------------------------------------------------------------------------------- /.github/workflows/new-build-R.yml: -------------------------------------------------------------------------------- 1 | # For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. 2 | # https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | name: R-CMD-check 12 | 13 | jobs: 14 | R-CMD-check: 15 | runs-on: ${{ matrix.config.os }} 16 | 17 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 18 | 19 | strategy: 20 | fail-fast: false 21 | matrix: 22 | config: 23 | - {os: windows-latest, r: 'release'} 24 | - {os: macOS-latest, r: 'release'} 25 | - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 26 | 27 | env: 28 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 29 | RSPM: ${{ matrix.config.rspm }} 30 | 31 | steps: 32 | - uses: actions/checkout@v2 33 | 34 | - uses: r-lib/actions/setup-r@master 35 | with: 36 | r-version: ${{ matrix.config.r }} 37 | 38 | - uses: r-lib/actions/setup-pandoc@master 39 | 40 | - name: Query dependencies 41 | run: | 42 | install.packages('remotes') 43 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 44 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 45 | shell: Rscript {0} 46 | 47 | - name: Cache R packages 48 | if: runner.os != 'Windows' 49 | uses: actions/cache@v2 50 | with: 51 | path: ${{ env.R_LIBS_USER }} 52 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 53 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 54 | 55 | - name: Install system dependencies 56 | if: runner.os == 'Linux' 57 | run: | 58 | while read -r cmd 59 | do 60 | eval sudo $cmd 61 | done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') 62 | 63 | - name: Install dependencies 64 | run: | 65 | remotes::install_cran("covr") 66 | remotes::install_deps(dependencies = TRUE) 67 | remotes::install_cran("rcmdcheck") 68 | shell: Rscript {0} 69 | 70 | - name: Check 71 | env: 72 | _R_CHECK_CRAN_INCOMING_REMOTE_: false 73 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") 74 | shell: Rscript {0} 75 | 76 | - name: Upload check results 77 | if: failure() 78 | uses: actions/upload-artifact@main 79 | with: 80 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 81 | path: check 82 | 83 | - name: Report test coverage 84 | run: covr::codecov() 85 | shell: Rscript {0} -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - dev 5 | - master 6 | 7 | name: Deploy-package-documentation 8 | 9 | jobs: 10 | pkgdown: 11 | runs-on: macos-latest 12 | env: 13 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - uses: r-lib/actions/setup-r@v1 18 | 19 | - uses: r-lib/actions/setup-pandoc@v1 20 | 21 | - name: Query dependencies 22 | run: | 23 | install.packages('remotes') 24 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 25 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 26 | shell: Rscript {0} 27 | 28 | - name: Restore R package cache 29 | uses: actions/cache@v2 30 | with: 31 | path: ${{ env.R_LIBS_USER }} 32 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 33 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 34 | 35 | - name: Install dependencies 36 | run: | 37 | remotes::install_deps(dependencies = TRUE) 38 | install.packages("pkgdown", type = "binary") 39 | shell: Rscript {0} 40 | 41 | - name: Install package 42 | run: R CMD INSTALL . 43 | 44 | - name: Deploy package documentation 45 | run: | 46 | git config --local user.email "actions@github.com" 47 | git config --local user.name "GitHub Actions" 48 | Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE, commit_message="deploy docs with a github action")' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | doc 2 | Meta 3 | inst/extdata/target_projects/*/_targets/* 4 | -------------------------------------------------------------------------------- /CNAME: -------------------------------------------------------------------------------- 1 | unitar.databio.org 2 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: unitar 2 | Title: Share targets across projects and users 3 | Version: 0.0.1 4 | Authors@R: c(person("Nathan", "Sheffield", email = "nathan@code.databio.org",role = c("aut", "cre"))) 5 | Description: A a simple R package that wraps the targets package. 6 | It provides functionality to use targets that span projects, which is outside the scope of the base targets package. With unitar, you can link two targets projects and load targets from external projects so that you can share caches and computing across users and projects. 7 | License: BSD_2_clause + file LICENSE 8 | Encoding: UTF-8 9 | LazyData: true 10 | Depends: targets, fs, withr 11 | Imports: pepr 12 | Suggests: 13 | testthat (>= 3.0.0), knitr, rmarkdown 14 | RoxygenNote: 7.1.1 15 | URL: https://github.com/databio/unitar 16 | BugReports: https://github.com/databio/unitar 17 | VignetteBuilder: knitr 18 | Config/testthat/edition: 3 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Nathan Sheffield -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(build_pep_resource_targets) 4 | export(build_pep_resource_targets_prj) 5 | export(get_tar_dirs) 6 | export(load_custom) 7 | export(load_external_file) 8 | export(my_meta) 9 | export(peptar_dirs) 10 | export(peptar_exec) 11 | export(peptar_load) 12 | export(peptar_make) 13 | export(peptar_meta) 14 | export(peptar_path) 15 | export(peptar_read) 16 | export(source_target_functions) 17 | export(tar_list) 18 | export(tar_pep_foreach) 19 | export(track_external_target) 20 | export(unitar_exec) 21 | export(unitar_list) 22 | export(unitar_load) 23 | export(unitar_make) 24 | export(unitar_meta) 25 | export(unitar_path) 26 | export(unitar_read) 27 | export(unitar_read_from_path) 28 | export(unitar_read_xprj) 29 | import(fs) 30 | import(pepr) 31 | import(targets) 32 | import(withr) 33 | -------------------------------------------------------------------------------- /R/peptar.R: -------------------------------------------------------------------------------- 1 | # peptar functions provide an easy interface to use unitar with pepr 2 | 3 | 4 | #' Execute a function across all target folders specified in the current Project. 5 | #' @param p A pepr::Project object representing your current project. 6 | #' @param ... Function and parameters to execute 7 | #' @export 8 | peptar_exec = function(p, ...) { 9 | tar_folders = p@config$tprojects 10 | tar_folders 11 | unitar_exec(tar_folders, ...) 12 | } 13 | 14 | 15 | #' Retrieves tar folders from PEP 16 | #' @param p A pepr::Project object representing your current project. 17 | #' @export 18 | peptar_dirs = function(p) { 19 | # return(p@config$tprojects) # No path normalization 20 | tproj_rel = function(relpath, relto) { 21 | if (fs::is_absolute_path(relpath)){ 22 | return(relpath) 23 | } else { 24 | return(paste0( 25 | normalizePath(withr::with_dir(dirname(relto), fs::path_wd(relpath))), "/")) 26 | } 27 | } 28 | unlist(lapply(p@config$tprojects, tproj_rel, p@file)) 29 | } 30 | 31 | 32 | #' Execute tar_meta across all target folders specified in the current Project. 33 | #' @param p A pepr::Project object representing your current project. 34 | #' @param ... Parameters to pass to tar_meta 35 | #' @export 36 | peptar_meta = function(p, ...) { 37 | unitar_meta(peptar_dirs(p), ...) 38 | } 39 | 40 | 41 | #' Execute tar_make across all target folders specified in the current Project. 42 | #' @param p A pepr::Project object representing your current project. 43 | #' @param ... Parameters to pass to tar_make 44 | #' @export 45 | peptar_make = function(p, ...) { 46 | unitar_make(peptar_dirs(p), ...) 47 | } 48 | 49 | 50 | #' Find path to a target from all your PEP target folders 51 | #' @param p A pepr::Project object representing your current project. 52 | #' @param tname The name of the target to query 53 | #' @export 54 | peptar_path = function(p, tname) { 55 | unitar_path(peptar_dirs(p), tname) 56 | } 57 | 58 | 59 | #' Load a target and return it, from all your PEP target folders 60 | #' @param p A pepr::Project object representing your current project. 61 | #' @param tname The name of the target to query 62 | #' @export 63 | peptar_read = function(p, tname) { 64 | unitar_read(peptar_dirs(p), tname) 65 | } 66 | 67 | 68 | #' Load a target invisibly, from all your PEP target folders 69 | #' @param p A pepr::Project object representing your current project. 70 | #' @param tname The name of the target to query 71 | #' @export 72 | peptar_load = function(p, tname) { 73 | unitar_load(peptar_dirs(p), tname) 74 | } 75 | 76 | 77 | #' Simplified tar_meta function with only a few fields. 78 | #' @export 79 | my_meta = function() { 80 | tar_meta(fields=c("name", "bytes", "seconds", "path")) 81 | } 82 | -------------------------------------------------------------------------------- /R/resource_projects.R: -------------------------------------------------------------------------------- 1 | #' Helper function to build target functions to track and read files 2 | #' 3 | #' This is a target factory that will produce a target to track 4 | #' a file, and then to read that file using an arbitrary function 5 | #' provided by the user. 6 | #' 7 | #' @param sample_name Name of the target 8 | #' @param rootpath Path to where resource files are kept 9 | #' @param filepath Relative path to file 10 | #' @param func Function to use to load file in R 11 | #' @export 12 | #' 13 | load_external_file = function(sample_name, rootpath, filepath, func) { 14 | fullpath = paste0(rootpath, filepath) 15 | name_file = paste0(sample_name, "_file") 16 | command_data = substitute(func(fullpath), env=list(fullpath=as.symbol(name_file), func=as.symbol(func))) 17 | 18 | list( 19 | tar_target_raw(name_file, fullpath, format = "file"), 20 | tar_target_raw(sample_name, command_data) 21 | ) 22 | } 23 | 24 | 25 | #' Experimental target factory to track but not duplicate external files. 26 | #' 27 | #' EXPERIMENTAL. This function is just a concept that has not been tested. 28 | #' It will change or disappear in future versions. 29 | #' @export 30 | track_external_target = function(tname, ext_tname, func) { 31 | fullpath = unitar_path(ext_tname) 32 | name_file = paste0(sample_name, "_file") 33 | command_data = substitute(func(fullpath), env=list(fullpath=as.symbol(name_file), func=as.symbol(func))) 34 | 35 | list( 36 | tar_target_raw(name_file, fullpath, format = "file"), 37 | tar_target_raw(tname, command_data) 38 | ) 39 | } 40 | 41 | 42 | #' Helper function to build targets from function calls with custom args 43 | #' 44 | #' This function is a target factory that will produce a target for an 45 | #' arbitrary function call with arbitrary arguments and values. 46 | #' 47 | #' @param tname Target name 48 | #' @param func Function to call 49 | #' @param argnames List of arg names 50 | #' @param argvals List of arg values 51 | #' @param argtypes List of arg types (use 'symbol' for targets) 52 | load_custom_old = function(tname, func, argvals, argtypes, argnames) { 53 | l = as.list(argvals) 54 | names(l) = argnames 55 | l[argtypes == "symbol"] = lapply(l[argtypes == "symbol"], as.symbol) 56 | l[argtypes == "numeric"] = lapply(l[argtypes == "numeric"], as.numeric) 57 | args_expr = as.call(c(as.symbol("list"), l)) 58 | command_data = substitute( 59 | do.call(func, args), 60 | env = list(args = args_expr, func = as.symbol(func)) 61 | ) 62 | tar_target_raw(tname, command_data) 63 | } 64 | 65 | 66 | # This version adds support for argtypes=file 67 | #' Helper function to build targets from function calls with custom args 68 | #' 69 | #' This function is a target factory that will produce a target for an 70 | #' arbitrary function call with arbitrary arguments and values. 71 | #' 72 | #' @param tname Target name 73 | #' @param func Function to call 74 | #' @param argvals List of arg values 75 | #' @param argtypes List of arg types (use 'symbol' for targets) 76 | #' @param argnames List of arg names 77 | #' @export 78 | load_custom = function(tname, func, argvals, argtypes, argnames=NULL) { 79 | l = as.list(argvals) 80 | # if (!is.null(argnames)) { 81 | names(l) = argnames 82 | # } 83 | 84 | # First, handle function calls that have file paths as argument: 85 | # 1. Add a file tracking target, with _fileX appended to the target name 86 | # 2. Adjust the argument to be a symbol with appropriate target name 87 | nFiles = length(l[argtypes == "file"]) 88 | targets_list = list() 89 | for (i in seq_len(nFiles)) { 90 | path = l[argtypes == "file"][i] 91 | tname_file = paste0(tname, "_file", i) 92 | targets_list = append(targets_list, 93 | tar_target_raw(tname_file, path, format="file")) 94 | l[argtypes == "file"][i] = tname_file 95 | argtypes[argtypes == "file"][i] = "symbol" 96 | } 97 | 98 | # Next, handle all other argument types. 99 | l[argtypes == "symbol"] = lapply(l[argtypes == "symbol"], as.symbol) 100 | l[argtypes == "numeric"] = lapply(l[argtypes == "numeric"], as.numeric) 101 | args_expr = as.call(c(as.symbol("list"), l)) 102 | command_data = substitute( 103 | do.call(func, args), 104 | env = list(args = args_expr, func = as.symbol(func)) 105 | ) 106 | targets_list = append(targets_list, tar_target_raw(tname, command_data)) 107 | targets_list 108 | } 109 | 110 | 111 | 112 | 113 | #' Target factory to build targets from a PEP 114 | #' 115 | #' @param p PEP defining targets to build 116 | build_pep_resource_targets_prj_old = function(p) { 117 | tbl = sampleTable(p) 118 | loadable_targets = list() 119 | i=1 120 | for (i in 1:nrow(tbl)) { 121 | if (tbl[[i, "type"]] == "file") { 122 | loadable_targets[[i]] = load_external_file(tbl[[i, "sample_name"]], 123 | config(p)$data_root, 124 | tbl[[i, "local_path"]], 125 | tbl[[i, "function"]]) 126 | } else if (tbl[[i, "type"]] == "call") { 127 | loadable_targets[[i]] = load_custom(tbl[[i, "sample_name"]], 128 | tbl[[i, "function"]], 129 | tbl[[i, "argval"]], 130 | tbl[[i, "argtype"]], 131 | tbl[[i, "argname"]] 132 | ) 133 | } 134 | } 135 | return(loadable_targets) 136 | } 137 | 138 | 139 | #' Target factory to build targets from a PEP 140 | #' 141 | #' @param p PEP defining targets to build 142 | #' @export 143 | build_pep_resource_targets_prj = function(p) { 144 | tbl = sampleTable(p) 145 | loadable_targets = list() 146 | i=1 147 | for (i in 1:nrow(tbl)) { 148 | loadable_targets[[i]] = load_custom(tbl[[i, "sample_name"]], 149 | tbl[[i, "function"]], 150 | pepr::.expandPath(tbl[[i, "argval"]]), 151 | tbl[[i, "argtype"]], 152 | tbl[[i, "argname"]] 153 | ) 154 | } 155 | return(loadable_targets) 156 | } 157 | 158 | #' Target factory for arbitrary functions on PEP samples 159 | #' 160 | #' This target factory creates a target for each element of a PEP. You can 161 | #' name the targets using a pattern that uses brackets to indicate sample 162 | #' attributes, like `{sample_name}`. 163 | #' 164 | #' @param p PEP. 165 | #' @param tpattern Target name, with patterns allowed 166 | #' @param func Function to call 167 | #' @param argvals_pattern List of arg values, with patterns allowed 168 | #' @param argtypes List of arg types (use 'symbol' for targets) 169 | #' @param argnames List of arg names 170 | #' @param combine_tname Target name for a combined target that merges all of the individual targets 171 | #' @param combine_func_name Character vector of a function that will combine them. 172 | #' 173 | #' @export 174 | tar_pep_foreach = function(p, tpattern, func, argvals_pattern, argtypes, argnames=NULL, 175 | combine_tname=NULL, combine_func_name="list") { 176 | tbl = sampleTable(p) 177 | loadable_targets = list() 178 | for (i in 1:nrow(tbl)) { 179 | tname = with(tbl[i,], glue::glue(tpattern)) 180 | argvals = sapply(argvals_pattern, function(x) { 181 | with(tbl[i,], glue::glue(x)) 182 | }) 183 | loadable_targets[[tname]] = unitar::load_custom(tname, func, argvals, argtypes, argnames) 184 | } 185 | tnames = names(loadable_targets) 186 | 187 | if (!is.null(combine_tname)) { 188 | loadable_targets[[length(loadable_targets)+1]] = unitar::load_custom( 189 | combine_tname, combine_func_name, tnames, rep("symbol", length(tnames)), tnames) 190 | } 191 | return(loadable_targets) 192 | } 193 | 194 | 195 | 196 | 197 | #' Target factory to build targets from a PEP 198 | #' 199 | #' @param config Path to PEP config file 200 | #' @export 201 | build_pep_resource_targets = function(config) { 202 | p = pepr::Project("config.yaml") 203 | return(build_pep_resource_targets_prj(p)) 204 | } 205 | 206 | 207 | #' Helper function to load target-creation functions from PEP config 208 | #' 209 | #' This function simple sources the target functions specified in 210 | #' a 'target_load_functions' attribute of a PEP. It's meant to be 211 | #' called from a _targets.R file, so you don't have to source those 212 | #' scripts individually, but can consolidate the configuration into 213 | #' the PEP config file. 214 | #' @param config Path to the PEP config file for the resource targets list. 215 | #' @export 216 | source_target_functions = function(config) { 217 | p = pepr::Project("config.yaml") 218 | 219 | # The config should have a pointer to source files: 220 | tf = config(p)$target_load_functions 221 | for (i in 1:length(tf)) { 222 | source(tf[[i]], local=parent.frame()) 223 | } 224 | } 225 | 226 | #' List the targets available for loading 227 | #' 228 | #' This function is useful for resource projects, where you want to see 229 | #' what you have available. It's just a simple wrapper that provides 230 | #' some sensible defaults to \code{tar_meta} for this use case. 231 | #' In the future I'd like it if this included a text description of the target 232 | #' @param n Number of targets to print (with sensible default) 233 | #' @param fields Fields passed to tar_meta (with sensible default) 234 | #' @param ... Any other arguments to pass to tar_meta 235 | #' @export 236 | tar_list = function(n=50, fields=c("name", "time", "bytes"), ...) { 237 | print(tar_meta(targets_only=TRUE, fields=fields, ...), n=n) 238 | invisible() 239 | } 240 | 241 | #' List all targets available across all projects 242 | #' 243 | #' If you have available targets from multiple projects, you might be 244 | #' interested in a quick look at them to see what you have available to load. 245 | #' This function just wraps tar_meta with a few defaults that are convenient 246 | #' for this purpose: it increase the default number of targets shown, and 247 | #' reduces the number of fields. It also runs it across all registered targets 248 | #' projects. 249 | #' @param n Number of targets to print (with sensible default) 250 | #' @param fields Fields passed to tar_meta (with sensible default) 251 | #' @param tar_dirs Target directories to search. NULL will use global option (the default). 252 | #' @param ... Any other arguments to pass to tar_meta 253 | #' @export 254 | unitar_list = function(n=50, fields=c("name", "time", "bytes"), tar_dirs=NULL, ...) { 255 | unitar_exec(func=tar_list, tar_dirs=tar_dirs) 256 | } 257 | -------------------------------------------------------------------------------- /R/unitar.R: -------------------------------------------------------------------------------- 1 | # PACKAGE DOCUMENTATION 2 | #' Targets that span projects 3 | #' 4 | #' Targets! now shared! 5 | #' 6 | #' @docType package 7 | #' @name unitar 8 | #' @author Nathan Sheffield 9 | #' 10 | #' @import fs withr pepr targets 11 | NULL 12 | 13 | #' Execute a function across a list of tar_target folders 14 | #' @param func Function to execute 15 | #' @param tar_dirs A list of root folders of targets-managed projects 16 | #' @param ... Additional arguments to function 17 | #' @export 18 | #' @examples 19 | #' tar_dirs="" 20 | #' unitar_exec(tar_dirs, tar_meta) 21 | unitar_exec = function(func=tar_make, tar_dirs=NULL, ...) { 22 | tar_dirs = get_tar_dirs(tar_dirs) 23 | lapply(tar_dirs, function(folder) { 24 | message(folder) 25 | tryCatch({ 26 | withr::with_dir(folder, func(...)) 27 | }, error=function(x) NA) 28 | }) 29 | } 30 | 31 | 32 | #' A global tar_make function that spans all tar_target folders 33 | #' @param tar_dirs A list of root folders of targets-managed projects 34 | #' @examples 35 | #' tar_dirs="" 36 | #' unitar_meta(tar_dirs) 37 | #' @export 38 | unitar_make = function(tar_dirs=NULL) { 39 | tar_dirs = get_tar_dirs(tar_dirs) 40 | unitar_exec(func=tar_make, tar_dirs=tar_dirs) 41 | } 42 | 43 | 44 | #' Call tar_meta across multiple tar projects 45 | #' @param tar_dirs A list of root folders of targets-managed projects 46 | #' @param ... Additional arguments to tar_meta function 47 | #' @examples 48 | #' tar_dirs="" 49 | #' unitar_meta(tar_dirs) 50 | #' @export 51 | unitar_meta = function(tar_dirs=NULL, ...) { 52 | tar_dirs = get_tar_dirs(tar_dirs) 53 | unitar_exec(func=tar_meta, tar_dirs=tar_dirs, ...) 54 | } 55 | 56 | 57 | #' Gets a tar_dirs folder from either argument or R option 58 | #' 59 | #' @param tar_dirs Folder or list of folders (or NULL if using option) 60 | #' @export 61 | get_tar_dirs = function(tar_dirs=NULL) { 62 | if (is.null(tar_dirs)) { 63 | tar_dirs = getOption("tar_dirs") 64 | } 65 | if (is.null(tar_dirs)) { 66 | stop("Must provide tar_dirs or set option tar_dirs") 67 | } 68 | return(tar_dirs) 69 | } 70 | 71 | 72 | #' Load targets from a list of possible target repositories 73 | #' 74 | #' This looks in priority order through the list of tprojects, and, if it finds 75 | #' a matching target, it reads it in and returns it. 76 | #' 77 | #' @param tname The name of the target to query 78 | #' @param tar_dirs A priority list of root folders of targets-managed projects 79 | #' @export 80 | unitar_read = function(tname, tar_dirs=NULL) { 81 | tar_dirs = get_tar_dirs(tar_dirs) 82 | utmeta = unitar_meta(tar_dirs) 83 | for (i in seq_len(length(utmeta))) { 84 | tmeta = utmeta[[i]] 85 | if ("name" %in% colnames(tmeta) && tname %in% tmeta$name) { 86 | folder = tar_dirs[i] 87 | return(withr::with_dir(folder, tar_read_raw(tname))) 88 | } 89 | } 90 | } 91 | 92 | 93 | #' Load targets from a list of possible target repositories 94 | #' 95 | #' This looks in priority order through the list of tprojects, and, if it finds 96 | #' a matching target, it loads it silently into the workspace 97 | #' 98 | #' @param tname The name of the target to query 99 | #' @param tar_dirs A priority list of root folders of targets-managed projects 100 | #' @export 101 | unitar_load = function(tname, tar_dirs=NULL) { 102 | tar_dirs = get_tar_dirs(tar_dirs) 103 | utmeta = unitar_meta(tar_dirs) 104 | for (i in seq_len(length(utmeta))) { 105 | tmeta = utmeta[[i]] 106 | if ("name" %in% colnames(tmeta) && tname %in% tmeta$name) { 107 | folder = tar_dirs[i] 108 | withr::with_dir(folder, tar_load_raw(tname, envir=parent.frame())) 109 | return(invisible()) 110 | } 111 | } 112 | } 113 | 114 | 115 | #' Returns the path to the cache of the given target 116 | #' 117 | #' @param tname The name of the target to query 118 | #' @param tar_dirs A priority list of root folders of targets-managed projects 119 | #' @export 120 | unitar_path = function(tname, tar_dirs=NULL) { 121 | tar_dirs = get_tar_dirs(tar_dirs) 122 | utmeta = unitar_meta(tar_dirs) 123 | for (i in seq_len(length(utmeta))) { 124 | tmeta = utmeta[[i]] 125 | if ("name" %in% colnames(tmeta) && tname %in% tmeta$name) { 126 | folder = enforceTrailingSlash(tar_dirs[i]) 127 | return(paste0(folder, "_targets/objects/", tname)) 128 | } 129 | } 130 | } 131 | 132 | 133 | #' Reads a target given a direct path, likely output from \code{unitar_path} 134 | #' 135 | #' @param tpath The path to the target to read 136 | #' @export 137 | unitar_read_from_path = function(tpath) { 138 | folder = gsub("(.*)/_targets/objects/.*", "\\1", tpath) 139 | tname = gsub(".*_targets/objects/(.*)", "\\1", tpath) 140 | unitar_read(tname, folder) 141 | } 142 | 143 | 144 | # Here's a cross-project target factory 145 | #' Load targets across projects 146 | #' 147 | #' This function will allow you to load and track cached targets 148 | #' from other target projects. 149 | #' @param tname The name of the target to query 150 | #' @param tar_dirs A priority list of root folders of targets-managed projects 151 | #' @export 152 | unitar_read_xprj = function(tname, tar_dirs=NULL) { 153 | list( 154 | tar_target_raw("file", unitar_path(tname, tar_dirs), format = "file"), 155 | tar_target_raw(tname, quote(unitar_read_from_path(file))) 156 | ) 157 | } 158 | 159 | -------------------------------------------------------------------------------- /R/utility.R: -------------------------------------------------------------------------------- 1 | enforceTrailingSlash = function(folder) { 2 | enforceEdgeCharacter(folder, appendChar="/") 3 | } 4 | 5 | enforceEdgeCharacter = function(string, prependChar="", appendChar="") { 6 | if (string=="" | is.null(string)) { 7 | return(string) 8 | } 9 | if(!is.null(appendChar)) { 10 | if (substr(string,nchar(string), nchar(string)) != appendChar) { # +1 ? 11 | string = paste0(string, appendChar) 12 | } 13 | } 14 | if (!is.null(prependChar)) { 15 | if (substr(string,1,1) != prependChar) { # +1 ? 16 | string = paste0(prependChar, string) 17 | } 18 | } 19 | return(string) 20 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unitar 2 | 3 | Unitar is a simple R package that wraps the [targets](https://github.com/ropensci/targets) package. To use unitar, you will need to already be familiar with `targets`, because the functionality is just an extension of targets and the naming conventions all follow the targets package approach. Unitar adds new functionality to use targets that span projects, which is outside the scope of the base targets package. With unitar, you can very easily link two targets projects, loading in built targets from other projects so that you can share caches and computing across users and projects. 4 | 5 | Please see the vignette in the vignettes folder for installation and usage instructions. 6 | 7 | ## Install 8 | 9 | For now, install with: 10 | 11 | ``` 12 | devtools::install_github("databio/unitar") 13 | ``` 14 | -------------------------------------------------------------------------------- /inst/extdata/R/functions.R: -------------------------------------------------------------------------------- 1 | # functions.R 2 | create_plot <- function(data) { 3 | ggplot(data) + 4 | geom_histogram(aes(x = Ozone)) + 5 | theme_gray(24) 6 | } -------------------------------------------------------------------------------- /inst/extdata/_targets.R: -------------------------------------------------------------------------------- 1 | # _targets.R 2 | library("targets") 3 | library("simpleCache") 4 | library("unitar") 5 | source("R/functions.R") 6 | 7 | 8 | p = pepr::Project("metadata/project_config.yaml") 9 | 10 | options(tidyverse.quiet = TRUE) 11 | tar_option_set(packages = c("biglm", "tidyverse", "BiocProject")) 12 | get_dhs_112_path = function() { 13 | return ("/home/nsheff/code/incubator/pep_targets/Rcache/dhs112.RData") 14 | } 15 | 16 | local_proc_ref = function(tgt) { 17 | d = readRDS(tgt) 18 | mean(d) 19 | } 20 | 21 | list( 22 | tar_target( 23 | ext_ref2, 24 | peptar_path(p, "ref2"), 25 | format="file" 26 | ), 27 | tar_target( 28 | mean_ref2, 29 | local_proc_ref(ext_ref2) 30 | ), 31 | tar_target( #shared cache 32 | dhs112_file, 33 | get_dhs_112_path(), 34 | format="file" 35 | ), 36 | tar_target( 37 | dhs112, 38 | simpleCache("dhs112", cacheDir="/home/nsheff/code/incubator/pep_targets/Rcache") 39 | ), 40 | tar_target( 41 | raw_data_file, 42 | "data/raw_data.csv", 43 | format = "file" 44 | ), 45 | tar_target( 46 | raw_data, 47 | read_csv(raw_data_file, col_types = cols()) 48 | ) 49 | ) 50 | -------------------------------------------------------------------------------- /inst/extdata/_targets/meta/meta: -------------------------------------------------------------------------------- 1 | name|type|data|command|depend|seed|path|time|size|bytes|format|iteration|parent|children|seconds|warnings|error 2 | unitar_exec|function|fffa05098f72825f|||||||||||||| 3 | peptar_folders|function|2e016411081aa6c0|||||||||||||| 4 | my_meta|function|a2dbbfe1f2668801|||||||||||||| 5 | peptar_exec|function|9302a82bbd0e1e15|||||||||||||| 6 | unitar_make|function|74671874c7c9990f|||||||||||||| 7 | unitar_meta|function|169d2f223b4dff11|||||||||||||| 8 | peptar_make|function|54128d9363ef4157|||||||||||||| 9 | unitar_load|function|182645441efcb71c|||||||||||||| 10 | unitar_path|function|053594a3c98de519|||||||||||||| 11 | peptar_meta|function|1fa06dae91a497f5|||||||||||||| 12 | peptar_load|function|f58737158e3d42d7|||||||||||||| 13 | peptar_path|function|a4a4e423f4363109|||||||||||||| 14 | project_config_bioc|stem|b39356ae076575dd|1478d67701dd4076|ef46db3751d8e999|356178347|example_peps/example_BiocProject_remote/project_config.yaml|46f3bd492e30a1bf|f02b269bc80ae470|129|file|vector|||0.615|| 15 | project_config|stem|0af797f926ee7551|ef127fbee0ee7627|ef46db3751d8e999|-403631507|example_peps/example_basic/project_config.yaml|0ed877b554c68d18|e5051d725c55b608|52|file|vector|||0.622|| 16 | biocpep|stem|db970b873bc8b7bc|f6d97b3f3bf805a3|38c18177cc12ffcf|129114666|_targets/objects/biocpep|22402af36fcfab36|583e8d6cd3131262|461|rds|vector|||0.009|| 17 | biocpeploaded|stem|fdac7f548bb0bae1|677400f1d3a0c6dc|38c18177cc12ffcf|931044283|_targets/objects/biocpeploaded|9a9582309a7ddac9|4fd82add577cbaa9|1397|rds|vector|||2.125|| 18 | mypep|stem|8d4fc127e023c34e|3c8a0e45e3fb9490|9e941e47da99166c|-586154504|_targets/objects/mypep|7401bafc14ebf757|e5d2926a8e144e03|390|rds|vector|||0.005|| 19 | data|stem|affe8f655c92c399|df3101ce91c63e21|6769840ca8af2331|1588979285|_targets/objects/data|ba50a53977e4147a|29b7562759c60885|336|rds|vector|||0.003|| 20 | fit|stem|5075c9e42b69cc46|aa0df6c5dbd10537|ac20c212a5fcb398|1780184594|_targets/objects/fit|ee7c309d7f754b5d|1968f0930b01f4af|731|rds|vector|||0.002|| 21 | hist|stem|d8123eed3621f91e|68877181ab74e51e|8a2373d9032cdbec|-1026346201|_targets/objects/hist|1a3e92a09c996277|14602498545ca866|43369|rds|vector|||0.008|| 22 | get_dhs_112_path|function|d5611fc877efc935|||||||||||||| 23 | create_plot|function|ab9344eabf5bc41d|||||||||||||| 24 | p|object|9ad8f28b4ad6376f|||||||||||||| 25 | local_proc_ref|function|23b718bf02022868|||||||||||||| 26 | dhs112|stem|335089219d650838|3074ad8c0f9165f2|cf2cbc26a8b549f5|-1269662132|_targets/objects/dhs112|b34898918bdd3472|1193f60a6442e075|7744|rds|vector|||1.104|| 27 | raw_data_file|stem|0c1295bc7428798a|b6df0c34fc22d1b9|ef46db3751d8e999|2110307107|data/raw_data.csv|8e6e929e175b2a04|c6fbfb9abe15abfd|72|file|vector|||0.613|| 28 | ext_ref2|stem|cb3801e6783e10cf|ebc362e2925129fe|4195ebf92c4ef7e3|483650588|/home/nsheff/code/unitar/inst/extdata/target_projects/refdata2/_targets/objects/ref2|cdf005c7b94850ab|d8c8f7fcf9e353fc|646|file|vector|||0.005|| 29 | dhs112_file|stem|83b6d30edf8a5e84|8ceb79b129f6ea3f|e0674cd29821e79d|1555617637|/home/nsheff/code/incubator/pep_targets/Rcache/dhs112.RData|796591c21fe64a8a|6137997a6f08d95b|7773|file|vector|||1.234|| 30 | raw_data|stem|c4cfda2f195137fe|000ed0cc054f0d35|d3b8523ae5a0cd3e|-979620141|_targets/objects/raw_data|4b7f742b3bd1d231|16c4820798935f49|337|rds|vector|||0.029|| 31 | mean_ref2|stem|f86e2bf267b45d9a|aba262c8b75d1447|7cd8c059e3aaec72|1403016961|_targets/objects/mean_ref2|9460cc996377f528|a96c250e4b19e03e|54|rds|vector|||0.001|| 32 | get_dhs_112_path|function|d5611fc877efc935 33 | create_plot|function|ab9344eabf5bc41d 34 | p|object|9ad8f28b4ad6376f 35 | local_proc_ref|function|23b718bf02022868 36 | dhs112|stem|335089219d650838|3074ad8c0f9165f2|cf2cbc26a8b549f5|-1269662132|_targets/objects/dhs112|b34898918bdd3472|1193f60a6442e075|7744|rds|vector|||1.104|| 37 | raw_data_file|stem|0c1295bc7428798a|b6df0c34fc22d1b9|ef46db3751d8e999|2110307107|data/raw_data.csv|8e6e929e175b2a04|c6fbfb9abe15abfd|72|file|vector|||0.613|| 38 | ext_ref2|stem|cb3801e6783e10cf|ebc362e2925129fe|4195ebf92c4ef7e3|483650588|/home/nsheff/code/unitar/inst/extdata/target_projects/refdata2/_targets/objects/ref2|b51822e0de71f9af|d8c8f7fcf9e353fc|646|file|vector|||0.005|| 39 | dhs112_file|stem|83b6d30edf8a5e84|8ceb79b129f6ea3f|e0674cd29821e79d|1555617637|/home/nsheff/code/incubator/pep_targets/Rcache/dhs112.RData|796591c21fe64a8a|6137997a6f08d95b|7773|file|vector|||1.234|| 40 | raw_data|stem|c4cfda2f195137fe|000ed0cc054f0d35|d3b8523ae5a0cd3e|-979620141|_targets/objects/raw_data|4b7f742b3bd1d231|16c4820798935f49|337|rds|vector|||0.029|| 41 | mean_ref2|stem|f86e2bf267b45d9a|aba262c8b75d1447|7cd8c059e3aaec72|1403016961|_targets/objects/mean_ref2|9460cc996377f528|a96c250e4b19e03e|54|rds|vector|||0.001|| 42 | -------------------------------------------------------------------------------- /inst/extdata/_targets/meta/progress: -------------------------------------------------------------------------------- 1 | name|type|parent|branches|progress 2 | -------------------------------------------------------------------------------- /inst/extdata/_targets/objects/dhs112: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/databio/unitar/7b807f15bfde6f05e13e99f060e6a45b5b41cfa5/inst/extdata/_targets/objects/dhs112 -------------------------------------------------------------------------------- /inst/extdata/_targets/objects/mean_ref2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/databio/unitar/7b807f15bfde6f05e13e99f060e6a45b5b41cfa5/inst/extdata/_targets/objects/mean_ref2 -------------------------------------------------------------------------------- /inst/extdata/_targets/objects/raw_data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/databio/unitar/7b807f15bfde6f05e13e99f060e6a45b5b41cfa5/inst/extdata/_targets/objects/raw_data -------------------------------------------------------------------------------- /inst/extdata/data/raw_data.csv: -------------------------------------------------------------------------------- 1 | Ozone,Solar.R,Wind,Temp,Month,Day 2 | 36,118,8.0,72,5,2 3 | 12,149,12.6,74,5,345 -------------------------------------------------------------------------------- /inst/extdata/metadata/project_config.yaml: -------------------------------------------------------------------------------- 1 | pep_version: "2.0.0" 2 | sample_table: sample_table.csv 3 | 4 | bioconductor: 5 | readFunName: readRemoteData 6 | readFunPath: readRemoteData.R 7 | 8 | tprojects: 9 | - ../target_projects/refdata1/ 10 | - ../target_projects/refdata2/ 11 | - ../ 12 | -------------------------------------------------------------------------------- /inst/extdata/metadata/project_config_resize.yaml: -------------------------------------------------------------------------------- 1 | pep_version: "2.0.0" 2 | sample_table: sample_table.csv 3 | 4 | bioconductor: 5 | read_fun_name: readRemoteData_resize 6 | read_fun_path: readRemoteData_resize.R 7 | -------------------------------------------------------------------------------- /inst/extdata/metadata/readRemoteData.R: -------------------------------------------------------------------------------- 1 | readRemoteData = function(project) { 2 | # get the data from the Project object 3 | url = pepr::sampleTable(project)$remote_url[[1]] 4 | # download the file 5 | bfc = BiocFileCache::BiocFileCache(cache=tempdir(),ask=FALSE) 6 | path = BiocFileCache::bfcrpath(bfc, url) 7 | # read it in 8 | df = read.table(path) 9 | # formatting 10 | colnames(df) = c('chr', 'start', 'end', 'name') 11 | # convert to GRanges object 12 | GenomicRanges::GRanges(df) 13 | } 14 | -------------------------------------------------------------------------------- /inst/extdata/metadata/readRemoteData_resize.R: -------------------------------------------------------------------------------- 1 | readRemoteData_resize = function(project, resize.width) { 2 | # get the data from the Project config 3 | url = pepr::sampleTable(project)$remote_url[[1]] 4 | # download the file 5 | bfc = BiocFileCache::BiocFileCache(cache=tempdir(),ask=FALSE) 6 | path = BiocFileCache::bfcrpath(bfc, url) 7 | # read it in 8 | df = read.table(path) 9 | # formatting 10 | colnames(df) = c('chr', 'start', 'end', 'name') 11 | # convert to GRanges object 12 | GenomicRanges::resize(x=GenomicRanges::GRanges(df), width=resize.width) 13 | } 14 | -------------------------------------------------------------------------------- /inst/extdata/metadata/sample_table.csv: -------------------------------------------------------------------------------- 1 | sample_name,remote_url 2 | encodeRegions,http://hgdownload.cse.ucsc.edu/goldenpath/hg19/database/encodeRegions 3 | -------------------------------------------------------------------------------- /inst/extdata/target_projects/crossfactory/_targets.R: -------------------------------------------------------------------------------- 1 | # _targets.R 2 | library("targets") 3 | library("unitar") 4 | options(tidyverse.quiet = TRUE) 5 | 6 | target_folders="../refdata1" 7 | 8 | list( 9 | unitar_read_xprj("big_data_set", target_folders) 10 | ) 11 | -------------------------------------------------------------------------------- /inst/extdata/target_projects/crossproj/_targets.R: -------------------------------------------------------------------------------- 1 | # _targets.R 2 | library("targets") 3 | library("unitar") 4 | options(tidyverse.quiet = TRUE) 5 | 6 | 7 | # Function that takes an external dataset (from another targets project), 8 | # and returns a modified version for this project. 9 | local_filter_big_reference_data = function(big_data_set_path) { 10 | big_data_set = unitar_read_from_path(big_data_set_path) 11 | big_data_set[big_data_set > 2] 12 | } 13 | 14 | list( 15 | tar_target( 16 | big_data_set_path, 17 | unitar_path("../refdata1", "big_data_set"), 18 | format = "file" 19 | ), 20 | tar_target( 21 | filtered_data_set, 22 | local_filter_big_reference_data(big_data_set_path) 23 | ) 24 | ) 25 | -------------------------------------------------------------------------------- /inst/extdata/target_projects/refdata1/_targets.R: -------------------------------------------------------------------------------- 1 | # _targets.R 2 | library("targets") 3 | options(tidyverse.quiet = TRUE) 4 | tar_option_set(packages = c("biglm", "tidyverse")) 5 | 6 | build_big_reference_data = function() { 7 | rnorm(20000) 8 | } 9 | 10 | filter_big_reference_data = function(big_data_set) { 11 | big_data_set[big_data_set > 0] 12 | } 13 | 14 | list( 15 | tar_target( 16 | big_data_set, 17 | build_big_reference_data() 18 | ), 19 | tar_target( 20 | filtered_data, 21 | filter_big_reference_data(big_data_set) 22 | ) 23 | ) 24 | -------------------------------------------------------------------------------- /inst/extdata/target_projects/refdata2/_targets.R: -------------------------------------------------------------------------------- 1 | # _targets.R 2 | library("targets") 3 | options(tidyverse.quiet = TRUE) 4 | tar_option_set(packages = c("biglm", "tidyverse")) 5 | 6 | ref_data_2 = function() { 7 | runif(100) 8 | } 9 | 10 | filter_2 = function(big_data_set) { 11 | big_data_set[big_data_set > 0] 12 | } 13 | 14 | list( 15 | tar_target( 16 | ref2, 17 | ref_data_2() 18 | ), 19 | tar_target( 20 | filt_ref2, 21 | filter_2(ref2) 22 | ) 23 | ) 24 | -------------------------------------------------------------------------------- /man/build_pep_resource_targets.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{build_pep_resource_targets} 4 | \alias{build_pep_resource_targets} 5 | \title{Target factory to build targets from a PEP} 6 | \usage{ 7 | build_pep_resource_targets(config) 8 | } 9 | \arguments{ 10 | \item{config}{Path to PEP config file} 11 | } 12 | \description{ 13 | Target factory to build targets from a PEP 14 | } 15 | -------------------------------------------------------------------------------- /man/build_pep_resource_targets_prj.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{build_pep_resource_targets_prj} 4 | \alias{build_pep_resource_targets_prj} 5 | \title{Target factory to build targets from a PEP} 6 | \usage{ 7 | build_pep_resource_targets_prj(p) 8 | } 9 | \arguments{ 10 | \item{p}{PEP defining targets to build} 11 | } 12 | \description{ 13 | Target factory to build targets from a PEP 14 | } 15 | -------------------------------------------------------------------------------- /man/build_pep_resource_targets_prj_old.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{build_pep_resource_targets_prj_old} 4 | \alias{build_pep_resource_targets_prj_old} 5 | \title{Target factory to build targets from a PEP} 6 | \usage{ 7 | build_pep_resource_targets_prj_old(p) 8 | } 9 | \arguments{ 10 | \item{p}{PEP defining targets to build} 11 | } 12 | \description{ 13 | Target factory to build targets from a PEP 14 | } 15 | -------------------------------------------------------------------------------- /man/get_tar_dirs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{get_tar_dirs} 4 | \alias{get_tar_dirs} 5 | \title{Gets a tar_dirs folder from either argument or R option} 6 | \usage{ 7 | get_tar_dirs(tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tar_dirs}{Folder or list of folders (or NULL if using option)} 11 | } 12 | \description{ 13 | Gets a tar_dirs folder from either argument or R option 14 | } 15 | -------------------------------------------------------------------------------- /man/load_custom.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{load_custom} 4 | \alias{load_custom} 5 | \title{Helper function to build targets from function calls with custom args} 6 | \usage{ 7 | load_custom(tname, func, argvals, argtypes, argnames = NULL) 8 | } 9 | \arguments{ 10 | \item{tname}{Target name} 11 | 12 | \item{func}{Function to call} 13 | 14 | \item{argvals}{List of arg values} 15 | 16 | \item{argtypes}{List of arg types (use 'symbol' for targets)} 17 | 18 | \item{argnames}{List of arg names} 19 | } 20 | \description{ 21 | This function is a target factory that will produce a target for an 22 | arbitrary function call with arbitrary arguments and values. 23 | } 24 | -------------------------------------------------------------------------------- /man/load_custom_old.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{load_custom_old} 4 | \alias{load_custom_old} 5 | \title{Helper function to build targets from function calls with custom args} 6 | \usage{ 7 | load_custom_old(tname, func, argvals, argtypes, argnames) 8 | } 9 | \arguments{ 10 | \item{tname}{Target name} 11 | 12 | \item{func}{Function to call} 13 | 14 | \item{argvals}{List of arg values} 15 | 16 | \item{argtypes}{List of arg types (use 'symbol' for targets)} 17 | 18 | \item{argnames}{List of arg names} 19 | } 20 | \description{ 21 | This function is a target factory that will produce a target for an 22 | arbitrary function call with arbitrary arguments and values. 23 | } 24 | -------------------------------------------------------------------------------- /man/load_external_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{load_external_file} 4 | \alias{load_external_file} 5 | \title{Helper function to build target functions to track and read files} 6 | \usage{ 7 | load_external_file(sample_name, rootpath, filepath, func) 8 | } 9 | \arguments{ 10 | \item{sample_name}{Name of the target} 11 | 12 | \item{rootpath}{Path to where resource files are kept} 13 | 14 | \item{filepath}{Relative path to file} 15 | 16 | \item{func}{Function to use to load file in R} 17 | } 18 | \description{ 19 | This is a target factory that will produce a target to track 20 | a file, and then to read that file using an arbitrary function 21 | provided by the user. 22 | } 23 | -------------------------------------------------------------------------------- /man/my_meta.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{my_meta} 4 | \alias{my_meta} 5 | \title{Simplified tar_meta function with only a few fields.} 6 | \usage{ 7 | my_meta() 8 | } 9 | \description{ 10 | Simplified tar_meta function with only a few fields. 11 | } 12 | -------------------------------------------------------------------------------- /man/peptar_dirs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_dirs} 4 | \alias{peptar_dirs} 5 | \title{Retrieves tar folders from PEP} 6 | \usage{ 7 | peptar_dirs(p) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | } 12 | \description{ 13 | Retrieves tar folders from PEP 14 | } 15 | -------------------------------------------------------------------------------- /man/peptar_exec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_exec} 4 | \alias{peptar_exec} 5 | \title{Execute a function across all target folders specified in the current Project.} 6 | \usage{ 7 | peptar_exec(p, ...) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{...}{Function and parameters to execute} 13 | } 14 | \description{ 15 | Execute a function across all target folders specified in the current Project. 16 | } 17 | -------------------------------------------------------------------------------- /man/peptar_load.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_load} 4 | \alias{peptar_load} 5 | \title{Load a target invisibly, from all your PEP target folders} 6 | \usage{ 7 | peptar_load(p, tname) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{tname}{The name of the target to query} 13 | } 14 | \description{ 15 | Load a target invisibly, from all your PEP target folders 16 | } 17 | -------------------------------------------------------------------------------- /man/peptar_make.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_make} 4 | \alias{peptar_make} 5 | \title{Execute tar_make across all target folders specified in the current Project.} 6 | \usage{ 7 | peptar_make(p, ...) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{...}{Parameters to pass to tar_make} 13 | } 14 | \description{ 15 | Execute tar_make across all target folders specified in the current Project. 16 | } 17 | -------------------------------------------------------------------------------- /man/peptar_meta.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_meta} 4 | \alias{peptar_meta} 5 | \title{Execute tar_meta across all target folders specified in the current Project.} 6 | \usage{ 7 | peptar_meta(p, ...) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{...}{Parameters to pass to tar_meta} 13 | } 14 | \description{ 15 | Execute tar_meta across all target folders specified in the current Project. 16 | } 17 | -------------------------------------------------------------------------------- /man/peptar_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_path} 4 | \alias{peptar_path} 5 | \title{Find path to a target from all your PEP target folders} 6 | \usage{ 7 | peptar_path(p, tname) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{tname}{The name of the target to query} 13 | } 14 | \description{ 15 | Find path to a target from all your PEP target folders 16 | } 17 | -------------------------------------------------------------------------------- /man/peptar_read.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/peptar.R 3 | \name{peptar_read} 4 | \alias{peptar_read} 5 | \title{Load a target and return it, from all your PEP target folders} 6 | \usage{ 7 | peptar_read(p, tname) 8 | } 9 | \arguments{ 10 | \item{p}{A pepr::Project object representing your current project.} 11 | 12 | \item{tname}{The name of the target to query} 13 | } 14 | \description{ 15 | Load a target and return it, from all your PEP target folders 16 | } 17 | -------------------------------------------------------------------------------- /man/source_target_functions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{source_target_functions} 4 | \alias{source_target_functions} 5 | \title{Helper function to load target-creation functions from PEP config} 6 | \usage{ 7 | source_target_functions(config) 8 | } 9 | \arguments{ 10 | \item{config}{Path to the PEP config file for the resource targets list.} 11 | } 12 | \description{ 13 | This function simple sources the target functions specified in 14 | a 'target_load_functions' attribute of a PEP. It's meant to be 15 | called from a _targets.R file, so you don't have to source those 16 | scripts individually, but can consolidate the configuration into 17 | the PEP config file. 18 | } 19 | -------------------------------------------------------------------------------- /man/tar_list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{tar_list} 4 | \alias{tar_list} 5 | \title{List the targets available for loading} 6 | \usage{ 7 | tar_list(n = 50, fields = c("name", "time", "bytes"), ...) 8 | } 9 | \arguments{ 10 | \item{n}{Number of targets to print (with sensible default)} 11 | 12 | \item{fields}{Fields passed to tar_meta (with sensible default)} 13 | 14 | \item{...}{Any other arguments to pass to tar_meta} 15 | } 16 | \description{ 17 | This function is useful for resource projects, where you want to see 18 | what you have available. It's just a simple wrapper that provides 19 | some sensible defaults to \code{tar_meta} for this use case. 20 | In the future I'd like it if this included a text description of the target 21 | } 22 | -------------------------------------------------------------------------------- /man/tar_pep_foreach.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{tar_pep_foreach} 4 | \alias{tar_pep_foreach} 5 | \title{Target factory for arbitrary functions on PEP samples} 6 | \usage{ 7 | tar_pep_foreach( 8 | p, 9 | tpattern, 10 | func, 11 | argvals_pattern, 12 | argtypes, 13 | argnames = NULL, 14 | combine_tname = NULL, 15 | combine_func_name = "list" 16 | ) 17 | } 18 | \arguments{ 19 | \item{p}{PEP.} 20 | 21 | \item{tpattern}{Target name, with patterns allowed} 22 | 23 | \item{func}{Function to call} 24 | 25 | \item{argvals_pattern}{List of arg values, with patterns allowed} 26 | 27 | \item{argtypes}{List of arg types (use 'symbol' for targets)} 28 | 29 | \item{argnames}{List of arg names} 30 | 31 | \item{combine_tname}{Target name for a combined target that merges all of the individual targets} 32 | 33 | \item{combine_func_name}{Character vector of a function that will combine them.} 34 | } 35 | \description{ 36 | This target factory creates a target for each element of a PEP. You can 37 | name the targets using a pattern that uses brackets to indicate sample 38 | attributes, like `{sample_name}`. 39 | } 40 | -------------------------------------------------------------------------------- /man/track_external_target.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{track_external_target} 4 | \alias{track_external_target} 5 | \title{Experimental target factory to track but not duplicate external files.} 6 | \usage{ 7 | track_external_target(tname, ext_tname, func) 8 | } 9 | \description{ 10 | EXPERIMENTAL. This function is just a concept that has not been tested. 11 | It will change or disappear in future versions. 12 | } 13 | -------------------------------------------------------------------------------- /man/unitar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \docType{package} 4 | \name{unitar} 5 | \alias{unitar} 6 | \title{Targets that span projects} 7 | \description{ 8 | Targets! now shared! 9 | } 10 | \author{ 11 | Nathan Sheffield 12 | } 13 | -------------------------------------------------------------------------------- /man/unitar_exec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_exec} 4 | \alias{unitar_exec} 5 | \title{Execute a function across a list of tar_target folders} 6 | \usage{ 7 | unitar_exec(func = tar_make, tar_dirs = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{func}{Function to execute} 11 | 12 | \item{tar_dirs}{A list of root folders of targets-managed projects} 13 | 14 | \item{...}{Additional arguments to function} 15 | } 16 | \description{ 17 | Execute a function across a list of tar_target folders 18 | } 19 | \examples{ 20 | tar_dirs="" 21 | unitar_exec(tar_dirs, tar_meta) 22 | } 23 | -------------------------------------------------------------------------------- /man/unitar_list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/resource_projects.R 3 | \name{unitar_list} 4 | \alias{unitar_list} 5 | \title{List all targets available across all projects} 6 | \usage{ 7 | unitar_list(n = 50, fields = c("name", "time", "bytes"), tar_dirs = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{n}{Number of targets to print (with sensible default)} 11 | 12 | \item{fields}{Fields passed to tar_meta (with sensible default)} 13 | 14 | \item{tar_dirs}{Target directories to search. NULL will use global option (the default).} 15 | 16 | \item{...}{Any other arguments to pass to tar_meta} 17 | } 18 | \description{ 19 | If you have available targets from multiple projects, you might be 20 | interested in a quick look at them to see what you have available to load. 21 | This function just wraps tar_meta with a few defaults that are convenient 22 | for this purpose: it increase the default number of targets shown, and 23 | reduces the number of fields. It also runs it across all registered targets 24 | projects. 25 | } 26 | -------------------------------------------------------------------------------- /man/unitar_load.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_load} 4 | \alias{unitar_load} 5 | \title{Load targets from a list of possible target repositories} 6 | \usage{ 7 | unitar_load(tname, tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tname}{The name of the target to query} 11 | 12 | \item{tar_dirs}{A priority list of root folders of targets-managed projects} 13 | } 14 | \description{ 15 | This looks in priority order through the list of tprojects, and, if it finds 16 | a matching target, it loads it silently into the workspace 17 | } 18 | -------------------------------------------------------------------------------- /man/unitar_make.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_make} 4 | \alias{unitar_make} 5 | \title{A global tar_make function that spans all tar_target folders} 6 | \usage{ 7 | unitar_make(tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tar_dirs}{A list of root folders of targets-managed projects} 11 | } 12 | \description{ 13 | A global tar_make function that spans all tar_target folders 14 | } 15 | \examples{ 16 | tar_dirs="" 17 | unitar_meta(tar_dirs) 18 | } 19 | -------------------------------------------------------------------------------- /man/unitar_meta.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_meta} 4 | \alias{unitar_meta} 5 | \title{Call tar_meta across multiple tar projects} 6 | \usage{ 7 | unitar_meta(tar_dirs = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{tar_dirs}{A list of root folders of targets-managed projects} 11 | 12 | \item{...}{Additional arguments to tar_meta function} 13 | } 14 | \description{ 15 | Call tar_meta across multiple tar projects 16 | } 17 | \examples{ 18 | tar_dirs="" 19 | unitar_meta(tar_dirs) 20 | } 21 | -------------------------------------------------------------------------------- /man/unitar_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_path} 4 | \alias{unitar_path} 5 | \title{Returns the path to the cache of the given target} 6 | \usage{ 7 | unitar_path(tname, tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tname}{The name of the target to query} 11 | 12 | \item{tar_dirs}{A priority list of root folders of targets-managed projects} 13 | } 14 | \description{ 15 | Returns the path to the cache of the given target 16 | } 17 | -------------------------------------------------------------------------------- /man/unitar_read.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_read} 4 | \alias{unitar_read} 5 | \title{Load targets from a list of possible target repositories} 6 | \usage{ 7 | unitar_read(tname, tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tname}{The name of the target to query} 11 | 12 | \item{tar_dirs}{A priority list of root folders of targets-managed projects} 13 | } 14 | \description{ 15 | This looks in priority order through the list of tprojects, and, if it finds 16 | a matching target, it reads it in and returns it. 17 | } 18 | -------------------------------------------------------------------------------- /man/unitar_read_from_path.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_read_from_path} 4 | \alias{unitar_read_from_path} 5 | \title{Reads a target given a direct path, likely output from \code{unitar_path}} 6 | \usage{ 7 | unitar_read_from_path(tpath) 8 | } 9 | \arguments{ 10 | \item{tpath}{The path to the target to read} 11 | } 12 | \description{ 13 | Reads a target given a direct path, likely output from \code{unitar_path} 14 | } 15 | -------------------------------------------------------------------------------- /man/unitar_read_xprj.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unitar.R 3 | \name{unitar_read_xprj} 4 | \alias{unitar_read_xprj} 5 | \title{Load targets across projects} 6 | \usage{ 7 | unitar_read_xprj(tname, tar_dirs = NULL) 8 | } 9 | \arguments{ 10 | \item{tname}{The name of the target to query} 11 | 12 | \item{tar_dirs}{A priority list of root folders of targets-managed projects} 13 | } 14 | \description{ 15 | This function will allow you to load and track cached targets 16 | from other target projects. 17 | } 18 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(unitar) 3 | 4 | test_check("unitar") 5 | -------------------------------------------------------------------------------- /tests/testthat/testall.R: -------------------------------------------------------------------------------- 1 | # Unit tests 2 | library("unitar") 3 | 4 | test_that("load", { 5 | project_root = system.file("extdata", "target_projects", package="unitar") 6 | 7 | project_subfolders = list.files(project_root) 8 | project_folders = paste(project_root, project_subfolders, sep="/") 9 | 10 | unitar::unitar_make(project_folders) 11 | 12 | big_data_set = unitar::unitar_read("big_data_set", project_folders) 13 | testthat::expect_equal(big_data_set[1] , -1.39609368) 14 | }) 15 | -------------------------------------------------------------------------------- /vignettes/unitar.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Getting started with unitar" 3 | author: "Nathan Sheffield" 4 | date: "`r Sys.Date()`" 5 | vignette: > 6 | %\VignetteIndexEntry{1. Getting started with unitar} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | %\VignetteEncoding{UTF-8} 9 | --- 10 | 11 | ```{r, echo=FALSE} 12 | # These settings make the vignette prettier 13 | # knitr::opts_chunk$set(results="hold", collapse=FALSE, message=FALSE) 14 | ``` 15 | 16 | # Introduction 17 | 18 | Unitar is a simple R package that wraps the [targets](https://github.com/ropensci/targets) package. To use unitar, you will need to already be familiar with `targets`, because the functionality is just an extension of targets and the naming conventions all follow the targets package approach. Unitar adds new functionality to use targets that span projects, which is outside the scope of the base targets package. With unitar, you can very easily link two targets projects, loading in built targets from other projects so that you can share caches and computing across users and projects. 19 | 20 | 21 | ## Installing unitar 22 | 23 | Install `unitar` like this: 24 | 25 | ```{r, eval=FALSE} 26 | devtools::install_github("databio/unitar") 27 | ``` 28 | 29 | ## Three modes of use 30 | 31 | There are three primary ways to use `unitar`, which differ in the way they treat external targets. External targets are targets that are not part of the current project, but belong to a different targets project. The 3 modes are: 32 | 33 | 1. **Basic loading of external targets**: Load targets computed in other projects. Don't track these in the current targets project, just re-use them, and let all tracking happen in the external project. This is the basic case. 34 | 35 | 2. **Load and track external targets locally**: Load external targets and also track them here. Duplicate external caches into local caches. If the external data changes, it will update the local caches, and all local files for the current project. 36 | 37 | 3. **Load and track external targets, but don't duplicate caches**: Load external targets and track the original files here, but don't duplicate them into here. This can you do by just adding a "file" target with the external target file, and then writing functions that use this to produce whatever derived targets you need. This way, your targets will update if the external target changes, but you don't have to pay the cost of storing the cache twice. 38 | 39 | For my use case, #3 is the most useful, but the others may also be useful depending on what you want. 40 | 41 | ### Use case #1: Basic loading of external targets 42 | 43 | In the [target_projects](/target_projects), we have 2 subfolders. Each of these represents a separate project that uses targets; you can find a `_targets.R` file in each subfolder. These represent your typical, independent targets project folders. 44 | 45 | Here's how you'd load these targets in using `unitar_read`: 46 | 47 | ```{r} 48 | library("unitar") 49 | project_root = system.file("extdata", "target_projects", package="unitar") 50 | 51 | project_subfolders = list.files(project_root) 52 | project_folders = paste(project_root, project_subfolders, sep="/") 53 | 54 | unitar::unitar_make(project_folders) 55 | 56 | big_data_set = unitar::unitar_read("big_data_set", project_folders) 57 | head(big_data_set) 58 | ``` 59 | 60 | `unitar_read` works like `tar_read`, but you give it a priority list of targets folders to search, and so it can search outside your current targets environment. This way you can share built targets across projects. 61 | 62 | `unitar_meta` works like `tar_meta`, but runs across all the given project folders: 63 | 64 | ```{r} 65 | unitar_meta(project_folders) 66 | ``` 67 | 68 | I prefer the `*_read` approach because it's explicit, but if you prefer to use the common R idiom of loading data using function side effects, you can also use `unitar_load`, which mimics the `tar_load` functionality: 69 | 70 | ```{r} 71 | unitar_load("big_data_set", project_folders) 72 | head(big_data_set) 73 | ``` 74 | 75 | ### Use case #2: Load and track external targets locally 76 | 77 | If there's a target in an external project that you want to use in your current project, the basic way (above) just loaded the target in from that other project. Another way to do it is to actually track that external target as a target in the local project. The difference is that with this way, you'll duplicate the cache of the file into the local folder. This duplication could be either an advantage or a disadvantage. If you want your project to be self-contained, with all your targets in the same folder, then you may want to track the files like that. 78 | 79 | To do this, we will need to add new targets to the our local `_targets.R`, so it will track the external targets. To do this, use `unitar_read_xprj()` like this: 80 | 81 | 82 | ```{r} 83 | # _targets.R 84 | 85 | library("unitar") 86 | tar_dirs="../refdata1" # external targets projects you want to track 87 | 88 | list( 89 | unitar_read_xprj("big_data_set", tar_dirs) 90 | ) 91 | ``` 92 | 93 | So, it's pretty simple, really. You can think of this as just registering any external targets into your local targets list. The `unitar_read_xprj` is a target factory that makes this super easy. 94 | 95 | ### Use case #3: Tracking external targets but not duplicating 96 | 97 | Finally, there's a third approach. What if you want to track the other external targets, so your stuff updates when they change, but you don't want to duplicate large caches into your local folder? This leads to option 3. Here, what you want to do is add the external file to your target list so it gets tracked, and use that as input, and then use `unitar_read_from_path` in a local function to process that data into the subset you want to keep locally. 98 | 99 | Here's an example: 100 | 101 | ```{r} 102 | # _targets.R 103 | library("targets") 104 | library("unitar") 105 | 106 | # Function that takes an external dataset (from another targets project), 107 | # and returns a modified version for this project. 108 | local_filter_big_reference_data = function(big_data_set_path) { 109 | big_data_set = unitar_read_from_path(big_data_set_path) 110 | big_data_set[big_data_set > 2] 111 | } 112 | 113 | list( 114 | tar_target( 115 | big_data_set_path, 116 | unitar_path("big_data_set", "../refdata1"), 117 | format = "file" 118 | ), 119 | tar_target( 120 | filtered_data_set, 121 | local_filter_big_reference_data(big_data_set_path) 122 | ) 123 | ) 124 | 125 | ``` 126 | 127 | Now, if `big_data_set` (from an external project) changes, that will invalidate your `filtered_data_set`, which will be recomputed. But you don't actually duplicate `big_data_set` into your local targets cache, saving space. 128 | 129 | 130 | ## Avoiding passing the tar_dirs 131 | 132 | Regardless of which of these options we choose, we're going to end up making calls in our scripts like thiS: 133 | 134 | ```{r} 135 | big_data_set = unitar::unitar_read("big_data_set", project_folders) 136 | ``` 137 | 138 | But it's more work to set up and pass around a `projects_folders` variable like this, relative to the simpler `targest::tar_read("big_data_set")` you could do in a local targets project. To get around this, all relevant `unitar` functions provide the ability to use R `options` as global variables, so you can set them once and then not worry about passing the project folders with every call. It's pretty easy to set up: 139 | 140 | ```{r} 141 | options(tar_dirs=project_folders) 142 | ``` 143 | 144 | Then you just leave that argument off of the `unitar_*` calls: 145 | 146 | ```{r} 147 | big_data_set = unitar::unitar_read("big_data_set") 148 | ``` 149 | 150 | Now, it's just as easy as using `targets` natively, but you get the added power of grabbing targets from multiple projects. 151 | 152 | 153 | ## Configuring external target folders with pepr 154 | 155 | The one possible place where this may not be ideal is if you want to maintain separate lists of folders, and query them separately. Using the `options(tar_dirs=project_folders)` approach limits you to a single `project_folders` variable. Alternatively, you can provide a list of folders in a configuration file for your project. `unitar` uses a project configuration file in [standard PEP format](http://pep.databio.org). You specify a list of target folders using the `tprojects` attribute, which may specify paths either absolute or relative to the configuration file. 156 | 157 | For example, the `project_config.yaml` file for the above `project_folders` example might look like this: 158 | 159 | ``` 160 | pep_version: "2.0.0" 161 | 162 | tprojects: 163 | - ../target_projects/refdata1/ 164 | - ../target_projects/refdata2/ 165 | - ../ 166 | ``` 167 | 168 | To make this work, `unitar` provides a series of `peptar_*` functions that work like the `unitar_*` functions above, but operate on PEPs that you can configure with your list of project folders. 169 | 170 | ```{r} 171 | pep_config = system.file("extdata", "metadata/project_config.yaml", package="unitar") 172 | p = pepr::Project(pep_config) 173 | 174 | unitar::peptar_dirs(p) 175 | 176 | unitar::peptar_path(p, "ref2") 177 | unitar::peptar_meta(p, fields="name") 178 | unitar::peptar_make(p) 179 | 180 | big_data_set2 = peptar_read(p, "big_data_set") 181 | head(big_data_set2) 182 | ``` 183 | 184 | Or, using `peptar_load`: 185 | 186 | ```{r} 187 | peptar_load(p, "big_data_set") 188 | head(big_data_set) 189 | ``` 190 | 191 | --------------------------------------------------------------------------------