Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toml read #12

Merged
merged 12 commits into from
Oct 26, 2024
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description: Tools for a simple, human-focused pipeline framework for
is designed to get developers up and running quickly with the code
they already have, and provide simple automation targets for use with
tools like 'cron' or 'Task Scheduler'.
License: GPL (>= 3)
License: MIT + file LICENSE
URL: https://github.com/asenetcky/strata
BugReports: https://github.com/asenetcky/strata/issues
Imports:
Expand All @@ -20,9 +20,9 @@ Imports:
lifecycle,
lubridate (>= 1.9.3),
purrr (>= 1.0.2),
RcppTOML (>= 0.2.2),
rlang (>= 1.1.4),
stringr (>= 1.5.1)
stringr (>= 1.5.1),
tibble (>= 3.2.1)
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Expand Down
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: strata authors
616 changes: 21 additions & 595 deletions LICENSE.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion R/builders.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ build_main <- function(project_path) {
if (!is_main) {
fs::file_create(main_path)
cat(
paste0("library(strata)\nstrata::main(", project_path, ")\n"),
paste0("library(strata)\nstrata::main('", project_path, "')\n"),
file = main_path,
append = TRUE
)
Expand Down
12 changes: 7 additions & 5 deletions R/source_wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ run_execution_plan <- function(execution_plan) {
initial_lamina <- execution_plan[1, ]$lamina_name

log_message("Strata started")
log_message(paste("Pipeline:", initial_stratum, "initialized"))
log_message(paste("Module:", initial_lamina, "initialized"))
log_message(paste("Stratum:", initial_stratum, "initialized"))
log_message(paste("Lamina:", initial_lamina, "initialized"))
for (row in seq_len(nrow(execution_plan))) {
row_scope <- execution_plan[row, ]
row_stratum <- row_scope$stratum
row_lamina <- row_scope$lamina_name


if (row_stratum != initial_stratum) {
log_message(paste("Pipeline:", row_stratum, "initialized"))
log_message(paste("Stratum:", initial_stratum, "finished"))
log_message(paste("Stratum:", row_stratum, "initialized"))
initial_stratum <- row_stratum
}

if (row_lamina != initial_lamina) {
log_message(paste("Module:", row_lamina, "initialized"))
log_message(paste("Lamina:", initial_lamina, "finished"))
log_message(paste("Lamina:", row_lamina, "initialized"))
initial_lamina <- row_lamina
}


log_message(paste("Executing:", row_scope$script_name))
source(row_scope$path)
}

Expand Down
70 changes: 69 additions & 1 deletion R/toml.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ initial_lamina_toml <- function(path) {

snapshot_toml <- function(toml_path) {
toml_path <- fs::path(toml_path)
toml <- RcppTOML::tomlparse(toml_path)
toml <- read_toml(toml_path)
toml_type <- names(toml)

vars <- c("type", "name", "order", "skip_if_fail", "created")
Expand Down Expand Up @@ -199,3 +199,71 @@ rewrite_from_dataframe <- function(toml_snapshot, toml_path) {
write_toml_lines(new_toml, toml_path)
invisible(toml_path)
}


read_toml <- function(toml_path) {
toml_path <- fs::path(toml_path)

toml_lines <- readLines(toml_path)
toml_type <-
toml_lines[1] |>
stringr::str_remove_all("\\[|\\]")


toml_length <- length(toml_lines)

toml_list <-
tibble::lst(
!!toml_type := tibble::lst()
)


if (toml_length > 1) {
created <- order <- skip_if_fail <- NULL
for (i in 2:toml_length) {
line <- toml_lines[i]

name <-
stringr::word(line)

vars <-
line |>
stringr::str_remove_all(
pattern = paste0(name, " = \\{|\\}")
) |>
stringr::str_trim() |>
stringr::str_split_1(", ") |>
purrr::map(
\(x) {
x |>
stringr::str_split_1(" = ") |>
purrr::set_names(c("key", "value"))
}
)


for (i in 1:length(vars)) {
assign(vars[[i]][["key"]], vars[[i]][["value"]],
# envir = toml[[toml_type]][[name]]
)
}

var_list <-
tibble::lst(
created = lubridate::as_date(created),
order = as.integer(order)
)


if(toml_type == "laminae") {
var_list <-
c(var_list, tibble::lst( skip_if_fail = as.logical(skip_if_fail)))
}

row_vars <- tibble::lst(!!name := var_list)
toml_list[[1]] <- c(toml_list[[1]], row_vars)
}
}

toml_list
}
58 changes: 43 additions & 15 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ knitr::opts_chunk$set(
<!-- badges: start -->
<!-- badges: end -->

The goal of strata is to ...
The goal of strata is to provide a framework for workflow automation
and reproducible analyses for R users and teams who may not have access
to many modern automation tooling and/or are otherwise
resource-constrained. Strata aims to be simple and allow users to
adopt it with minimal changes to existing code and use whatever automation
they have access to. There is only one target file users will need to automate, `main.R`, which will run through the entire project with the
settings they specified as they build their strata of code. Strata is
designed to get out of the users' way and play nice with packages like
`renv`, `cronR`, and `taskscheduleR`.

## Installation

Expand All @@ -35,21 +43,41 @@ This is a basic example which shows you how to solve a common problem:

```{r example}
library(strata)
## basic example code
```

What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so:

```{r cars}
summary(cars)
```

You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date. `devtools::build_readme()` is handy for this.

You can also embed plots, for example:
tmp <- fs::dir_create(fs::file_temp())
strata::build_stratum(
path = tmp,
stratum_name = "first_stratum",
order = 1
)

stratum_path <-
fs::path(
tmp, "strata", "first_stratum"
)
strata::build_lamina(
stratum_path = stratum_path,
lamina_name = "first_lamina",
order = 1
)
strata::build_lamina(
stratum_path = stratum_path,
lamina_name = "second_lamina",
order = 2
)

lamina_path1 <- fs::path(stratum_path, "first_lamina")
lamina_path2 <- fs::path(stratum_path, "second_lamina")
code_path1 <- fs::path(lamina_path1, "my_code1.R")
code_path2 <- fs::path(lamina_path2, "my_code2.R")


my_code1 <- fs::file_create(code_path1)
my_code2 <- fs::file_create(code_path2)
cat(file = my_code1, "print('Hello, World!')")
cat(file = my_code2, "print('Goodbye, World!')")

source(fs::path(tmp,"main.R"))

```{r pressure, echo = FALSE}
plot(pressure)
```

In that case, don't forget to commit and push the resulting figure files, so they display on GitHub and CRAN.
79 changes: 54 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
<!-- badges: start -->
<!-- badges: end -->

The goal of strata is to …
The goal of strata is to provide a framework for workflow automation and
reproducible analyses for R users and teams who may not have access to
many modern automation tooling and/or are otherwise
resource-constrained. Strata aims to be simple and allow users to adopt
it with minimal changes to existing code and use whatever automation
they have access to. There is only one target file users will need to
automate, `main.R`, which will run through the entire project with the
settings they specified as they build their strata of code. Strata is
designed to get out of the users’ way and play nice with packages like
`renv`, `cronR`, and `taskscheduleR`.

## Installation

Expand All @@ -24,29 +33,49 @@ This is a basic example which shows you how to solve a common problem:

``` r
library(strata)
## basic example code
```

What is special about using `README.Rmd` instead of just `README.md`?
You can include R chunks like so:

``` r
summary(cars)
#> speed dist
#> Min. : 4.0 Min. : 2.00
#> 1st Qu.:12.0 1st Qu.: 26.00
#> Median :15.0 Median : 36.00
#> Mean :15.4 Mean : 42.98
#> 3rd Qu.:19.0 3rd Qu.: 56.00
#> Max. :25.0 Max. :120.00
tmp <- fs::dir_create(fs::file_temp())
strata::build_stratum(
path = tmp,
stratum_name = "first_stratum",
order = 1
)

stratum_path <-
fs::path(
tmp, "strata", "first_stratum"
)
strata::build_lamina(
stratum_path = stratum_path,
lamina_name = "first_lamina",
order = 1
)
strata::build_lamina(
stratum_path = stratum_path,
lamina_name = "second_lamina",
order = 2
)

lamina_path1 <- fs::path(stratum_path, "first_lamina")
lamina_path2 <- fs::path(stratum_path, "second_lamina")
code_path1 <- fs::path(lamina_path1, "my_code1.R")
code_path2 <- fs::path(lamina_path2, "my_code2.R")


my_code1 <- fs::file_create(code_path1)
my_code2 <- fs::file_create(code_path2)
cat(file = my_code1, "print('Hello, World!')")
cat(file = my_code2, "print('Goodbye, World!')")

source(fs::path(tmp,"main.R"))
#> [2024-10-25 08:31:12.288163] INFO: Strata started
#> [2024-10-25 08:31:12.288272] INFO: Stratum: first_stratum initialized
#> [2024-10-25 08:31:12.288332] INFO: Lamina: first_lamina initialized
#> [2024-10-25 08:31:12.288433] INFO: Executing: my_code1
#> [1] "Hello, World!"
#> [2024-10-25 08:31:12.28873] INFO: Lamina: first_lamina finished
#> [2024-10-25 08:31:12.288796] INFO: Lamina: second_lamina initialized
#> [2024-10-25 08:31:12.288848] INFO: Executing: my_code2
#> [1] "Goodbye, World!"
#> [2024-10-25 08:31:12.289503] INFO: Strata finished - duration: 0.0012 seconds
```

You’ll still need to render `README.Rmd` regularly, to keep `README.md`
up-to-date. `devtools::build_readme()` is handy for this.

You can also embed plots, for example:

<img src="man/figures/README-pressure-1.png" width="100%" />

In that case, don’t forget to commit and push the resulting figure
files, so they display on GitHub and CRAN.