Skip to content

Commit

Permalink
Merge pull request #89 from dfe-analytical-services/renv-update
Browse files Browse the repository at this point in the history
Renv update
  • Loading branch information
rmbielby authored May 20, 2024
2 parents 8899f98 + cd0d3db commit 8b01eaf
Show file tree
Hide file tree
Showing 73 changed files with 11,094 additions and 15,584 deletions.
32 changes: 23 additions & 9 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
source("renv/activate.R")
# ---------------------------------------------------------
# This is the .Rprofile file
#
# Use it to include any functions you want to run before any other code is run.
# For example, using renv automatically sources its activate script to the .RProfile file
# This ensures that all renv checks on package versions happens before any code is run.
#
#
# ---------------------------------------------------------

shhh <- suppressPackageStartupMessages # It's a library, so shhh!
cat("Sourcing .Rprofile.", fill = TRUE)

source("renv/activate.R")

tidy_code <- function() {
shhh(source("global.r"))
shhh(tidy_code_function())
if (system.file(package = "dfeshiny") != "") {
library(dfeshiny)
} else {
warning("dfeshiny package is not installed, please run renv::restore() to set up the necessary package environment")
}

# Function to run tests
run_tests_locally <- function() {
Sys.unsetenv("http_proxy")
Sys.unsetenv("https_proxy")
shhh(source("global.R"))
source("global.r")
# message("================================================================================")
# message("== testthat ====================================================================")
# message("")
# shhh(testthat::test_dir("tests/testthat"))
# testthat::test_dir("tests/testthat")
# message("")
message("================================================================================")
message("== shinytest ===================================================================")
message("== shinytest2 ==================================================================")
message("")
shhh(shinytest::testApp())
shinytest2::test_app()
message("")
message("================================================================================")
}

# Install commit-hooks locally
statusWriteCommit <- file.copy(".hooks/pre-commit.R", ".git/hooks/pre-commit", overwrite = TRUE)
18 changes: 1 addition & 17 deletions .github/workflows/shiny-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
automatedTests:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})
name: shiny-tests

strategy:
fail-fast: false
Expand All @@ -29,7 +29,6 @@ jobs:

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{matrix.config.r}}
use-public-rspm: true

- name: Cache R packages
Expand All @@ -53,21 +52,6 @@ jobs:
run: |
if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv")
renv::restore()
- name: Find PhantomJS path
id: phantomjs
run: |
echo "::set-output name=path::$(Rscript -e 'cat(shinytest:::phantom_paths()[[1]])')"
- name: Cache PhantomJS
uses: actions/cache@v1
with:
path: ${{ steps.phantomjs.outputs.path }}
key: ${{ runner.os }}-phantomjs
restore-keys: ${{ runner.os }}-phantomjs
- name: Install PhantomJS
shell: Rscript {0}
run: |
options(install.packages.check.source = "no")
if (!shinytest::dependenciesInstalled()) shinytest::installDependencies()
- name: Run tests
shell: Rscript {0}
run: |
Expand Down
82 changes: 82 additions & 0 deletions .hooks/pre-commit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env Rscript
cat("Running commit hooks...",fill=TRUE)
shhh <- suppressPackageStartupMessages # It's a library, so shhh!
shhh(library(dplyr))
shhh(library(xfun))
shhh(library(dfeshiny))

error_flag <- FALSE

datalog <- "datafiles_log.csv"
log_files <- read.csv(datalog, stringsAsFactors = FALSE)
ign_files <- read.csv(".gitignore", header = FALSE, stringsAsFactors = FALSE)
colnames(ign_files)[1] <- "filename"

cat("Contents of the .gitignore file:")
print(ign_files)

