Skip to content

Commit

Permalink
support svglite dependency inference (#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Jul 9, 2024
1 parent 37f5b31 commit 449d41f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# renv (development version)

* `renv` now infers a dependency on the `svglite` package if it detects
calls of the form `ggsave(filename = "path.svg")`. (#1930)

* `renv` now supports setting of GitHub authentication credentials via
any of `GITHUB_TOKEN`, `GITHUB_PAT`, and `GH_TOKEN`. (#1937)

Expand Down
25 changes: 25 additions & 0 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ renv_dependencies_discover_r <- function(path = NULL,
renv_dependencies_discover_r_box,
renv_dependencies_discover_r_targets,
renv_dependencies_discover_r_glue,
renv_dependencies_discover_r_ggplot2,
renv_dependencies_discover_r_parsnip,
renv_dependencies_discover_r_database
)
Expand Down Expand Up @@ -1423,6 +1424,30 @@ renv_dependencies_discover_r_glue <- function(node, stack, envir) {

}

renv_dependencies_discover_r_ggplot2 <- function(node, stack, envir) {

node <- renv_call_expect(node, "ggplot2", "ggsave")
if (is.null(node))
return(FALSE)

# check for attempts to save to '.svg', and assume svglite is
# required in this scenario.
matched <- catch(match.call(function(filename, ...) {}, node))
if (inherits(matched, "error"))
return(FALSE)

filename <- matched$filename
if (!is.character(filename))
return(FALSE)

if (!endswith(filename, ".svg"))
return(FALSE)

envir[["svglite"]] <- TRUE
TRUE

}

renv_dependencies_discover_r_glue_impl <- function(string, node, envir) {

# get open, close delimiters
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,18 @@ test_that("dependencies() with different extensions", {
expect_true("B" %in% deps$Package)

})

test_that("dependencies() can infer an svglite dependency from ggsave", {

document <- heredoc('
library(ggplot2)
ggsave(filename = "test.svg")
')

file <- renv_scope_tempfile("renv-test-", fileext = ".R")
writeLines(document, con = file)

deps <- dependencies(file, quiet = TRUE)
expect_contains(deps$Package, "svglite")

})

0 comments on commit 449d41f

Please sign in to comment.