Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
CorradoLanera committed Apr 26, 2024
2 parents df8c351 + 42b25b5 commit ffdc05e
Show file tree
Hide file tree
Showing 9 changed files with 852 additions and 629 deletions.
6 changes: 6 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ if (interactive()) {
store = file.path(.get_prj_shared_path(), "_targets"),
config = "reports/_targets.yaml"
)

.run <- function() {
source(here::here("dev/run.R")) # check and make pipeline.
}
ui_info("Exexute {ui_code('.run()')} to make the pipeline.")

}
10 changes: 9 additions & 1 deletion 01-FIRST_RUN.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
## run the following code (after having renamed your project's folder
## at your convenience)
{
if (!requireNamespace("renv", quietly = TRUE)) {
stop(
"Before to run this all, please `install.packages('renv')`."
)
}
renv::restore(prompt = FALSE)
rstudioapi::restartSession()
prj_name <- basename(here::here())
fs::file_move(
here::here("laims.analysis.Rproj"), # old project's name
Expand Down Expand Up @@ -45,10 +52,11 @@ usethis::edit_r_environ("project")

## [Optional] Finaly, update all your packages in the project's library.
renv::upgrade()
rstudioapi::restartSession()
renv::update()
renv::status()
renv::snapshot()

rstudioapi::restartSession()

## At your convenience, replace your readme file
usethis::use_readme_rmd()
Expand Down
32 changes: 25 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Here below put your small tiny supporting functions -------------

`%||%` <- function(x, y) if (is.null(x)) y else x

view_in_excel <- function(.data) {
if (interactive()) {
Expand All @@ -22,7 +22,7 @@ extract_fct_names <- function(path) {



get_input_data_path <- function(x) {
get_input_data_path <- function(x = "") {
file.path(
Sys.getenv("PRJ_SHARED_PATH"),
Sys.getenv("INPUT_DATA_FOLDER"),
Expand All @@ -31,7 +31,7 @@ get_input_data_path <- function(x) {
normalizePath()
}

get_output_data_path <- function(x) {
get_output_data_path <- function(x = "") {
file.path(
Sys.getenv("PRJ_SHARED_PATH"),
Sys.getenv("OUTPUT_DATA_FOLDER"),
Expand All @@ -42,13 +42,31 @@ get_output_data_path <- function(x) {


share_objects <- function(obj_list) {
file_name <- paste0(names(obj_list), ".rds")
now <- lubridate::now() |>
stringr::str_remove_all("\\W+") |>
stringr::str_sub(1, 12)

file_name_now <- stringr::str_c(
names(obj_list), "-", now, ".rds"
)

obj_paths <- file.path(get_output_data_path(file_name)) |>
file_name_latest <- stringr::str_c(
names(obj_list), "-", "latest", ".rds"
)

obj_paths_now <- get_output_data_path(file_name_now) |>
normalizePath(mustWork = FALSE) |>
purrr::set_names(names(obj_list))

obj_paths_latest <- get_output_data_path(file_name_latest) |>
normalizePath(mustWork = FALSE) |>
purrr::set_names(names(obj_list))

# Those must be RDS
purrr::walk2(obj_list, obj_paths, readr::write_rds)
obj_paths
obj_list |>
purrr::walk2(obj_paths_now, readr::write_rds)
obj_list |>
purrr::walk2(obj_paths_latest, readr::write_rds)

obj_paths_latest
}
43 changes: 25 additions & 18 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
library(targets)
library(tarchetypes)
# This is an example _targets.R file. Every
# {targets} pipeline needs one.
# Use tar_script() to create _targets.R and tar_edit()
# to open it again for editing.
# Then, run tar_make() to run the pipeline
# and tar_read(result) to view the results.
library(crew) # parallel computing
controller <- crew::crew_controller_local(
name = "anvur_controller",
workers = 1
)

# Set target-specific options such as packages.
tar_option_set(
# error handling
error = "continue", # "null" create target with non-errored branches
workspace_on_error = TRUE,
# fast data formats
format = "qs",
# parallel computing
storage = "worker",
retrieval = "worker",
controller = controller
)

# Define custom functions and other global objects.
# This is where you write source(\"R/functions.R\")
# if you keep your functions in external scripts.
list.files(here::here("R"), pattern = "\\.R$", full.names = TRUE) |>
lapply(source) |> invisible()

# Set target-specific options such as packages.
tar_option_set(
error = "continue",
workspace_on_error = TRUE
)

# End this file with a list of target objects.
list(

# Import your file from custom (shared) location, and preprocess them
tar_target(
tar_files_input(
db_raw_path,
file.path(get_input_data_path("db_raw.csv")),
format = "file"
# get_input_data_path("db_raw.csv"), # single
get_input_data_path() |>
list.files(pattern = "\\.csv$", full.names = TRUE) # multiple
),

# Use {qs} in {targets} to save space and time in save/load objects
tar_target(db_raw, import_data(db_raw_path), format = "qs"),
tar_target(db, preprocess(db_raw), format = "qs"),
tar_target(db_raw, import_data(db_raw_path)),
tar_target(db, preprocess(db_raw)),


# Call your custom functions as needed.
tar_target(relevantResult, relevant_computation(db), format = "qs"),
tar_target(relevantResult, relevant_computation(db)),

# compile yor report
tar_render(report, here::here("reports/report.Rmd")),
Expand Down
8 changes: 4 additions & 4 deletions dev/00-template_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ stop("00-setup.R is not intended to be re-run/sourced.", call. = FALSE)
# Development packages ---------------------------------------------

dev_pkgs <- c(
"checkmate", "covr", "devtools", "distill", "fs", "here", "htmltools",
"knitr", "lintr", "purrr", "qs", "rstudioapi", "spelling",
"stringr", "targets", "tarchetypes", "testthat", "usethis",
"withr"
"checkmate", "covr", "crew", "devtools", "distill", "fs", "here",
"htmltools", "knitr", "lintr", "purrr", "qs", "rstudioapi",
"spelling", "stringr", "targets", "tarchetypes", "testthat",
"usethis", "withr"
)
renv::install(dev_pkgs)

Expand Down
49 changes: 20 additions & 29 deletions dev/run.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
# To view your current pipeline status, and possibly run/update it,
# you can simply source/execute this script (maybe using the
# `dev/03-run_cycle.R` script)
{
function(proceed = TRUE, save_all = TRUE) {
if (interactive()) {
if (
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
save_all
) {
rstudioapi::documentSaveAll()
}

targets::tar_visnetwork() |>
print()
(\() if (interactive()) {
if (
requireNamespace("rstudioapi") &&
rstudioapi::isAvailable()
) {
usethis::ui_done("All scripts saved.")
rstudioapi::documentSaveAll()
}

proceed <- usethis::ui_yeah(paste0(
"Do you want to make your pipeline? ",
"({usethis::ui_code('tar_make')})"
))
}
usethis::ui_todo("Check your pipeline.")
targets::tar_visnetwork() |>
print()

if (proceed) {
withr::with_envvar(
list(RSTUDIO_VERSION = "2021.09.0"), {
# devtools::test(stop_on_failure = TRUE)
targets::tar_make()
}
)
targets::tar_visnetwork(targets_only = TRUE) |>
print()
}
usethis::ui_todo("Make your pipeline.")
if (usethis::ui_yeah("Do you want to make your pipeline?")) {
targets::tar_make()
usethis::ui_todo("Review the updated status.")
targets::tar_visnetwork(targets_only = TRUE, outdated = FALSE) |>
print()
}
}()
})()



Loading

0 comments on commit ffdc05e

Please sign in to comment.