Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra authored Jun 9, 2024
0 parents commit 952f9e1
Show file tree
Hide file tree
Showing 18 changed files with 2,783 additions and 0 deletions.
1 change: 1 addition & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
47 changes: 47 additions & 0 deletions .github/workflows/test-targets-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: test targets workflow

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
workflow_dispatch:
# branches:
# - '*'

jobs:
test-targets-workflow:
runs-on: ubuntu-latest
container: rocker/tidyverse:4.3.3
env:
GIT_CRYPT_KEY64: ${{ secrets.GIT_CRYPT_KEY64 }}
steps:
- uses: actions/checkout@v4

- name: Install system dependencies
run: |
apt-get update && apt-get install -y --no-install-recommends \
libxt6 libglpk-dev
- name: Trust git repo
run: |
git config --global --add safe.directory /__w/ihtm-targets-template/ihtm-targets-template
- name: Install packages from renv.lock (with cache)
if: ${{ !env.ACT }}
uses: r-lib/actions/setup-renv@v2
with:
cache-version: 2

- name: Install packages from renv.lock (local, no cache)
if: ${{ env.ACT }}
run: |
renv::restore()
shell: Rscript {0}

- name: Run workflow
run: |
targets::tar_make()
shell: Rscript {0}


6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
_targets
docs
Empty file added R/.gitkeep
Empty file.
34 changes: 34 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#'
#' Collect all targets and lists of targets in the environment
#'
#'
all_targets <- function(env = parent.env(environment()),
type = "tar_target",
add_list_names = TRUE) {

## Function to determine if an object is a type (a target),
## or a list on only that type
rfn <- function(obj)
inherits(obj, type) || (is.list(obj) && all(vapply(obj, rfn, logical(1))))

## Get the names of everything in the environment
## (e.g. sourced in the _targets.R file)
objs <- ls(env)

out <- list()
for (o in objs) {
obj <- get(o, envir = env) ## Get each top-level object in turn
if (rfn(obj)) { ## For targets and lists of targets
out[[length(out) + 1]] <- obj ## Add them to the output

## If the object is a list of targets, add a vector of the target names
## to the environment So that one can call `tar_make(list_name)` to make
## all the targets in that list
if (add_list_names && is.list(obj)) {
target_names <- vapply(obj, \(x) x$settings$name, character(1))
assign(o, target_names, envir = env)
}
}
}
return(out)
}
71 changes: 71 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

# A template R workflow for general data analysis

<!-- badges: start -->
<!-- badges: end -->

This repository is a template for a [`docker`](https://www.docker.com/get-started)-containerised, [`{targets}`](https://docs.ropensci.org/targets/)-based, [`{renv}`](https://rstudio.github.io/renv/articles/renv.html)-enabled [`R`](https://cran.r-project.org/) workflow for general data analysis.

## About the Project



## Repository Structure

The project repository is structured as follows:

```
sc-policy-review
|-- .github/
|-- data/
|-- data-raw/
|-- outputs/
|-- R/
|-- reports
|-- renv
|-- renv.lock
|-- .Rprofile
|-- packages.R
|-- _targets.R
```

* `.github` contains project testing and automated deployment of outputs workflows via continuous integration and continuous deployment (CI/CD) using Github Actions.

* `data/` contains intermediate and final data outputs produced by the workflow.

* `data-raw/` contains raw datasets, usually either downloaded from source or added manually, that are used in the project. This directory is empty given that the raw datasets used in this project are restricted and are only distributed to eligible members of the project. This directory is kept here to maintain reproducibility of project directory structure and ensure that the workflow runs as expected. Those who are collaborating on this project and who have permissions to use the raw datasets should include their copies of the raw dataset into this directory in their local versions of this repository.

* `outputs/` contains compiled reports and figures produced by the workflow.

* `R/` contains functions developed/created specifically for use in this workflow.

* `reports/` contains literate code for R Markdown reports rendered in the workflow.

* `renv/` contains `renv` package specific files and directories used by the package for maintaining R package dependencies within the project. The directory `renv/library`, is a library that contains all packages currently used by the project. This directory, and all files and sub-directories within it, are all generated and managed by the `renv` package. Users should not change/edit these manually.

* `renv.lock` file is the `renv` lockfile which records enough metadata about every package used in this project that it can be re-installed on a new machine. This file is generated by the `renv` package and should not be changed/edited manually.

* `.Rprofile` file is a project R profile generated when initiating `renv` for the first time. This file is run automatically every time R is run within this project, and `renv` uses it to configure the R session to use the `renv` project library.

* `packages.R` file lists out all R package dependencies required by the workflow.

* `_targets.R` file defines the steps in the workflow's data ingest, data processing, data analysis, and reporting pipeline.

## Reproducibility

### R package dependencies

This project was built using `R 4.4.0`. This project uses the `renv` framework to record R package dependencies and versions. Packages and versions used are recorded in `renv.lock` and code used to manage dependencies is in `renv/` and other files in the root project directory. On starting an R session in the working directory, run `renv::restore()` to install R package dependencies.

94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# A template R workflow for general data analysis

<!-- badges: start -->

<!-- badges: end -->

This repository is a template for a
[`docker`](https://www.docker.com/get-started)-containerised,
[`{targets}`](https://docs.ropensci.org/targets/)-based,
[`{renv}`](https://rstudio.github.io/renv/articles/renv.html)-enabled
[`R`](https://cran.r-project.org/) workflow for general data analysis.

## About the Project

## Repository Structure

The project repository is structured as follows:

sc-policy-review
|-- .github/
|-- data/
|-- data-raw/
|-- outputs/
|-- R/
|-- reports
|-- renv
|-- renv.lock
|-- .Rprofile
|-- packages.R
|-- _targets.R

- `.github` contains project testing and automated deployment of
outputs workflows via continuous integration and continuous
deployment (CI/CD) using Github Actions.

- `data/` contains intermediate and final data outputs produced by the
workflow.

- `data-raw/` contains raw datasets, usually either downloaded from
source or added manually, that are used in the project. This
directory is empty given that the raw datasets used in this project
are restricted and are only distributed to eligible members of the
project. This directory is kept here to maintain reproducibility of
project directory structure and ensure that the workflow runs as
expected. Those who are collaborating on this project and who have
permissions to use the raw datasets should include their copies of
the raw dataset into this directory in their local versions of this
repository.

- `outputs/` contains compiled reports and figures produced by the
workflow.

- `R/` contains functions developed/created specifically for use in
this workflow.

- `reports/` contains literate code for R Markdown reports rendered in
the workflow.

- `renv/` contains `renv` package specific files and directories used
by the package for maintaining R package dependencies within the
project. The directory `renv/library`, is a library that contains
all packages currently used by the project. This directory, and all
files and sub-directories within it, are all generated and managed
by the `renv` package. Users should not change/edit these manually.

- `renv.lock` file is the `renv` lockfile which records enough
metadata about every package used in this project that it can be
re-installed on a new machine. This file is generated by the `renv`
package and should not be changed/edited manually.

- `.Rprofile` file is a project R profile generated when initiating
`renv` for the first time. This file is run automatically every time
R is run within this project, and `renv` uses it to configure the R
session to use the `renv` project library.

- `packages.R` file lists out all R package dependencies required by
the workflow.

- `_targets.R` file defines the steps in the workflow’s data ingest,
data processing, data analysis, and reporting pipeline.

## Reproducibility

### R package dependencies

This project was built using `R 4.4.0`. This project uses the `renv`
framework to record R package dependencies and versions. Packages and
versions used are recorded in `renv.lock` and code used to manage
dependencies is in `renv/` and other files in the root project
directory. On starting an R session in the working directory, run
`renv::restore()` to install R package dependencies.
50 changes: 50 additions & 0 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
################################################################################
#
# General Targets Workflow
#
################################################################################

## Load libraries and custom functions -----------------------------------------
suppressPackageStartupMessages(source("packages.R"))
for (f in list.files(here::here("R"), full.names = TRUE)) source (f)

## Create targets and list targets objects -------------------------------------

### Data targets
data_targets <- tar_plan(

)


### Processing targets
processing_targets <- tar_plan(

)


### Analysis targets
analysis_targets <- tar_plan(

)


### Output targets
output_targets <- tar_plan(

)


### Reporting targets
report_targets <- tar_plan(

)


### Deploy targets
deploy_targets <- tar_plan(

)


## List targets
all_targets()
Empty file added data/.gitkeep
Empty file.
13 changes: 13 additions & 0 deletions ihtm-targets-template.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
Empty file added outputs/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions packages.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
################################################################################
#
# Load packages required for standard workflows
#
################################################################################

library(targets)
library(tarchetypes)
library(here)
library(rmarkdown)
library(knitr)
library(kableExtra)
library(dplyr)
library(tidyr)
library(openxlsx)
library(ggplot2)
Loading

0 comments on commit 952f9e1

Please sign in to comment.