具有自定义字体的ggplot无法在shinyapps.io上正确显示 [英] ggplot with customized font not showing properly on shinyapps.io
问题描述
我可以使用以下方法在ggplot
中自定义字体:
I can customize the font in a ggplot
with:
library(extrafont)
windowsFonts()
font_import(pattern = "comic", prompt = FALSE)
loadfonts(device = "win")
windowsFonts()
ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size=16, family="Comic Sans MS"))
这呈现为:
有关此主题的更多信息,例如此处和我还可以将该图和Extrafont集成到一个在本地运行的闪亮应用程序中,如下所示:
I can also integrate that plot and extrafont into a shiny app which runs locally as follows:
library(ggplot2)
library(extrafont)
library(shiny)
font_import(paths = "www", pattern = "comic", prompt = FALSE)
loadfonts()
print(fonts())
ui <- fluidPage(plotOutput("plot"),textOutput("fonts"))
server <- function(input, output) {
output$plot <- renderPlot({
ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size=16, family="Comic Sans MS"))
})
output$fonts <- renderText(print(fonts()))
}
shinyApp(ui = ui, server = server)
但是,当我尝试将其部署到Shinyapps.io时,出现错误:
However, when I try to deploy this to shinyapps.io I get an error:
应用程序无法启动(以代码1退出).
The application failed to start (exited with code 1).
用R注册字体,在www中扫描ttf文件...提取.afm .ttf文件中的文件... /srv/connect/apps/21-comic-font/www/comici.ttfWarnung in gzfile(dest, "w")kann komprimierte Datei '/opt/R/3.4.3/lib/R/library/extrafontdb/metrics/comici.afm.gz'nicht öffnen. Grundevtl. 权限被拒绝" Fehler的值[3L]: kann Verbindung nichtöffnenRuft auf:本地... tryCatch-> tryCatchList-> tryCatchOne->Ausführungangehalten
Registering fonts with R Scanning ttf files in www ... Extracting .afm files from .ttf files... /srv/connect/apps/21-comic-font/www/comici.ttfWarnung in gzfile(dest, "w") kann komprimierte Datei '/opt/R/3.4.3/lib/R/library/extrafontdb/metrics/comici.afm.gz' nicht öffnen. Grund evtl. 'Permission denied' Fehler in value[3L] : kann Verbindung nicht öffnen Ruft auf: local ... tryCatch -> tryCatchList -> tryCatchOne -> Ausführung angehalten
我试图通过结合此处的答案来解决此问题.我将我的.ttf
文件添加到了www
目录中,并且将extrafontdb
包源添加到了r-lib
目录中. (当然,我两个都部署了.)
I tried to solve this by incorporating the answer from here. I added my .ttf
files to the www
directory and the extrafontdb
package source to the r-lib
directory. (And of course I deployed both..).
完整的app.R
文件现在如下所示:
The complete app.R
file now looks like:
.libPaths(c('r-lib', .libPaths()))
install.packages('r-lib/extrafontdb_1.0.tar.gz',type = 'source',repos = NULL)
library(ggplot2)
library(extrafontdb)
library(extrafont)
library(shiny)
font_import(paths = "www", pattern = "comic", prompt = FALSE)
loadfonts()
print(fonts())
ui <- fluidPage(plotOutput("plot"),textOutput("fonts"))
server <- function(input, output) {
output$plot <- renderPlot({
ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size=16, family="Comic Sans MS"))
})
output$fonts <- renderText(print(fonts()))
}
shinyApp(ui = ui, server = server)
当我部署它时,我得到一个正在运行的应用程序和以下输出:
When I deploy this I get a running app and the following output:
现在奇怪的是,renderText(print(fonts()))
打印 Comic Sans MS .因此似乎我的字体已加载.但是该图未显示正确的字体.
Now the strange thing is, that renderText(print(fonts()))
prints Comic Sans MS. So it seems that my font got loaded. But the plot does not show the proper font.
那是为什么?而我该如何解决呢?
Why is that? And how can I solve it?
推荐答案
我发现了一个似乎可以在shinyapps.io
上使用的解决方案(但不适用于本地,因为它是仅Linux的解决方案.可以使用我原来的'ComicSans MS'字体,但该字体还是不漂亮..;-))
I found a solution that seems to work on shinyapps.io
(but not locally, since it is a Linux only solution. And somehow it did not work with my original 'ComicSans MS' font, but that font is not beautiful anyway.. ;-))
我们在这里:
- 将自定义字体放在
www
目录中:例如IndieFlower.ttf
来自此处 - 按照
此处
- Place custom font in
www
directory: e.g.IndieFlower.ttf
from here - Follow the steps from here
这将导致以下app.R
文件:
ibrary(ggplot2)
library(shiny)
dir.create('~/.fonts')
file.copy("www/IndieFlower.ttf", "~/.fonts")
system('fc-cache -f ~/.fonts')
ui <- fluidPage(plotOutput("plot"))
server <- function(input, output) {
output$plot <- renderPlot({
ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size = 16, family = "IndieFlower"))
})
}
shinyApp(ui = ui, server = server)
情节如下:
这篇关于具有自定义字体的ggplot无法在shinyapps.io上正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!