Skip to content

Commit

Permalink
add packages argument
Browse files Browse the repository at this point in the history
  • Loading branch information
luisDVA committed Oct 30, 2024
1 parent 9f63b24 commit c58c58d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

export(encode_image)
export(generate_hexsession_js)
export(get_pkg_data)
export(make_tile)
export(snap_tile)
importFrom(base64enc,base64encode)
Expand Down
47 changes: 35 additions & 12 deletions R/get_package_data.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
#' Get package logos and urls
#'
#' @return A list with paths to logos and URLs for loaded packages
#' @export
get_pkg_data <- function() {
attached_pkgs <- getLoaded()
imgpaths <- find_imgpaths(attached_pkgs)
logopaths <- find_logopaths(imgpaths, pkgnames=attached_pkgs)
pendingLogos <- make_missingLogos(attached_pkgs, logopaths)
logopaths[is.na(logopaths)] <- pendingLogos
urlsForlinks <- pkgurls(attached_pkgs)
#' Get package data
#' @param packages Character vector of package names (default is NULL, uses loaded packages)
#' @return A list containing logopaths and urls for the packages
get_pkg_data <- function(packages = NULL) {
if (is.null(packages)) {
packages <- getLoaded()
} else {
# Check if specified packages are installed
not_installed <- packages[!packages %in% installed.packages()[,"Package"]]
if (length(not_installed) > 0) {
stop("The following packages are not installed: ", paste(not_installed, collapse = ", "))
}
}

list(logopaths = logopaths, urls = urlsForlinks)
imagepaths <- find_imgpaths(packages)
logopaths <- find_logopaths(imagepaths, packages)

# Generate missing logos
missing <- is.na(logopaths)
if (any(missing)) {
generated <- make_missingLogos(packages[missing], logopaths[missing])
logopaths[missing] <- generated
}

# Get package URLs
urls <- sapply(packages, function(pkg) {
desc <- packageDescription(pkg)
url <- desc$URL
if (is.null(url)) {
paste0("https://cran.r-project.org/package=", pkg)
} else {
strsplit(url, ",")[[1]][1] # Use the first URL if multiple are provided
}
})

list(logopaths = logopaths, urls = urls)
}
36 changes: 18 additions & 18 deletions R/make_tile.R
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
#' Generate tile of package logos
#'
#' @param dark_mode Draw the tile on a dark background?
#' @param packages Character vector of package names to include (default: NULL, which uses loaded packages)
#' @return Path to the output file
#' @importFrom jsonlite toJSON
#' @importFrom base64enc base64encode
#' @export
make_tile <- function(dark_mode=FALSE) {

make_tile <- function(dark_mode=FALSE, packages=NULL) {
# Create a temporary directory in the current working directory
temp_dir <- file.path(getwd(), "temp_hexsession")
dir.create(temp_dir, showWarnings = FALSE)
temp_dir <- file.path(getwd(), "temp_hexsession")
dir.create(temp_dir, showWarnings = FALSE)

# Generate package data and save to temporary file in the temp directory
package_data <- get_pkg_data()
temp_file <- file.path(temp_dir, "package_data.rds")
saveRDS(package_data, temp_file)
# Generate package data and save to temporary file in the temp directory
package_data <- get_pkg_data(packages)
temp_file <- file.path(temp_dir, "package_data.rds")
saveRDS(package_data, temp_file)

# Generate JavaScript file
js_file <- file.path(temp_dir, "hexsession.js")
generate_hexsession_js(package_data$logopaths, package_data$urls, dark_mode, js_file)
# Generate JavaScript file
js_file <- file.path(temp_dir, "hexsession.js")
generate_hexsession_js(package_data$logopaths, package_data$urls, dark_mode, js_file)

# Path to the Quarto template
template_path <- system.file("templates", "hexout.qmd", package = "hexsession")
# Path to the Quarto template
template_path <- system.file("templates", "hexout.qmd", package = "hexsession")

# Copy the template to the temp directory
file.copy(template_path, file.path(temp_dir, "hexout.qmd"), overwrite = TRUE)
# Copy the template to the temp directory
file.copy(template_path, file.path(temp_dir, "hexout.qmd"), overwrite = TRUE)

# Build and run the Quarto cli render call
quarto_call <-
# Build and run the Quarto cli render call
quarto_call <-
sprintf(
'quarto render "%s" -P dark_mode:%s',
file.path(temp_dir, "hexout.qmd"),tolower(as.character(dark_mode))
)
system(quarto_call)
system(quarto_call)

viewer <- getOption("viewer")
viewer("temp_hexsession/hexout.html")
Expand Down
9 changes: 8 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ knitr::opts_chunk$set(
[![CRAN status](https://www.r-pkg.org/badges/version/hexsession)](https://CRAN.R-project.org/package=hexsession)
<!-- badges: end -->

The goal of hexsession is to create a tile of hexagonal logos for each loaded package in a session (all packages attached to the search path except for base packages).
The goal of hexsession is to create a tile of hexagonal logos for packages installed on your machine. Tiles can be created for a set of packages specified with a character vector, or for the loaded packages in your session (all packages attached to the search path except for base packages).

## Installation

Expand All @@ -35,12 +35,19 @@ remotes::install_github("luisdva/hexsession")

With hexsession installed, we can create a self-contained HTML file with tiled hex logos for all loaded packages in a session. If a package does not have a logo bundled in `man/figures/` or if the image cannot be found easily, a generic-looking logo with the package name will be generated.

* svg files are internally converted to png
* If the images bundled with a package do not match 'logo', a users are prompted to specify which file to use.



For a given session with libraries loaded in addition to base packages:

```{r example, eval=FALSE}
library(hexsession)
make_tile()
# custom set of packages
make_tile(packages=c("terra","sf","tidyr"))
```

The `make_tile()` function will render the HTML output in a new folder in the working directory using a Quarto template file that will also be copied to this new directory.
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](h
status](https://www.r-pkg.org/badges/version/hexsession)](https://CRAN.R-project.org/package=hexsession)
<!-- badges: end -->

The goal of hexsession is to create a tile of hexagonal logos for each
loaded package in a session (all packages attached to the search path
except for base packages).
The goal of hexsession is to create a tile of hexagonal logos for
packages installed on your machine. Tiles can be created for a set of
packages specified with a character vector, or for the loaded packages
in your session (all packages attached to the search path except for
base packages).

## Installation

Expand All @@ -32,11 +34,18 @@ not have a logo bundled in `man/figures/` or if the image cannot be
found easily, a generic-looking logo with the package name will be
generated.

- svg files are internally converted to png
- If the images bundled with a package do not match ‘logo’, a users are
prompted to specify which file to use.

For a given session with libraries loaded in addition to base packages:

``` r
library(hexsession)
make_tile()

# custom set of packages
make_tile(packages=c("terra","sf","tidyr"))
```

The `make_tile()` function will render the HTML output in a new folder
Expand Down
11 changes: 7 additions & 4 deletions man/get_pkg_data.Rd

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

4 changes: 3 additions & 1 deletion man/make_tile.Rd

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

0 comments on commit c58c58d

Please sign in to comment.