thebioengineer / camcorder

Record plots generated during an R session and replay as a gif!
https://thebioengineer.github.io/camcorder/
Other
201 stars 7 forks source link

Problem with systemfonts (?) #17

Closed z3tt closed 2 years ago

z3tt commented 3 years ago

Hey @thebioengineer, I found a strange issue when using the {systemfonts} package in combination with gg_record() and pdf output. Haven't used the package for a while but I am pretty sure we did use ragg/systemfonts back then as well. Do I miss something? Is there actually a way to specify the device, e.g. cairo_pdf?

Here is a reprex:

options(warn = -1)

library(ggplot2)

g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()

theme_set(theme_minimal(base_family = "Roboto"))
g

## with camcorder loaded (works)
library(camcorder)

g

## with PNG recording turned on (works)
gg_record(dir = file.path(tempdir(),"recording"), device = "png", width = 15, height = 10)

g

## with PDF recording turned on (does not work)
gg_record(dir = file.path(tempdir(),"recording"), device = "pdf", width = 15, height = 10)
g
#> Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type

## the problem is related to text elements only - theme_void works
theme_set(theme_void(base_family = "Bangers"))
g

Created on 2021-05-06 by the reprex package (v1.0.0)

z3tt commented 3 years ago

Any chance to investigate/solve that any time soon? I would love to use {camcorder} more regularly but in its current state, it's not useful at all for me. I would be happy to help @thebioengineer (if you tell me how :D)

thebioengineer commented 3 years ago

Hey @z3tt - This is a problem with ggsave(), not gg_record() based on my testing:

> options(warn = -1)
> 
> library(ggplot2)
> 
> g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
> 
> theme_set(theme_minimal(base_family = "Roboto"))
> g
> ggsave("test.png", g, device = "png")
Saving 8.53 x 5.33 in image
> ggsave("test.png", g, device = "pdf")
Saving 8.53 x 5.33 in image
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
z3tt commented 3 years ago

Interesting, I didn't know! However, this works for me:

ggsave("test.pdf", g, device = cairo_pdf)

At least for Windows, it would be great to have cairo_pdf as the default when saving to pdf. That would also fix the issue.

thebioengineer commented 3 years ago

I have updated in PR #18 to accept functions, so cairo_pdf is now an acceptable argument - I am hesitant to change from the "default" behavior of ggsave, as my thought is gg_record is an analogue to simply record on an ongoing basis.

z3tt commented 3 years ago

Hi @thebioengineer, I have updated the branches and the package. However, when using a device function as argument for device, e.g. device = ragg::agg_png(), it throws an error that the argument does not match. I think it is due to the fact that is.function() expects the function without parentheses, i.e. is.function(ragg::agg_png()) returns FALSE and is.function(ragg::agg_png) returns TRUE. I am not sure what the correct way if I am using it incorrectly or if this is something we need to fix, so I'd appreciate any feedback.

Edit: btw, same is true when I try to use cairo_pdf.

thebioengineer commented 3 years ago

Thanks for pointing that out!

z3tt commented 2 years ago

Works now thanks to #25 when passed as device = cairo_pdf (without quotation marks as in ggsave())