Skip to content

Commit

Permalink
🎨 Improve rendering for better visuals
Browse files Browse the repository at this point in the history
  • Loading branch information
oduerr committed May 20, 2024
1 parent 480a929 commit 3c0b89f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 12 deletions.
25 changes: 18 additions & 7 deletions R/data_gen/Render_Quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
library(quarto)
library(magrittr)
library(yaml)
library(edudat)

# Define the source and destination directories
source_dir <- "data"
Expand All @@ -22,14 +23,16 @@ rendered_date <- format(Sys.Date(), "%B %d, %Y")
qmd_files <- list.files(source_dir, pattern = "\\.qmd$", full.names = TRUE)

# Function to create custom header
create_custom_header <- function(filename) {
csv_filename <- gsub("\\.qmd$", ".csv", filename)
link <- paste0(base_url, filename)
create_custom_header <- function(qmd_basename, attributes) {
#qmd_basename e.g. "abodauer.qmd"
load_name <- attributes$source
link <- paste0(base_url, qmd_basename)

header <- paste0(
"Rendered from [", filename, "](", link, ")\n\n",
"This file can be loaded with:\n\n",
"Rendered from [", qmd_basename, "](", link, ")\n\n",
"This file can be loaded using edudat with:\n\n",
"```r\n",
"df = load_data('", csv_filename, "')\n",
"df = load_data('", load_name, "')\n",
"```\n\n"
)
return(header)
Expand All @@ -56,8 +59,16 @@ readme_content <- list("### Created on ", rendered_date, "\n\n")

# Copy each .qmd file to the destination directory, modify it, and render it
for (qmd_file in qmd_files) {
#qmd_file <- qmd_files[1]
# Get the base filename without the path
qmd_basename <- basename(qmd_file)
name = gsub("\\.qmd$", "", qmd_basename)

# YAML file path
attributes <- get_yaml_attributes(name)

# Load the data into a data frame
#df = load_data(attributes$source)

# Extract the title from the .qmd file
title <- extract_title(qmd_file)
Expand All @@ -69,7 +80,7 @@ for (qmd_file in qmd_files) {
file.copy(qmd_file, dest_qmd_file, overwrite = TRUE)

# Create and prepend the custom header to the copied .qmd file
header_content <- create_custom_header(qmd_basename)
header_content <- create_custom_header(qmd_basename, attributes)
prepend_header(dest_qmd_file, header_content)

# Print message indicating which file is being rendered
Expand Down
3 changes: 3 additions & 0 deletions R/edudat/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
S3method(print,edudat)
S3method(summary,edudat)
export(as.stan_data)
export(get_cache_dir)
export(get_yaml_attributes)
export(list_cache_files)
export(load_data)
export(show_code)
export(show_data)
export(show_loading)
export(show_mini_help)
export(source_extra_code)
25 changes: 24 additions & 1 deletion R/edudat/R/edudat.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' Get the cache directory based on the operating system
#'
#' @return The path to the cache directory
#' @export
get_cache_dir <- function() {
if (.Platform$OS.type == "windows") {
return(normalizePath(file.path(Sys.getenv("USERPROFILE"), ".edudat_cache"), winslash = "/", mustWork = FALSE))
Expand Down Expand Up @@ -104,9 +105,14 @@ load_data <- function(source, verbose = FALSE) {
# Adding attributes to the data frame
attr(df, "dataset_name") <- name
attr(df, "download_url") <- download_url
attr(df, "source") <- source

# Save the attributes to a YAML file
attributes = c(attributes, list(dataset_name = name, download_url = download_url))
attributes = c(attributes, list(
dataset_name = name,
download_url = download_url,
source=source
))
file_path <- file.path(get_cache_dir(), paste0(name,".yaml"))
save_attributes_to_yaml(attributes, file_path)

Expand All @@ -117,6 +123,23 @@ load_data <- function(source, verbose = FALSE) {
return (df)
}


#' returns the YAML-Arributes of a dataset
#'
#' @param name Name of the dataset (e.g., "challenger")
#' @return A list containing the YAML attributes of the dataset or NULL if the dataset does not exist
#' @export
get_yaml_attributes <- function(name) {
file_path <- file.path(get_cache_dir(), paste0(name,".yaml"))
# check if the file exists
if (!file.exists(file_path)) {
return(NULL)
}
attributes <- yaml::read_yaml(file_path)
return(attributes)
}


#' Load the Quarto documentation file from repository and cache it locally
#'
#' @param name Name of the Quarto file (e.g., "challenger.qmd")
Expand Down
55 changes: 55 additions & 0 deletions R/edudat/R/quarto_related.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,58 @@ show_code <- function(df) {
return(code_str)
}

#' Generate R and Python code showing how to load a dataset, which is needed in quarto documents
#'
#' @param attributes A list containing the attributes of the dataset, including 'download_url' and 'source'
#' @return No return value, called for side effects. Outputs formatted R and Python code for loading the data.
#' @export
#' @examples
#'
#' ```{r, echo=FALSE, eval=TRUE, message=FALSE, results='asis'}
#' library(edudat)
#' df = load_data('abodauer.csv.gz')
#' attributes = attributes(df)
#' show_loading(attributes(df))
#' ```
show_loading <- function(attributes) {
r_code <- sprintf("df <- read.csv(\"%s\", stringsAsFactors = FALSE)", attributes$download_url)
r_code2 <- sprintf("df <- edudat::load_data(\"%s\")", attributes$source)
py_code <- sprintf("df = pd.read_csv(\"%s\")", attributes$download_url)

if (knitr::is_html_output()) {
cat(
"## Copy & Paste this code to load the data into R:\n\n",
"<pre><code class='language-r'>\n",
r_code,
"\n</code></pre>\n\n",
"## Copy & Paste this code to load the data into Python (need pandas as pd):\n\n",
"<pre><code class='language-python'>\n",
py_code,
"\n</code></pre>\n",
sep = ""
)
} else if (knitr::is_latex_output()) {
cat(
"\\begin{framed}\n",
"Loading: \\newline \n",
"\\footnotesize\n", # Make the text smaller
"Copy and Paste this code to load the data into R:\n\n",
"\\begin{verbatim}\n",
r_code,
"\n\\end{verbatim}\n\n",
"Using the edudat package (see https://github.com/tensorchiefs/data/):\n\n",
"\\begin{verbatim}\n",
r_code2,
"\n\\end{verbatim}\n\n",
"Copy and Paste this code to load the data into Python (need pandas as pd):\n\n",
"\\begin{verbatim}\n",
py_code,
"\n\\end{verbatim}\n",
"\\end{framed}\n",
sep = ""
)
} else {
cat("Output format not supported.")
}
}

4 changes: 4 additions & 0 deletions R/edudat/build_and_check.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#############################################
### PLEASE RESTART R BEFORE RUNNING THIS SCRIPT ###
#############################################

# Load necessary libraries
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
Expand Down
4 changes: 2 additions & 2 deletions docs/abodauer.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
Rendered from
[abodauer.qmd](https://github.com/tensorchiefs/data/tree/docs/data/abodauer.qmd)

This file can be loaded with:
This file can be loaded using edudat with:

``` r
df = load_data('abodauer.csv')
df = load_data('abodauer.csv.gz')
```

## Description
Expand Down
2 changes: 1 addition & 1 deletion docs/challenger.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Rendered from
[challenger.qmd](https://github.com/tensorchiefs/data/tree/docs/data/challenger.qmd)

This file can be loaded with:
This file can be loaded using edudat with:

``` r
df = load_data('challenger.csv')
Expand Down
2 changes: 1 addition & 1 deletion docs/lr4.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Rendered from
[lr4.qmd](https://github.com/tensorchiefs/data/tree/docs/data/lr4.qmd)

This file can be loaded with:
This file can be loaded using edudat with:

``` r
df = load_data('lr4.csv')
Expand Down

0 comments on commit 3c0b89f

Please sign in to comment.