Skip to content

Commit

Permalink
Some redoc, intentation & resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinFay committed Dec 24, 2021
1 parent 23f277a commit 27b70a5
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 89 deletions.
131 changes: 72 additions & 59 deletions R/bundle_resources.R
Original file line number Diff line number Diff line change
@@ -1,88 +1,101 @@
#' Automatically serve golem external resources
#'
#' This function is a wrapper around `htmltools::htmlDependency` that
#'
#' This function is a wrapper around `htmltools::htmlDependency` that
#' automatically bundles the CSS and JavaScript files in `inst/app/www`
#' and which are created by `golem::add_css_file()` , `golem::add_js_file()`
#' and `golem::add_js_handler()`.
#'
#' This function also preload [activate_js()] which allows to
#'
#' This function also preload [activate_js()] which allows to
#' use preconfigured JavaScript functions via [invoke_js()].
#'
#' @param path The path to the folder where the external files are located.
#' @param app_title The title of the app, to be used as an application title.
#' @param app_builder The name of the app builder to add as a meta tag.
#' Turn to NULL if you don't want this meta tag to be included.
#' @param app_builder The name of the app builder to add as a meta tag.
#' Turn to NULL if you don't want this meta tag to be included.
#' @inheritParams htmltools::htmlDependency
#'
#' @param with_sparkles C'est quand que tu vas mettre des paillettes dans ma vie Kevin?
#'
#' @importFrom htmltools htmlDependency
#' @export
#'
#'
#' @return an htmlDependency
bundle_resources <- function(
path,
app_title,
name = "golem_resources",
version = "0.0.1",
meta = NULL,
head = NULL,
attachment = NULL,
package = NULL,
all_files = TRUE,
app_builder = "golem"
){

bundle_resources <- function(path,
app_title,
name = "golem_resources",
version = "0.0.1",
meta = NULL,
head = NULL,
attachment = NULL,
package = NULL,
all_files = TRUE,
app_builder = "golem",
with_sparkles = FALSE) {
if (
length(
list.files(path)
list.files(path)
) > 0
){
) {
res <- list()
res[[
length(res) + 1
]] <- htmltools::htmlDependency(
name,
version,
src = path,
script = list.files(
path,
pattern = "\\.js$",
recursive = TRUE
length(res) + 1
]] <- htmltools::htmlDependency(
name,
version,
src = path,
script = list.files(
path,
pattern = "\\.js$",
recursive = TRUE
),
meta = c("app-builder" = app_builder, meta),
head = c(
as.character(
tags$title(app_title)
),
meta =c("app-builder" = app_builder, meta),
head = c(
as.character(
tags$title(app_title)
),
as.character(
golem::activate_js()
),
head
as.character(
golem::activate_js()
),
attachment = attachment,
package = package,
all_files = all_files
)
# For some reason `htmlDependency` doesn't bundle css,
head
),
attachment = attachment,
package = package,
all_files = all_files
)
# For some reason `htmlDependency` doesn't bundle css,
# So add them by hand
css_nms <- paste0(
basename(path),
"/",
basename(path),
"/",
list.files(
path,
pattern = "\\.css$",
pattern = "\\.css$",
recursive = TRUE
)
)
)

for (i in css_nms ){

for (i in css_nms) {
res[[
length(res) + 1
]] <- tags$link(
href = i,
rel = "stylesheet"
)
}

if (with_sparkles) {
res[[
length(res) + 1
]] <- tags$link(
href = i,
rel = "stylesheet"
)
length(res) + 1
]] <- htmlDependency(
"sparkles",
version = utils::packageVersion("golem"),
src = system.file(
"utils",
package = "golem"
),
script = "sparkle.js"
)
}

res
}
}
}
40 changes: 37 additions & 3 deletions R/use_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use_utils_ui <- function(pkg = get_golem_wd(),
) {
file_already_there_dance(
where = pth,
open = FALSE
open_file = FALSE
)
} else {
use_utils_test_ui()
Expand All @@ -57,20 +57,54 @@ use_utils_test_ui <- function(pkg = get_golem_wd()) {
)

if (added) {
cat_green_tick("Tests on utils UI added")
cat_green_tick("Tests on utils_ui added")
}
}

#' @export
#' @rdname utils_files
use_utils_server <- function(pkg = get_golem_wd()) {
use_utils_server <- function(pkg = get_golem_wd(),
with_test = FALSE) {
added <- use_utils(
file_name = "golem_utils_server.R",
folder_name = "R",
pkg = pkg
)
if (added) {
cat_green_tick("Utils server added")

if (with_test) {
if (!isTRUE(dir.exists("tests"))) {
use_testthat()
}
pth <- path(
pkg, "tests", "testthat", "test-golem_utils_server.R"
)
if (
file.exists(pth)
) {
file_already_there_dance(
where = pth,
open_file = FALSE
)
} else {
# use_utils_test_server()
}
}
}
}

#' @export
#' @rdname utils_files
use_utils_test_ui <- function(pkg = get_golem_wd()) {
added <- use_utils(
file_name = "test-golem_utils_server.R",
folder_name = "tests/testthat",
pkg = pkg
)

if (added) {
cat_green_tick("Tests on utils_server added")
}
}

Expand Down
37 changes: 18 additions & 19 deletions inst/utils/golem_utils_server.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Inverted versions of in, is.null and is.na
#'
#'
#' @noRd
#'
#'
#' @examples
#' 1 %not_in% 1:10
#' not_null(NULL)
Expand All @@ -12,24 +12,24 @@ not_null <- Negate(is.null)
not_na <- Negate(is.na)

#' Removes the null from a vector
#'
#'
#' @noRd
#'
#' @example
#'
#' @example
#' drop_nulls(list(1, NULL, 2))
drop_nulls <- function(x){
drop_nulls <- function(x) {
x[!sapply(x, is.null)]
}

#' If x is `NULL`, return y, otherwise return x
#'
#'
#' @param x,y Two elements to test, one potentially `NULL`
#'
#'
#' @noRd
#'
#'
#' @examples
#' NULL %||% 1
"%||%" <- function(x, y){
"%||%" <- function(x, y) {
if (is.null(x)) {
y
} else {
Expand All @@ -38,14 +38,14 @@ drop_nulls <- function(x){
}

#' If x is `NA`, return y, otherwise return x
#'
#'
#' @param x,y Two elements to test, one potentially `NA`
#'
#'
#' @noRd
#'
#'
#' @examples
#' NA %||% 1
"%|NA|%" <- function(x, y){
#' NA %|NA|% 1
"%|NA|%" <- function(x, y) {
if (is.na(x)) {
y
} else {
Expand All @@ -54,11 +54,10 @@ drop_nulls <- function(x){
}

#' Typing reactiveValues is too long
#'
#'
#' @inheritParams reactiveValues
#' @inheritParams reactiveValuesToList
#'
#'
#' @noRd
rv <- function(...) shiny::reactiveValues(...)
rvtl <- function(...) shiny::reactiveValuesToList(...)

rvtl <- function(...) shiny::reactiveValuesToList(...)
Loading

0 comments on commit 27b70a5

Please sign in to comment.