Skip to content

Commit 7335abb

Browse files
committed
wip
1 parent d86f107 commit 7335abb

10 files changed

Lines changed: 8662 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
```{r}
3+
getwd()
4+
setwd("~/code/try_things_here/ADVANCED_R/BOOK")
5+
6+
# local = F (default) env is globalenv
7+
# local = T, then environment is where called from (not created)
8+
source
9+
formals(source)
10+
args(source)
11+
?source
12+
```
13+
14+
### source a file
15+
16+
```{r}
17+
?source
18+
# source_file contains x + x
19+
x=0
20+
21+
f = function() {
22+
x=10
23+
source("source_file", local=F) # use global
24+
source("source_file", local=T, echo=T) # use env where called from
25+
26+
}
27+
res = f()
28+
res
29+
```
30+
31+
```{r}
32+
expr(2 + 2) # 2 + 2
33+
eval(expr(2 + 2)) # 4
34+
expr(eval(expr(2 + 2)))
35+
eval(expr(eval(expr(2 + 2)))) # eval ALL
36+
37+
eval(eval(expr(eval(expr(2 + 2)))))
38+
expr(eval(eval(expr(eval(expr(2 + 2)))))) # one long expression
39+
40+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Chapter 20 - evaluation
2+
3+
```{r}
4+
# eval( {expr}, {env})
5+
6+
x = 10
7+
eval(x) # 10
8+
expr(x) # does not evalu
9+
eval(expr(x)) # now, we get 10
10+
11+
12+
eval(expr(x), env(x=1)) # now 1, b/c that is env
13+
```
14+
15+
```{r}
16+
## Build local2()
17+
18+
# Clean up variables created earlier
19+
rm(x, y)
20+
21+
# x, y exist only inside
22+
foo <- local({
23+
x <- 10
24+
y <- 200
25+
x + y
26+
})
27+
28+
foo
29+
30+
#> [1] 210
31+
# our rough equivalent
32+
local2 <- function(expr) {
33+
env <- env(caller_env())
34+
eval(enexpr(expr), env)
35+
}
36+
local2(2 + 2)
37+
38+
x=10
39+
y=200
40+
local2({x+y}) # 210
41+
42+
local2({x=1 # 201
43+
x+y})
44+
```

ADVANCED_R/BOOK/source_file

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x + x

BASE/101_invisible.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ f2 <- function(x) invisible(x)
88

99
f1("This prints")
1010

11+
1112
f2("This does not print")
1213
(f2("This does not print"))
1314
z <- f2("This does not print")
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
library(syntheticadam)
2+
library(digest)
3+
4+
# copy config_workflow file in the root folder
5+
file.copy("inst/config_workflow.yml", "./")
6+
7+
ignore_checksum <- c("ad_adlb_param_tab.R") # templates where we do not compare checksums
8+
9+
# read folder from config_workflow.yml file and create folders
10+
current_script_directory <- getwd()
11+
12+
config <- yaml::read_yaml("./config_workflow.yml")
13+
esub_path <- config[["paths"]][["adam"]][["esub"]]
14+
15+
adam_path <- config[["paths"]][["adam"]][["data"]]
16+
adam_path_parquet_expected <- gsub("adams", "adams_expected", config[["paths"]][["adam"]][["data"]])
17+
adam_path_parquet_actual <- config[["paths"]][["adam"]][["data"]]
18+
templates_dir <- file.path("inst", "templates")
19+
20+
dir.create(esub_path, showWarnings = FALSE, recursive = TRUE)
21+
dir.create(adam_path, showWarnings = FALSE, recursive = TRUE)
22+
dir.create(adam_path_parquet_actual, showWarnings = FALSE, recursive = TRUE)
23+
dir.create(adam_path_parquet_expected, showWarnings = FALSE, recursive = TRUE)
24+
25+
###################################################
26+
# 1. Retrieve syntheticadam data (expected data)
27+
###################################################
28+
29+
# read all data from syntheticadam package and store them as parquet files on expected data dir
30+
datasets <- data(package = "syntheticadam")
31+
dataset_names <- datasets$results[, "Item"]
32+
for (dataset_name in dataset_names) {
33+
data_obj <- get(dataset_name, pos = "package:syntheticadam")
34+
parquet_file <- file.path(adam_path_parquet_expected, paste0(dataset_name, ".parquet"))
35+
arrow::write_parquet(data_obj, parquet_file)
36+
}
37+
38+
########################################################
39+
# 2. Run snakemake workflow, and retrieve failed rules
40+
########################################################
41+
42+
# clean previous snakemake logs
43+
logs_dir <- "inst/templates/logs/"
44+
unlink(".snakemake", recursive = TRUE)
45+
dir.create(logs_dir, recursive = TRUE)
46+
47+
output <- system2(c("snakemake", "-F", "-j8", "--snakefile", "./inst/Snakefile", "all"), stdout = TRUE, stderr = TRUE)
48+
cat(paste(output, collapse = "\n"))
49+
50+
# Loop over logs files in logs dir - check erors cases
51+
log_files <- list.files(logs_dir, pattern = ".*\\.log$", full.names = TRUE)
52+
53+
# Find the Snakemake log file in .snakemake/log directory
54+
failed_logs_paths <- c()
55+
warnings_logs_paths <- c()
56+
for (log_file in log_files) {
57+
# retrieve failed rules
58+
snakemake_log <- readLines(log_file)
59+
snakemake_log <- paste(snakemake_log, collapse = "\n")
60+
if (grepl("Execution halted", snakemake_log)) {
61+
failed_logs_paths <- c(failed_logs_paths, log_file)
62+
}
63+
if (grepl("Warning message:", snakemake_log)) {
64+
warnings_logs_paths <- c(warnings_logs_paths, log_file)
65+
}
66+
}
67+
68+
69+
if (length(failed_logs_paths) > 0) {
70+
# exit code 1 will end up with error in the gitlab-ci pipelines
71+
print("Some templates failed")
72+
for (log_path in failed_logs_paths) {
73+
cat(sprintf("\nTemplate %s failed\n", gsub(".log", "", basename(log_path))))
74+
failed_rule_log <- readLines(log_path)
75+
cat(paste(failed_rule_log, collapse = "\n"))
76+
}
77+
q(status = 1)
78+
}
79+
80+
# check also other potential errors in the snakemake cmd
81+
if (!is.null(attr(output, "status"))) {
82+
print("Snakemake cmd failed")
83+
cat(paste(output, collapse = "\n"))
84+
q(status = 1)
85+
}
86+
87+
#############################################################
88+
# 3. Compare syntheticadam data with actual templates data
89+
#############################################################
90+
91+
# list produced datasets and compare them to syntheticadam data
92+
actual_paths <- list.files(adam_path_parquet_actual, pattern = "*.parquet")
93+
diff_templates <- c()
94+
for (parquet_path in actual_paths) {
95+
data_actual <- arrow::read_parquet(file.path(adam_path_parquet_actual, parquet_path))
96+
data_expected <- arrow::read_parquet(file.path(adam_path_parquet_expected, parquet_path))
97+
output_diff <- diffdf::diffdf(compare = data_actual, base = data_expected)
98+
if (diffdf::diffdf_has_issues(output_diff)) {
99+
# display differences in case differences found by diffdf()
100+
diff_templates <- c(diff_templates, list(list(parquet_path = parquet_path, diff = output_diff)))
101+
}
102+
}
103+
104+
105+
# exit code 125 will end up with warnings in the gitlab-ci pipelines
106+
if (length(diff_templates) > 0) {
107+
print("Some templates have differences with syntheticadam package :")
108+
for (d in diff_templates) {
109+
print(sprintf("Differences detected for data %s", d$parquet_path))
110+
print(d$diff)
111+
}
112+
}
113+
114+
if (length(warnings_logs_paths) > 0) {
115+
# exit code 1 will end up with error in the gitlab-ci pipelines
116+
print("Some templates ends-up with warnings")
117+
for (log_path in warnings_logs_paths) {
118+
cat(sprintf("\nWarning detected on Template log %s\n", gsub(".log", "", basename(log_path))))
119+
# warn_rule_log <- readLines(log_path)
120+
# cat(paste(warn_rule_log, collapse='\n'))
121+
}
122+
}

jsonvalidate/110_setup.qmd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
### jsonvalidate::
3+
4+
```
5+
# the JSON data file to check is "910_schema_file.qmd"
6+
# the schema for this file is .. "900_schema.qmd".
7+
```
8+
9+
```{r}
10+
library(jsonvalidate)
11+
12+
# read schema
13+
obj = jsonvalidate::json_schema$new("900_schema.qmd")
14+
obj
15+
16+
is.function(obj$validate) #T
17+
```
18+
### validate
19+
```{r}
20+
obj$validate("910_json_file.qmd") #T
21+
22+
23+
# validate, or test, against schema
24+
obj$validate("{}") #F
25+
obj$validate("{}", verbose=T) #F
26+
27+
{
28+
"id": 1,
29+
"name": "A green door",
30+
"price": 12.50,
31+
"tags": ["home", "green"]
32+
}
33+
```

jsonvalidate/900_schema.qmd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "Product",
4+
"description": "A product from Acme's catalog",
5+
"type": "object",
6+
"properties": {
7+
"id": {
8+
"description": "The unique identifier for a product",
9+
"type": "integer"
10+
},
11+
"name": {
12+
"description": "Name of the product",
13+
"type": "string"
14+
},
15+
"price": {
16+
"type": "number",
17+
"minimum": 0,
18+
"exclusiveMinimum": true
19+
},
20+
"tags": {
21+
"type": "array",
22+
"items": {
23+
"type": "string"
24+
},
25+
"minItems": 1,
26+
"uniqueItems": true
27+
}
28+
},
29+
"required": ["id", "name", "price"]
30+
}

jsonvalidate/910_json_file.qmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": 1,
3+
"name": "A green door",
4+
"price": 12.50,
5+
"tags": ["home", "green"]
6+
}

0 commit comments

Comments
 (0)