Skip to content

Commit 61e6dfa

Browse files
authored
Support returning asset URLs via public method within Dash class (#160)
1 parent 8c9678b commit 61e6dfa

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

R/dash.R

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,53 @@ Dash <- R6::R6Class(
702702
}
703703
private$callback_context_
704704
},
705-
705+
706+
# ------------------------------------------------------------------------
707+
# return asset URLs
708+
# ------------------------------------------------------------------------
709+
get_asset_url = function(asset_path, prefix = "/") {
710+
app_root_path <- Sys.getenv("DASH_APP_PATH")
711+
712+
if (app_root_path == "" && getAppPath() != FALSE) {
713+
# app loaded via source(), root path is known
714+
app_root_path <- dirname(private$app_root_path)
715+
} else {
716+
# app not loaded via source(), env var not set, no reliable way to ascertain root path
717+
warning("application not started via source(), and DASH_APP_PATH environment variable is undefined. get_asset_url returns NULL since root path cannot be reliably identified.")
718+
return(NULL)
719+
}
720+
721+
asset <- lapply(private$asset_map,
722+
function(x) {
723+
# asset_path should be prepended with the full app root & assets path
724+
# if leading slash(es) present in asset_path, remove them before
725+
# assembling full asset path
726+
asset_path <- file.path(app_root_path,
727+
private$assets_folder,
728+
sub(pattern="^/+",
729+
replacement="",
730+
asset_path))
731+
return(names(x[x == asset_path]))
732+
}
733+
)
734+
asset <- unlist(asset, use.names = FALSE)
735+
736+
if (length(asset) == 0)
737+
stop(sprintf("the asset path '%s' is not valid; please verify that this path exists within the '%s' directory.",
738+
asset_path,
739+
private$assets_folder))
740+
741+
# strip multiple slashes if present, since we'll
742+
# introduce one when we concatenate the prefix and
743+
# asset path & prepend the asset name with route prefix
744+
return(gsub(pattern="/+",
745+
replacement="/",
746+
paste(prefix,
747+
private$assets_url_path,
748+
asset,
749+
sep="/")))
750+
},
751+
706752
# ------------------------------------------------------------------------
707753
# convenient fiery wrappers
708754
# ------------------------------------------------------------------------
@@ -1051,7 +1097,7 @@ Dash <- R6::R6Class(
10511097

10521098
# need to check whether the assets have actually been updated, since
10531099
# this function is also called to generate the asset map
1054-
if (private$asset_modtime > private$app_launchtime) {
1100+
if (!is.na(private$asset_modtime) && private$asset_modtime > private$app_launchtime) {
10551101
# here we use mapply to make pairwise comparisons for each of the
10561102
# asset classes in the map -- before/after for css, js, and other
10571103
# assets; this returns a list whose subelements correspond to each
Binary file not shown.

0 commit comments

Comments
 (0)