Skip to content

Commit 6b7fff0

Browse files
authored
Merge branch 'main' into auto-merge-options
2 parents 6bdc4dc + 4e48fd1 commit 6b7fff0

17 files changed

+326
-3698
lines changed

.Rprofile

-1
This file was deleted.

.github/workflows/ci.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ jobs:
1919
with:
2020
r-version: 'release'
2121
use-public-rspm: true
22-
# - uses: r-lib/actions/setup-renv@v2
2322
- uses: pre-commit/[email protected]

.lintr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
linters: linters_with_defaults(
22
cyclocomp_linter(complexity_limit = 10L),
3-
indentation_linter(indent = 4L),
3+
indentation_linter(indent = 2L),
44
line_length_linter(length = 88L)
55
)

.pre-commit-config.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ repos:
66
rev: v5.0.0
77
hooks:
88
- id: trailing-whitespace
9-
exclude: renv/activate.R
109
- id: end-of-file-fixer
1110
- id: check-yaml
1211
- id: check-added-large-files
@@ -32,7 +31,7 @@ repos:
3231
rev: dcd2a60
3332
hooks:
3433
- id: style-files
35-
args: [--style_pkg=styler, --indent_by=4, --style_fun=tidyverse_style]
34+
args: [--style_pkg=styler, --style_fun=tidyverse_style]
3635
- id: lintr
3736
- id: parsable-R
3837
- id: no-browser-statement

.vscode/settings.json

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
"r.alwaysUseActiveTerminal": true,
33
"r.bracketedPaste": true,
44
"r.rpath.mac": "/opt/homebrew/bin/R",
5-
"r.useRenvLibPath": true,
65
"r.workspaceViewer.clearPrompt": false,
76
"r.rterm.mac": "radian",
8-
"r.libPaths": [
9-
"/Users/dorme/Research/Virtual_Rainforest/ve_data_science/renv/library/macos/R-4.4/aarch64-apple-darwin23.6.0 "
10-
]
117
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ As a brief overview, this involves setting up the following tools:
2121
tools.
2222
* **Poetry**. This is a Python package manager that we use to manage a shared set of
2323
Python packages used across the project.
24-
* **R**. We will be using R extensively for analysis and data visualisation.
25-
* **renv**. This is an R package manager that, like `poetry`, is being used to manage
26-
the shared set of R packages used across the project.
24+
* **R**. We will be using R extensively for analysis and data visualisation. At the
25+
moment, we are managing package use and versioning with a simple list of packages.
26+
This is currently very light touch and we may use something stricter in the future.
2727
* **pre-commit**. This is a QA tool - we have a set of configured checks that run
2828
whenever you try and `git commit` some changes to the repo. If the changes fail any of
2929
the checks, then you will have to fix them and try again. Sometimes, the QA checks can

maintenance/metadata_check.R

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#' ---
2+
#' title: Script metadata checker
3+
#'
4+
#' description: |
5+
#' This file provides the function `check_metadata` that can be used to check that
6+
#' a given file contains the expected metadata and that it can read correctly.
7+
#'
8+
#' We aren't sure if we are going to support R markdown rather than Jupyter
9+
#' notebooks but we'll start with providing some support.
10+
#'
11+
#' VE_module: None
12+
#'
13+
#' author:
14+
#' - name: David Orme
15+
#'
16+
#' status: wip
17+
#'
18+
#' package_dependencies:
19+
#' - rmarkdown
20+
#' - yaml
21+
#' - tools
22+
#'
23+
#' usage_notes: |
24+
#' From within R use:
25+
#'
26+
#' source('templates/metadata_check.R')
27+
#' yaml <- check_metadata('path/to/file.R')
28+
#' ---
29+
30+
library(rmarkdown)
31+
library(yaml)
32+
library(tools)
33+
34+
check_metadata <- function(file_path) {
35+
#' Check VE data science script and notebook metadata
36+
#'
37+
#' Checks that a script file or notebook contains expected metadata. It raises an
38+
#' error if the file is missing metadata, if the metadata is badly formatted or if
39+
#' it contains unexpected values.
40+
#'
41+
#' @param file_path The path to the file to be checked
42+
#'
43+
#' @return The function returns the YAML metadata if it can be read cleanly.
44+
#'
45+
#'
46+
47+
if (!file.exists(file_path)) {
48+
simpleError("File path not found.")
49+
}
50+
51+
# Extract the file extension
52+
file_type <- tolower(file_ext(file_path))
53+
54+
# For R Markdown files, can just use the standard YAML parser
55+
if (file_type == "rmd") {
56+
yaml <- rmarkdown::yaml_front_matter(file_path)
57+
} else if (file_type == "r") {
58+
# Load the script as text
59+
content <- readLines("templates/R_script_template.r")
60+
61+
# Locate the YAML document blocks
62+
document_markers <- grep("#' ---", content)
63+
n_doc_markers <- length(document_markers)
64+
65+
# Check there are two...
66+
if (n_doc_markers != 2) {
67+
simpleError(sprintf("Found %i not 2 YAML metadata markers.", n_doc_markers))
68+
}
69+
70+
# And that the first one is at the start of the file
71+
if (document_markers[1] != 1) {
72+
simpleError(
73+
sprintf("First YAML metadata markers is not at the file start.")
74+
)
75+
}
76+
77+
# Extract the block and strip the comments
78+
yaml_block <- content[document_markers[1]:document_markers[2]]
79+
yaml_block <- sub("^#' ?", "", yaml_block)
80+
81+
yaml <- try(yaml.load(yaml_block), silent = TRUE)
82+
if (inherits(yaml, "try-error")) {
83+
simpleError(
84+
paste0(
85+
"Could not load YAML metadata. The YAML loader repoorts:\n",
86+
yaml[1]
87+
)
88+
)
89+
}
90+
}
91+
92+
# TODO - this could be extended to validate the contents.
93+
94+
return(yaml)
95+
}

r_requirements.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is used to record R packages used by the project.
2+
#
3+
# We did look at using `renv` to manage this automatically but it added a layer of
4+
# complexity and unexpected issues and workflow problems and so we have dropped it for
5+
# now.
6+
7+
# This is required for R support in VSCode
8+
library(languageserver)
9+
10+
# This is required to use R with Jupyter
11+
library(IRkernel)

0 commit comments

Comments
 (0)