Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Initialize report using functions #12

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c95cbd4
usethis::create_tidy_package()
Felixmil Aug 29, 2023
133fc47
Setup package
Felixmil Aug 29, 2023
8b7d9a7
Move existing files to appropriate folders
Felixmil Aug 29, 2023
8715246
Add new_report function
Felixmil Aug 29, 2023
4ac95e7
Update DESCRIPTION
Felixmil Aug 29, 2023
dff3339
Add function documentation
Felixmil Aug 29, 2023
4cbf0ad
reduce the number of R-CMD-check to one config for now
Felixmil Aug 29, 2023
7edf26e
Create a new report now create a full directory with preconfigured me…
Felixmil Aug 29, 2023
a976651
merge main
Felixmil Aug 30, 2023
87a5588
fix function
Felixmil Aug 30, 2023
e0b99e8
Improve new_report function
Felixmil Sep 8, 2023
25bcb63
add files to template
Felixmil Sep 8, 2023
7095875
default printing of tables is using kable for nice looking rendering
Felixmil Sep 18, 2023
2c5d29c
Add dummy data, plots and text
Felixmil Sep 18, 2023
f486317
Add utils scripts
Felixmil Sep 18, 2023
05357f0
Update template
Felixmil Sep 18, 2023
4e394ca
--> camelCase
Felixmil Sep 19, 2023
52cceed
Add a new standalone tutorial template
Felixmil Sep 19, 2023
9518523
Moved main template in a folder
Felixmil Sep 19, 2023
1c645a5
update test
Felixmil Sep 19, 2023
6eda46c
Merged origin/main into initialize-report-using-functions
Felixmil Sep 19, 2023
55dff88
renamed template directory to report
Felixmil Sep 20, 2023
57b7020
renamed template Rproj and qmd file to be consistent across templates
Felixmil Sep 20, 2023
1fccf33
devtools::document(roclets = c('rd', 'collate', 'namespace'))
Felixmil Sep 20, 2023
09be9bc
Replace Report/ with Reports/ make notebook parameterized
Felixmil Sep 20, 2023
344ff27
Improve default report theme
Felixmil Sep 20, 2023
6b7370a
Improve theme
Felixmil Sep 21, 2023
723b097
Update test pdf reports with latest "theme"
Felixmil Sep 21, 2023
1fc9744
Add options to get/save data from path defined in projectConfiguratio…
Felixmil Sep 27, 2023
95dd9be
Add SimulationResults to the resultsFolder built from projectConfigur…
Felixmil Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ RoxygenNote: 7.2.3
URL: https://esqlabs.github.io/esqlabsR.reports/
Imports:
fs (>= 1.6.3),
glue (>= 1.6.2)
glue (>= 1.6.2),
rstudioapi (>= 0.14),
yaml (>= 2.3.7)
Remotes:
tidyverse/glue,
r-lib/withr,
r-lib/fs
r-lib/fs,
vubiostat/r-yaml,
rstudio/rstudioapi
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(new_report)
export(getYamlStartEnd)
export(newReport)
export(replaceYaml)
18 changes: 0 additions & 18 deletions R/initEsqlabsProject.R

This file was deleted.

44 changes: 44 additions & 0 deletions R/new-report.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Create a new report
#'
#' @param file_name a string that represent the name of the report that will be created.
#' @param path a string that represent the path where to create the report.
#' @param template one string (either `report` or `tutorial`) representing the type of template to initialize.
#' @param subtitle the subtitle of the report. empty by default.
#' @param author the name of the author(s). "esqlabs Gmbh" by default.
#' @param datetime the date time that will appear on the report in "YYYY-MM-DD HH:MM:SS" format. default to time of generation of the report.
#'
#' @export
#'
#' @examples
#' newReport("my_report", "report_folder/")
newReport <- function(report_title, path = "Reports/", template = "report", subtitle = NA, author = NA, datetime = NA) {


template_dir <- system.file(paste0("templates/", template), package = "esqlabsR.reports")
target_dir <- file.path(path, report_title)
project_filename <- glue::glue("{report_title}.Rproj")
project_fullpath <- file.path(target_dir, project_filename)
report_filename <- glue::glue("{report_title}.qmd")
report_fullpath <- file.path(target_dir, report_filename)

fs::dir_copy(path = template_dir, new_path = target_dir)

file.rename(file.path(target_dir, "template.Rproj"),
project_fullpath)

file.rename(file.path(target_dir, "template.qmd"),
report_fullpath)

args <- list(title = report_title,
subtitle = subtitle,
author = author,
date = datetime)

editYaml(report_fullpath, args)

if(interactive()){
rstudioapi::openProject(project_fullpath)
}

}

15 changes: 0 additions & 15 deletions R/new_report.R

This file was deleted.

85 changes: 85 additions & 0 deletions R/yaml-handler.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
editYaml <- function(file_path, args) {

original_yaml <- extractYaml(text = readLines(file_path))

new_yaml <- original_yaml

for (arg in names(args)) {
if(!is.na(args[[arg]])){
new_yaml[[arg]] <- args[[arg]]
}
}

replaceYaml(file_path = file_path,
new_yaml = new_yaml)

}


#' Get YAML header from a text vector
#'
#' @param text a vector of string the text read from a file
#'
#' @return a list representing the yaml that can be exported back to yaml format
#'
#' @examples
extractYaml <- function(text) {

yaml_lines_index <- getYamlStartEnd(text = text)

yaml <- yaml::read_yaml(text = text[yaml_lines_index[1]:yaml_lines_index[2]])

return(yaml)

}


#' Replace yaml header of a file
#'
#' @param file_path a string representing the file containing the yaml header to replace.
#' @param new_yaml a list representing the yaml header to inject
#' @export
#'
#' @examples
replaceYaml <- function(file_path, new_yaml) {

text <- readLines(file_path)

yaml_start_end <- getYamlStartEnd(text = text, only_content = FALSE)

text_without_yaml <- text[-c(yaml_start_end[1]:yaml_start_end[2])]

text_with_new_yaml <- c("---",
yaml::as.yaml(new_yaml),
"---",
text_without_yaml)

writeLines(text_with_new_yaml, file_path)

}

#' Identify where the yaml header starts and end in a file
#'
#' @param text a string vector containing the read lines of a file
#' @param only_content a logical defining if the returned index should be for
#' the yaml delimiters or the yaml content.
#'
#' @return a numeric vector of size two representing the index
#' @export
#'
#' @examples
getYamlStartEnd <- function(text, only_content = TRUE) {
yaml_delimiters_index <- which(text == "---")

if (only_content) {
index_correction = 1
} else {
index_correction = 0
}

yaml_lines_start_index <- yaml_delimiters_index[1]+index_correction
yaml_lines_end_index <- yaml_delimiters_index[2]-index_correction

return(c(yaml_lines_start_index, yaml_lines_end_index))

}
170 changes: 0 additions & 170 deletions inst/templates/esqlabsR.reports.qmd

This file was deleted.

1 change: 1 addition & 0 deletions inst/templates/report/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
Loading