Skip to content

error for adding external Javascript #91

@z267xu

Description

@z267xu
  1. If we set external Javascript in
    Dash$new(external_scripts = "your_own_js"),
    we would have error like:

"Error in as.character(function (description, open = "", blocking = TRUE, : cannot coerce type 'closure' to vector of type 'character'"

The error comes from generate_js_dist_html function in dashR package utils file. The reason is the typo in sprintf, which should be href instead of url.

dashR/R/utils.R

Lines 513 to 520 in 6bc44b5

generate_js_dist_html <- function(href,
local = FALSE,
local_path = NULL,
prefix = NULL) {
if (!(local)) {
if (grepl("^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$", href, perl=TRUE)) {
sprintf("<script src=\"%s\"></script>", url)
}

  1. Also if we mix resources with attributes like src, integrity, crossorigin, etc, like:

external_scripts <- list(list(
src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.js',
integrity = 'sha256-Qqd/EfdABZUcAxjOkMi8eGEivtdTkh3b65xCZL4qAQA=',
crossorigin = 'anonymous'))

Dash$new(external_scripts = external_scripts)

we may have error like

"warning: the condition has length > 1 and only the first element will be used from if (grepl("^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$",
error: values must be length 1,
but FUN(X[[2]]) result is length 4 from vapply(self$config$external_stylesheets, generate_css_dist_html,"

I guess we should manipulate input href before we call function generate_js_dist_html() and generate_css_dist_html()

dashR/R/utils.R

Lines 492 to 532 in 6bc44b5

generate_css_dist_html <- function(href,
local = FALSE,
local_path = NULL,
prefix = NULL) {
if (!(local)) {
if (grepl("^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$", href, perl=TRUE)) {
sprintf("<link href=\"%s\" rel=\"stylesheet\">", href)
}
else
stop(sprintf("Invalid URL supplied in external_stylesheets. Please check the syntax used for this parameter."), call. = FALSE)
} else {
# strip leading slash from href if present
href <- sub("^/", "", href)
modified <- as.integer(file.mtime(local_path))
sprintf("<link href=\"%s%s?m=%s\" rel=\"stylesheet\">",
prefix,
href,
modified)
}
}
generate_js_dist_html <- function(href,
local = FALSE,
local_path = NULL,
prefix = NULL) {
if (!(local)) {
if (grepl("^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$", href, perl=TRUE)) {
sprintf("<script src=\"%s\"></script>", url)
}
else
stop(sprintf("Invalid URL supplied. Please check the syntax used for this parameter."), call. = FALSE)
} else {
# strip leading slash from href if present
href <- sub("^/", "", href)
modified <- as.integer(file.mtime(local_path))
sprintf("<script src=\"%s%s?m=%s\"></script>",
prefix,
href,
modified)
}
}

@rpkyle

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions