Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serializer): Add support for ragg and svglite #964

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Suggests:
geojsonsf,
redoc,
rapidoc,
sf
sf,
ragg,
svglite
RoxygenNote: 7.3.2
Collate:
'async.R'
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export(register_serializer)
export(registered_docs)
export(registered_parsers)
export(registered_serializers)
export(serializer_agg_jpeg)
export(serializer_agg_png)
export(serializer_agg_tiff)
export(serializer_bmp)
export(serializer_cat)
export(serializer_content_type)
Expand All @@ -92,6 +95,7 @@ export(serializer_png)
export(serializer_print)
export(serializer_rds)
export(serializer_svg)
export(serializer_svglite)
export(serializer_text)
export(serializer_tiff)
export(serializer_tsv)
Expand Down
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# plumber (development version)

* Added support for graphic devices provided by ragg and svglite (@thomasp85
#964)

# plumber 1.2.2

* Allow to set plumber options using environment variables `?options_plumber`. (@meztez #934)
* Allow to set plumber options using environment variables `?options_plumber`. (@meztez #934)
* Add support for quoted boundary for multipart request parsing. (@meztez #924)
* Fix #916, related to `parseUTF8` return value attribute `srcfile` on Windows. (#930)
* Fix #916, related to `parseUTF8` return value attribute `srcfile` on Windows. (#930)

# plumber 1.2.1

Expand Down
65 changes: 58 additions & 7 deletions R/serializer.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,56 @@ serializer_pdf <- function(..., type = "application/pdf") {
)
}

# ragg -------------------------------------------------------------------------

#' @describeIn serializers JPEG image serializer using ragg. See also: [ragg::agg_jpeg()]
#' @export
serializer_agg_jpeg <- function(..., type = "image/jpeg") {
rlang::check_installed("ragg")
serializer_device(
type = type,
dev_on = function(filename) {
ragg::agg_jpeg(filename, ...)
}
)
}
#' @describeIn serializers PNG image serializer using ragg. See also: [ragg::agg_png()]
#' @export
serializer_agg_png <- function(..., type = "image/png") {
rlang::check_installed("ragg")
serializer_device(
type = type,
dev_on = function(filename) {
ragg::agg_png(filename, ...)
}
)
}
#' @describeIn serializers TIFF image serializer using ragg. See also: [ragg::agg_tiff()]
#' @export
serializer_agg_tiff <- function(..., type = "image/tiff") {
rlang::check_installed("ragg")
serializer_device(
type = type,
dev_on = function(filename) {
ragg::agg_tiff(filename, ...)
}
)
}

# svglite ----------------------------------------------------------------------

#' @describeIn serializers SVG image serializer using svglite. See also: [svglite::svglite()]
#' @export
serializer_svglite <- function(..., type = "image/svg+xml") {
rlang::check_installed("svglite")

serializer_device(
type = type,
dev_on = function(filename) {
svglite::svglite(filename, ...)
}
)
}


add_serializers_onLoad <- function() {
Expand Down Expand Up @@ -659,13 +706,17 @@ add_serializers_onLoad <- function() {
register_serializer("htmlwidget", serializer_htmlwidget)

# devices
register_serializer("device", serializer_device)
register_serializer("jpeg", serializer_jpeg)
register_serializer("png", serializer_png)
register_serializer("svg", serializer_svg)
register_serializer("bmp", serializer_bmp)
register_serializer("tiff", serializer_tiff)
register_serializer("pdf", serializer_pdf)
register_serializer("device", serializer_device)
register_serializer("jpeg", serializer_jpeg)
register_serializer("png", serializer_png)
register_serializer("svg", serializer_svg)
register_serializer("bmp", serializer_bmp)
register_serializer("tiff", serializer_tiff)
register_serializer("pdf", serializer_pdf)
register_serializer("agg_jpeg", serializer_agg_jpeg)
register_serializer("agg_png", serializer_agg_png)
register_serializer("agg_tiff", serializer_agg_tiff)
register_serializer("svglite", serializer_svglite)


## Do not register until implemented
Expand Down
20 changes: 20 additions & 0 deletions man/serializers.Rd

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

13 changes: 12 additions & 1 deletion tests/testthat/test-serializer-device.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ test_that("pdf produces an image", {
expect_device_output("pdf", "application/pdf", NULL)
})


test_that("agg_png produces an image", {
expect_device_output("agg_png", "image/png", NULL)
})
test_that("agg_jpeg produces an image", {
expect_device_output("agg_jpeg", "image/jpeg", NULL)
})
test_that("agg_tiff produces an image", {
expect_device_output("agg_tiff", "image/tiff", NULL)
})
test_that("svglite produces an image", {
expect_device_output("svglite", "image/svg+xml", NULL)
})


context("plumb() device serializer")
Expand Down
4 changes: 4 additions & 0 deletions vignettes/rendering-output.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Annotation | Content Type | Description/References
`@serializer bmp` | `image/bmp` | Images created with `bmp()`
`@serializer tiff` | `image/tiff` | Images created with `tiff()`
`@serializer pdf` | `application/pdf` | PDF File created with `pdf()`
`@serializer agg_jpeg` | `image/jpeg` | Images created with `ragg::agg_jpeg()`
`@serializer agg_png` | `image/png` | Images created with `ragg::agg_png()`
`@serializer agg_tiff` | `image/tiff` | Images created with `ragg::agg_tiff()`
`@serializer svglite` | `image/svg` | Images created with `svglite::svglite()`


### Boxed vs Unboxed JSON
Expand Down
Loading