-
Notifications
You must be signed in to change notification settings - Fork 261
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
png
serializer with dynamic image size
#837
Comments
I did an example over stackoverflow https://stackoverflow.com/questions/65098103/plumber-r-render-a-svg-file/65101289#65101289 library(plumber)
device_size <- function() {
h_ <- 7
w_ <- 7
list(
h = function() h_,
w = function() w_,
set_h = function(h) if (!is.null(h)) {h_ <<- as.numeric(h)},
set_w = function(w) if (!is.null(w)) {w_ <<- as.numeric(w)}
)
}
output_size <- device_size()
serializer_dynamic_svg <- function(..., type = "image/svg+xml") {
serializer_device(
type = type,
dev_on = function(filename) {
grDevices::svg(filename,
width = output_size$w(),
height = output_size$h())
}
)
}
register_serializer("svg", serializer_dynamic_svg)
#* @filter dynamic_size
function(req) {
if (req$PATH_INFO == "/plot") {
output_size$set_w(req$args$width)
output_size$set_h(req$args$height)
}
plumber::forward()
} Will revisit |
This is a great solution given the tools available, @meztez ! |
@logstar Dynamic sizes is a difficult thing to implement as
I believe so! Minor adjustments:
dyn_param_png_serializer <- function(plot_func, width, height, res) {
tmpfn <- base::tempfile()
base::on.exit({
if (file.exists(tmpfn)) {
unlink(tmpfn)
}
}, add = TRUE)
grDevices::png(tmpfn, width = width, height = height, res = res)
device_id <- grDevices::dev.cur()
on.exit({
grDevices::dev.off(device_id)
}, add = TRUE)
plot_func()
img <- base::readBin(tmpfn, "raw", n = base::file.info(tmpfn)$size)
return(img)
}
I would like to make this experience better. I don't know if it will be this function directly or through something similar to @meztez 's
Check out https://adv-r.hadley.nz/functions.html?q=lexi#lexical-scoping . The whole book is a great resource for learning the nitty gritty details about R! Here is a good example from https://prl.ccs.neu.edu/blog/2019/09/10/scoping-in-r/: x <- 1
f <- function() {
x
}
g <- function() {
x <- 2
f()
}
g() # What does this return? |
Hi @meztez , Thanks for the tips to overwrite the 'png'. I just have one question for the here is my code:
|
I'd love to see this (or the filter version) inside the library. Anything I (or others) can assist with? In addition to the ideas above, I'm wondering about whether to make the content-type itself dynamic - switching between jpeg, png and svg on client demand |
This provides a way forward without adding new serializer methods...
serializer_png <- function(..., type = "image/png") {
doc_args <- list(...)
serializer_device(
type = type,
dev_on = function(filename, ..., req, res) {
# Overwrite args that can be dynamic (Possibly more args exist)
doc_args$width <- req$args$width %||% doc_args$width
doc_args$height <- req$args$height %||% doc_args$height
# Open device
rlang::exec(grDevices::png, filename, !!!doc_args)
}
)
}
Thank you! |
Start of a PR is in.... there is lots of testing and documentation needed if anyone else wants to contribute 👍 Going to need to get the other PRs #889 #891 #892 out of the way before this one can be merged - as there will be merge conflicts around the endpoint, parameter and open api code... Small nudge: could do with RStudio making decisions on these PRs. Delays will cause extra work ... my memory is fading... making changes gets harder every day :) |
PR Redone with personal email... and might have to do it again yet... I have too many email addresses ... |
-- https://www.rplumber.io/articles/rendering-output.html#customizing-image-serializers viewed on 10/18/2021
According to the above documentation, a
png
serializer with dynamic image sizes can be implemented with the currentplumber
framework. This issue is a feature request for creating a friendly and stable interface for users to directly usepng
serializer with dynamic image sizes.I attempted to implement a
png
serializer with dynamic image sizes,dyn_param_png_serializer
, which is listed below. However, isdyn_param_png_serializer
properly implemented? Asplumber::serializer_device
apparently also supports async execution, willdyn_param_png_serializer
cause async errors? Willdyn_param_png_serializer
likely be supported by futureplumber
versions?Additionally, expressions inside
dyn_param_png_serializer
cannot directly access variables in/plot
endpoint function, e.g.image_width
andmyData
, but why the passedmy_plot_func
can be called directly withindyn_param_png_serializer
? Is there any pointers on the scope of variables in the context ofplumber
endpoint function definition, registration, runtime, etc?This example is adapted from https://www.rplumber.io/articles/introduction.html and
plumber/R/serializer.R
Lines 473 to 535 in 06e46f3
The text was updated successfully, but these errors were encountered: