Skip to content

Commit

Permalink
fix for pkgs without explicit urls
Browse files Browse the repository at this point in the history
  • Loading branch information
luisDVA committed Oct 7, 2024
1 parent 6b0a0a2 commit a9171fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
65 changes: 39 additions & 26 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#' Get loaded packages
#' @return A character vector of the attached packages (excludes base packages)
getLoaded <- function() {
#also exclude hexsession
basepkgs <- c("stats", "graphics", "grDevices", "utils", "datasets",
"methods", "base","hexsession")
# also exclude hexsession
basepkgs <- c(
"stats", "graphics", "grDevices", "utils", "datasets",
"methods", "base", "hexsession"
)
(.packages())[!(.packages()) %in% basepkgs]
}

Expand All @@ -13,9 +15,10 @@ getLoaded <- function() {
find_imgpaths <- function(pkgnames) {
lapply(pkgnames, function(x) {
list.files(system.file(package = x),
pattern = "\\.png$|\\.jpg$|\\.svg$",
recursive = TRUE,
full.names = TRUE)
pattern = "\\.png$|\\.jpg$|\\.svg$",
recursive = TRUE,
full.names = TRUE
)
})
}

Expand All @@ -25,7 +28,7 @@ find_imgpaths <- function(pkgnames) {
find_logopaths <- function(imagepaths) {
logopaths <- lapply(imagepaths, function(x) x[grepl("logo", x, ignore.case = TRUE)])
logopaths[lengths(logopaths) == 0] <- NA_character_
logopaths <- sapply(logopaths,"[[",1)
logopaths <- sapply(logopaths, "[[", 1)
unlist(logopaths)
}

Expand All @@ -34,25 +37,36 @@ find_logopaths <- function(imagepaths) {
#' @importFrom utils packageDescription
#' @return A vector of package URLs
pkgurls <- function(pkgnames) {
allurls <- purrr::map(pkgnames,\(x) packageDescription(x,fields="URL"))
splturls <- purrr::map(allurls,\(x) unlist(strsplit(x,",|\n")))
purrr::map_chr(splturls,`[`,1)
allurls <- purrr::map(pkgnames, \(x) {
url <- packageDescription(x, fields = "URL")
if (is.null(url) || all(is.na(url)) || all(url == "")) NA_character_ else url
})
splturls <- purrr::map(allurls, \(x) {
if (all(is.na(x))) NA_character_ else unlist(strsplit(x, ",|\n"))
})
purrr::map_chr(splturls, \(x) {
if (all(is.na(x))) NA_character_ else x[1]
})
}



#' Encode image to Base64
#' @param file_path Path to an image file
#' @return Base64 encoded string of the image
#' @export
encode_image <- function(file_path) {
tryCatch({
encoded <- base64enc::base64encode(file_path)
ext <- tolower(tools::file_ext(file_path))
paste0("data:image/", ext, ";base64,", encoded)
}, error = function(e) {
warning(paste("Error encoding file:", file_path, "-", e$message))
NULL
})
tryCatch(
{
encoded <- base64enc::base64encode(file_path)
ext <- tolower(tools::file_ext(file_path))
paste0("data:image/", ext, ";base64,", encoded)
},
error = function(e) {
warning(paste("Error encoding file:", file_path, "-", e$message))
NULL
}
)
}

#' Create missing logos
Expand All @@ -65,20 +79,19 @@ make_missingLogos <- function(attached_pkgs, logopaths) {


create_logo <- function(pkgname) {

widthhex <- magick::image_info(basehex)$width

fig <- magick::image_blank(700, 700) |>
magick::image_annotate(pkgname, size = 100, color = 'white') |>
magick::image_annotate(pkgname, size = 100, color = "white") |>
magick::image_trim() |>
magick::image_border('none') |>
magick::image_resize(paste0(widthhex-30, 'x'))
magick::image_border("none") |>
magick::image_resize(paste0(widthhex - 30, "x"))

magick::image_composite(basehex, fig, operator = 'SrcOver',gravity = "center")
magick::image_composite(basehex, fig, operator = "SrcOver", gravity = "center")
}

logoimgs <- purrr::map(pending,create_logo)
imgpaths <- paste0(tempfile(),"_",pending,".png")
purrr::walk2(logoimgs,imgpaths,\(x,y) magick::image_write(x,y))
logoimgs <- purrr::map(pending, create_logo)
imgpaths <- paste0(tempfile(), "_", pending, ".png")
purrr::walk2(logoimgs, imgpaths, \(x, y) magick::image_write(x, y))
imgpaths
}
17 changes: 12 additions & 5 deletions inst/templates/hexout.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,30 @@ document.addEventListener('DOMContentLoaded', function() {
imagePaths.forEach((path, index) => {
const div = document.createElement('div');
const a = document.createElement('a');
const img = document.createElement('img');
img.src = path;
img.alt = 'Hexagon Image ' + (index + 1);
a.href = linkUrls[index];
a.target = '_blank'; // Opens link in a new tab
if (linkUrls[index] && linkUrls[index] !== "NA") {
// If there's a valid URL, wrap the image in a link
const a = document.createElement('a');
a.href = linkUrls[index];
a.target = '_blank'; // Opens link in a new tab
a.appendChild(img);
div.appendChild(a);
} else {
// If there's no URL or it's "NA", just add the image without a link
div.appendChild(img);
}
a.appendChild(img);
div.appendChild(a);
container.appendChild(div);
});
});
</script>
```

0 comments on commit a9171fb

Please sign in to comment.