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

RStudio 1.4 support? #31

Closed
cpsievert opened this issue Apr 17, 2020 · 11 comments
Closed

RStudio 1.4 support? #31

cpsievert opened this issue Apr 17, 2020 · 11 comments

Comments

@cpsievert
Copy link
Contributor

@cpsievert cpsievert commented Apr 17, 2020

Just FYI, RStudio 1.4 comes with the capability of customizing the graphics backend. Perhaps showtext could leverage that new ability to render fonts in the RStudio graphics device?

@yixuan
Copy link
Owner

@yixuan yixuan commented Apr 17, 2020

Thanks for sharing the news. This looks like an interesting update. I'll try the new RStudio when it is released.

@cpsievert
Copy link
Contributor Author

@cpsievert cpsievert commented Apr 17, 2020

Sounds good, thanks for considering. In case you didn't know, RStudio 1. 4 is currently available in the daily builds https://dailies.rstudio.com/

@yixuan
Copy link
Owner

@yixuan yixuan commented Apr 20, 2020

Good to know that. I'll have a try when I get some time for this.

@yixuan
Copy link
Owner

@yixuan yixuan commented May 2, 2020

Tracing back to rstudio/rstudio#2142, I accidentally found a workaround for the issue with RStudio 1.3 or earlier. Basically you just need to run the following code at the beginning of each R session:

trace(grDevices::png, exit = quote({
    showtext::showtext_begin()
}), print = FALSE)

And then run any showtext code in RStudio:

library(showtext)
font_add_google("Lobster", "lobster")
showtext_auto()

plot(1, pch = 16, cex = 3)
text(1, 1.1, "A fancy dot", family = "lobster", col = "steelblue", cex = 3)

Can you test if it works for you?

@cpsievert
Copy link
Contributor Author

@cpsievert cpsievert commented May 2, 2020

Nice, can confirm this works on RStudio Preview (1.3.952). I did notice, though, that exporting to pdf via the export dropdown doesn't appear to work

@yixuan
Copy link
Owner

@yixuan yixuan commented May 2, 2020

Then you just need to add another tracer:

trace(grDevices::pdf, exit = quote({
    showtext::showtext_begin()
}), print = FALSE)
@cpsievert
Copy link
Contributor Author

@cpsievert cpsievert commented May 4, 2020

Interestingly it appears that if the RStudioGD is already open, this trace() workaround doesn't work, for example, if you first plot

plot(1)

then trace()

trace(grDevices::png, exit = quote({
    showtext::showtext_begin()
}), print = FALSE)

This no longer works

library(showtext)
font_add_google("Lobster", "lobster")
showtext_auto()

plot(1, pch = 16, cex = 3)
text(1, 1.1, "A fancy dot", family = "lobster", col = "steelblue", cex = 3)
@yixuan
Copy link
Owner

@yixuan yixuan commented May 4, 2020

The thing is, RStudio will open a new PNG device after it plots, so the device might be already open before trace() is called. You can first create a fake plot to force RStudio open a new device:

plot(1)
trace(grDevices::png, exit = quote({
    showtext::showtext_begin()
}), print = FALSE)
# Force the new device to apply tracer
plot.new()

library(showtext)
font_add_google("Lobster", "lobster")
showtext_auto()

plot(1, pch = 16, cex = 3)
text(1, 1.1, "A fancy dot", family = "lobster", col = "steelblue", cex = 3)
@cpsievert
Copy link
Contributor Author

@cpsievert cpsievert commented May 4, 2020

Oh, I see, so RStudio must close and open a new device on every plot.new or grid.newpage? Is that your understanding of it?

@yixuan
Copy link
Owner

@yixuan yixuan commented May 4, 2020

This is my guess, but may be wrong. RStudioGD seems to have some sophisticated operations at the backend. 🤔

Something that may be useful for checking is to set print = TRUE in trace():

trace(grDevices::png, exit = quote({
    cat("device opened\n")
}), print = TRUE)

plot(1)
plot(1)

In one of my sessions, the result is

> plot(1)
Tracing grDevices:::png("/tmp/RtmpmX3pAy/b64514f1d9c54eacb9e9152db63e5b4c.png",  .... on exit 
device opened
Tracing grDevices:::png("/tmp/RtmpmX3pAy/b64514f1d9c54eacb9e9152db63e5b4c.png",  .... on exit 
device opened
> plot(1)
Tracing grDevices:::png("/tmp/RtmpmX3pAy/72a6cf6868774d568d7abc6dececba9d.png",  .... on exit 
device opened

I check the last PNG file and it is 0 byte, so it should be used for future plots.

@yixuan
Copy link
Owner

@yixuan yixuan commented Aug 22, 2020

As the new version is already on CRAN, I guess we can close this issue first.

@yixuan yixuan closed this Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.