# Run a pass through the .gitignore files and look for any issues
if(ncol(ign_files)>1){
cat("ERROR: It looks like you've got commas in the .gitignore. Please correct the .gitignore file and try again.")
error_flag <- TRUE
} else {
for(i in 1:nrow(ign_files)){
if(grepl(' ',ign_files$filename[i])){
cat("ERROR: It looks like you've got spaces in filenames in the .gitignore. Please rename your files if they contain spaces and update the .gitignore file accordingly.")
error_flag <- TRUE
}
}
}

suffixes <- "xlsx$|dat$|csv$|tex$|pdf$"

current_files <- data.frame(files = list.files("./", recursive = TRUE)) %>%
filter(grepl(suffixes, files), !grepl("renv|datafiles_log.csv", files))

for (file in current_files$files) {
if (!file %in% log_files$filename) {
cat("Error:", file, "is not recorded in datafiles_log.csv.\n\n")
cat("Please add an entry to datafiles_log.csv for this file and mark it as unpublished, published or reference.\n\n")
error_flag <- TRUE
} else {
file_status <- (log_files %>% filter(filename == file))$status
if (!file_status %in% c("published", "Published", "reference", "Reference", "dummy", "Dummy")) {
if (!file %in% ign_files$filename & !grepl("unpublished",file)) {
cat("Error:", file, "is not logged as published or reference data in datafiles_log.csv and is not found in .gitignore.\n\n")
cat("If the file contains published or reference data then update its entry in datafiles_log.csv.\n\n")
cat("If the file contains unpublished data then add it to the .gitignore file.\n\n")
error_flag <- TRUE
}
else {
cat(file,"is recorded in the logfile as unpublished data and in .gitignore and so will not be included as part of the commit.\n\n")
}
}
}
}

if(grepl('G-Z967JJVQQX', htmltools::includeHTML(("google-analytics.html"))) &
!(toupper(Sys.getenv("USERNAME")) %in% c("CFOSTER4", "CRACE", "LSELBY","RBIELBY", "JMACHIN"))){
cat("Cleaning out the template's Google Analytics tag.",fill=TRUE)
gsub_file("google-analytics.html", pattern = "G-Z967JJVQQX", replacement = "G-XXXXXXXXXX")
gsub_file("ui.R", pattern = "Z967JJVQQX", replacement = "XXXXXXXXXX")
system2(command = "git", args=c("add","google-analytics.html"))
}

if (error_flag) {
cat("Warning, aborting commit. Unrecognised data files found, please update .gitignore or datafiles_log.csv.\n")
quit(save = "no", status = 1, runLast = FALSE)
}

tidy_output <- tidy_code()
if(any(tidy_output)){
error_flag <- TRUE
}

if (error_flag) {
cat("Warning: Code did not appear to have been tidied.\nI've run tidy code for you,
please check your files and the dashboard still works and then re-stage and try committing again.")
quit(save = "no", status = 1, runLast = FALSE)
}


# End of hooks
12 changes: 12 additions & 0 deletions datafiles_log.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
filename,status
data/regional_data_FD_PG.csv,published
data/regional_movement_FD_PG.csv,published
data/sankey data 1-3YAG_FD_PG.csv,published
data/sankey data 1-5YAG_FD_PG.csv,published
data/sankey data 3-5YAG_FD_PG.csv,published
data/tables_data_3digit_FD_PG.csv,published
data/boundaries/Regions__December_2019__Boundaries_EN_BFE.cpg,published
data/boundaries/Regions__December_2019__Boundaries_EN_BFE.dbf,published
data/boundaries/Regions__December_2019__Boundaries_EN_BFE.prj,published
data/boundaries/Regions__December_2019__Boundaries_EN_BFE.shp,published
data/boundaries/Regions__December_2019__Boundaries_EN_BFE.shx,published
1 change: 1 addition & 0 deletions global.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ shhh(library(networkD3))
shhh(library(styler))
shhh(library(shinyGovstyle))
shhh(library(shinyjs))
shhh(library(dfeshiny))
# shhh(library(shinya11y))

# update years for text
Expand Down
Loading

0 comments on commit 8b01eaf

Please sign in to comment.