-
Notifications
You must be signed in to change notification settings - Fork 295
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
Output plots do not support font family + weird default font #604
Comments
it doesn’t look like it should. look at the second pic, the fonts are still pixelated. the only difference is that the second one seems to have higher DPI, and therefore a bigger pixel font was used. I assume there’s something going on with your fontconfig, and the PNG device seems to select the wrong font to draw. try |
Hmm, does it look correct when using |
If both are using For instance,
Doesn't work in the notebook (no font change) but works when I run it inside R. |
Things are different because in IRkernel, we need to record and replay the plots. (We create multiple plot formats, and therefore have to replay the plots multiple times). It was a lot of work to find the perfect recording device: IRkernel/R/environment_shadow.r Lines 109 to 125 in e38f34a
But yes, this is of course a source of differences. |
So basically if the |
Okay, so here's what I did. Clearly, there is something off that I am not able to locate. :/ In R terminal:
In Jupyter Notebook with Kernel to the same R:
|
Not exactly, the recording device also makes a difference. The You could try: IRkernel:::init_null_device()
# replicate how IRkernel records plots
dev.new()
# plot call goes here
p <- recordPlot() # I think this goes after the plot call, not sure though
dev.off()
# replicate what repr_*.recordedplot does
png(...) # or your device of choice
replayPlot(p)
dev.off() this should reproduce what happens… |
I also experience this issue. Could it be that the option |
Well, try setting it! I don’t have any problems, so I need all the help you can give if you want me to fix it. |
i was having this same issue. for me, i believe it was related to having r-cairo package, which i had installed via conda. i rolled back my conda environment to before this installation and the plots are all crisp again. |
I have exactly this issue. @raivivek I see that you closed the ticket recently, were you able to come up with a solution? For completeness, let me just show what I get in various cases: Plain R
R, but trying to mimic IRkernel's plotting method, according to @flying-sheep 's instructions
R, but trying to mimic IRkernel's plotting method, according to @flying-sheep 's instructions AND setting
Jupyter Notebook
What we see is that
@flying-sheep Do you have any idea what's going on here? Do you see any reason why the IRkernel in the Jupyter environment behaves different from the replication instructions you gave? Is it possible that - somehow - despite my explicit setting of the |
Btw, @flying-sheep I'm not sure what rules you have for issues: I replied to a closed issue since it really seems like the same, so you might want to keep everything together. But I'm happy to open a new one (and reference this issue as related) if you prefer that. |
I could find this: https://stackoverflow.com/a/17955000/247482 Tbh, the whole situation is pretty messy, and a) many people having wrongly compiled R installations and b) the huge amount of config options influencing it is just too much to handle. If there’s any code I can use to detect and fix this from IRkernel, I’ll add it, but idk how. |
Thanks for having a look! I understand your difficulty in supporting the wild variety of R installations (and optional configurations that R supports). I'm pretty sure though that my R installation is 'sane' enough, since there I can create high-quality plots in every scenario. Seems that stackoverflow ticket suggests that if
That works fine, the Anyway, what I'm really looking for is what the difference could be between running
in plain R, or running
in a Jupyter Notebook, since your earlier comments seemed to suggest these should do the same. I've checked a bit the IRkernel code to see if I could discover more differences, and I see that the My rationale is: if I can find the function that causes the text to go bad, I may be able to understand how it can be fixed or circumvented. |
@casparvl I abandoned my efforts to get this to work. If the issue continues to persist for you, maybe I can reopen the issue. Otherwise, you are also welcome to create a new issue and continue the discussion there. Let me know what you prefer. |
I think the problem might be that jupyter sets up the capturing device before the I assume running the # setup code
IRkernel:::init_null_device()
# first cell
dev.new()
options(bitmapType='cairo')
dev.off()
# second cell, now newly created device has that option set
dev.new()
ggplot(iris) + geom_point(aes(Sepal.Width, Petal.Width)) + cowplot::theme_cowplot(font_family='DejaVu Serif')
p <- recordPlot()
dev.off()
# since a plot was created in the cell, it now gets replayed
png('test3_irkernel.png')
replayPlot(p)
dev.off() Regarding bitmapType:
I don’t know why on your system it doesn’t default to |
Well, not setting it is not an option: the default device it picks made me hit this issue, so I have to set it in order to even get a working kernel on my (headless) node. But: I actually made progress! I managed to get a plot with nice looking axes labels, in my Jupyter IRkernel! What I do:
And I finally get an image with nice looking axis labels: I came up with the idea after reading the following in the Cairo docs:
and figured: that should work in this context, even if IRkernel already has a device initialized. If I change Though the above gives me a workaround, I don't (yet) understand is why in my normal R session it did respect Since that is a direct call to Anyway, maybe this analysis puts you on track for finding the actual bug in |
Yeah, only vector devices use the font family: If you check the docs for To fix this we need to understand the differences. I assume that global ggplot and base R ( I initially decided for Cairo because the default rasterizer (at least on linux) looks crappy and even the dots it draws are wonky non-circles. But of course the lack of proper font support makes a reevaluation necessary 🤔 The R Markdown Cookbook gives a list of possible knitr devices, one of them being |
Can you try the Looks like mixing and matching fonts is no problem: Looks a bit more blurry that Cairo, but OK. Might even look better on a high DPI display: |
Hm, silly question maybe: how do I install an R package from a certain git branch? (I normally use install.packages(), but that just pulls from CRAN) I would like to give it a try (though it may take me a few days to find the time). Actually, instead of changing the backend, I was thinking if this function https://github.com/IRkernel/repr/blob/master/R/repr_recordedplot.r#L80 shouldn't just be altered in order to get the
The only challenge here is to translate the
I did a bit of digging: you could do this with the
Clearly, I'd grab this in a function Maybe I'm missing something here, but wouldn't this be a solution? I don't think setting the CairoFonts should hurt anybody and I do personally think the quality of the Cairo plotting device is one of the best. |
devtools::install_github('https://github.com/IRkernel/repr.git', ref = 'ragg')
Definitely. I think we should do that! I also think that going for Maybe we should add a |
@flying-sheep Sorry for going dead silent for so long. As these things go, I had my suggested solution for setting the fonts on the Cairo device on my 'TODO' list for a very long time, until I ran into this issue again and finally made a PR for it... Note that my PR solves a slightly different problem that you PR on using In any case, I'd say the PRs are orthogonal: we can add mine, and still add support for |
Hi,
It appears that my IRKernel setup is not working as expected. The plot appears to be rough and changing the fonts doesn't work as expected. See below.
However, after using the function from #559, the plot shows up as it should along with font changes as required which tells me that underlying R is working as expected. I am not sure what could be wrong. Shouldn't the first plot look like the second one anyway?
The text was updated successfully, but these errors were encountered: