Skip to content

Commit

Permalink
Render on the desired directory (#20)
Browse files Browse the repository at this point in the history
* Make it more robust

* Check quarto version that should be used

* Add the remaining code

* Render in a separate folder

* Add prefix to files

* Update quarto version

* Fix problems with prefix_output

* Add file

* Add riskmetric file as an example

* update example to not leave anything

* Set up website

* Simplify rendering steps

---------

Co-authored-by: Lluís <[email protected]>
  • Loading branch information
llrs-roche and llrs authored Feb 7, 2025
1 parent e4369dc commit d4b290f
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tinytex: true
version: 1.7.13

- uses: r-lib/actions/check-r-package@v2
with:
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
release:
types: [published]
workflow_dispatch:

name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/[email protected]
with:
clean: false
branch: gh-pages
folder: docs
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ License: MIT + file LICENSE
URL: https://github.com/pharmaR/riskreports
BugReports: https://github.com/pharmaR/riskreports/issues
Depends:
R (>= 3.5.0)
R (>= 4.4.0)
Imports:
methods,
quarto (>= 1.4.4)
Expand Down
61 changes: 44 additions & 17 deletions R/reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
#' @param params A list of execute parameters passed to the template
#' @param ... Additional arguments passed to `quarto::quarto_render()`
#'
#' @return A report
#' @return A path to the reports generated, called by its side effects.
#' @export
#' @examples
#' package_report(
#' pr <- package_report(
#' package_name = "dplyr",
#' package_version = "1.1.4",
#' params = list(
#' assessment_path = system.file("assessments/dplyr.rds", package = "riskreports"),
#' image = "rhub/ref-image")
#' )
#'
#' @export
#' pr
#' file.remove(pr)
package_report <- function(
package_name,
package_version,
Expand All @@ -41,7 +43,7 @@ package_report <- function(
}

full_name <- paste0(package_name, "_v", package_version)
output_file <- paste0("validation_report_", full_name,".html")
output_file <- paste0("validation_report_", full_name, ".qmd")

params$package_name <- package_name
params$package_version <- package_version
Expand All @@ -57,26 +59,51 @@ package_report <- function(
params$assessment_path <- normalizePath(params$assessment_path, mustWork = TRUE)
}
# Bug on https://github.com/quarto-dev/quarto-cli/issues/5765

v <- quarto::quarto_version()
if (v < package_version("1.7.13")) {
warning("Please install the latest (devel) version of Quarto")
}
# https://github.com/quarto-dev/quarto-r/issues/81#issuecomment-1375691267
# quarto rendering happens in the same place as the file/project
# To avoid issues copy to a different place and render there.
render_dir <- output_dir()
fc <- file.copy(from = template_path,
to = file.path(render_dir, output_file), overwrite = TRUE,
copy.date = TRUE)

if (any(!fc)) {
stop("Copying to the rendering directory failed.")
}

template <- list.files(render_dir, full.names = TRUE)
template <- template[endsWith(template, "qmd")]

if (length(template) > 1) {
stop("There are more than one template!\n",
"Please have only one quarto file on the directory.")
}

prefix_output <- paste0("validation_report_", full_name)
pre_rendering <- list.files(render_dir, full.names = TRUE)

suppressMessages({suppressWarnings({
out <- quarto::quarto_render(
template_path,
input = template,
output_format = "all",
execute_params = params,
...
)
})})

# Move reports after creation (work around issue https://github.com/quarto-dev/quarto-cli/issues/5765)
lf <- list.files(dirname(template_path), full.names = TRUE)
files_template <- lf[!dir.exists(lf)]
file_name <- tools::file_path_sans_ext(basename(template_path))
files_template <- files_template[startsWith(basename(files_template),
file_name)]
files_template <- files_template[!endsWith(files_template, ".qmd")]
output_file = paste0("validation_report_", full_name,
".", tools::file_ext(files_template))
file.rename(files_template, output_file)
invisible(output_file)
fr <- file.remove(file.path(render_dir, output_file))
if (any(!fr)) {
warning("Failed to remove the quarto template used from the directory.")
}

post_rendering <- list.files(render_dir, full.names = TRUE)
output_files <- setdiff(post_rendering, pre_rendering)
invisible(output_files)
}

is.empty <- function(x) {
Expand Down
15 changes: 15 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ fill_in <- function(list, names) {
list[missing_names] <- NA
list
}


output_dir <- function() {
opt <- getOption("riskreports_output_dir", default = NULL)
env <- Sys.getenv("RISKREPORTS_OUTPUT_DIR", unset = getwd())

opt %||% env
}

rendering_dir <- function() {
opt <- getOption("riskreports_rendering_dir", default = NULL)
env <- Sys.getenv("RISKREPORTS_RENDERING_DIR", unset = tempdir())

opt %||% env
}
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
url: ~
template:
bootstrap: 5

Binary file added inst/assessments/dplyr.rds
Binary file not shown.
9 changes: 4 additions & 5 deletions inst/report/pkg_template.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
title: "Validation Report - `r paste0(params$package_name, '@', params$package_version)`"
date: "`r Sys.time()`"
date-format: "ddd MMM DD hh:mm:ss A YYYY"
params:
package_name: dplyr
Expand Down Expand Up @@ -83,7 +82,7 @@ d_riskmetric <- readRDS(risk_path)
# Assesment produces a data.frame with one row
r_riskmetric <- riskreports::assessment(d_riskmetric)
is_na <- sapply(r_riskmetric, is.na)
is_na <- sapply(r_riskmetric, function(x){is.na(x) || (is.character(x) && startsWith(x, "no applicable"))})
knitr::kable(r_riskmetric[, !is_na]) # Use this to have some summary text and report it.
# d_riskmetric
```
Expand Down Expand Up @@ -120,15 +119,15 @@ d_riskmetric$export_help[sort(d_riskmetric$exported_namespace)]

### Examples

There are `r sum(d_riskmetric$has_examples)` help pages with examples, from `r length(d_riskmetric$has_examples)` (`r sprintf("%0.2f", sum(d_riskmetric$has_examples)/length(d_riskmetric$has_examples)*100) ` %).
There are `r if (!is_na["has_examples"]) sum(d_riskmetric$has_examples)` help pages with examples, from `r if (!is_na["has_examples"]) {length(d_riskmetric$has_examples)}` (`r if (!is_na["has_examples"]) {sprintf("%0.2f", sum(d_riskmetric$has_examples)/length(d_riskmetric$has_examples)*100)} else {"?"} ` %).

### NEWS

The package has `r d_riskmetric$has_news` NEWS file and it is `r if(!d_riskmetric$news_current) "not"` current.
The package has `r if ("has_news" %in% names(d_riskmetric) && !is.null(d_riskmetric$has_news)) print(d_riskmetric$has_news)` NEWS file and it is `r if (is.null(d_riskmetric$news_current) && !d_riskmetric$news_current) "not"` current.

### License

The package uses `r d_riskmetric$license`.
The package uses `r if ("license" %in% names(d_riskmetric) && !is.null(d_riskmetric$license)) print(d_riskmetric$license)`.



Expand Down
8 changes: 5 additions & 3 deletions man/package_report.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4b290f

Please sign in to comment.