【问题标题】:ggplot with customized font not showing properly on shinyapps.io带有自定义字体的 ggplot 在 shinyapps.io 上未正确显示
【发布时间】:2019-08-01 15:52:40
【问题描述】:

我可以自定义 ggplot 中的字体:

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"))

这呈现为:

关于这个主题的更多信息,例如herehere


我还可以将该绘图和额外字体集成到一个闪亮的应用程序中,该应用程序在本地运行如下:

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 时,出现错误:

应用程序无法启动(以代码 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 奥芬。地面事件。 'Permission denied' Fehler in value[3L] : kann Verbindung nicht öffnen Ruft auf: local ... tryCatch -> tryCatchList -> tryCatchOne -> Ausführung angelhalten


我试图通过结合here 的答案来解决这个问题。我将.ttf 文件添加到www 目录,并将extrafontdb 包源添加到r-lib 目录。 (当然,我同时部署了两者..)。

完整的app.R 文件现在看起来像:

.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)

当我部署它时,我会得到一个正在运行的应用程序和以下输出:

现在奇怪的是,renderText(print(fonts())) 打印 Comic Sans MS。所以它似乎我的字体被加载了。但是情节没有显示正确的字体。

这是为什么呢?我该如何解决?

【问题讨论】:

    标签: r ggplot2 shiny extrafont


    【解决方案1】:

    我找到了一个似乎适用于shinyapps.io 的解决方案(但不适用于本地,因为它是一个仅限 Linux 的解决方案。不知何故,它不适用于我原来的“ComicSans MS”字体,但该字体是反正不漂亮.. ;-))

    我们开始吧:

    1. 将自定义字体放在www 目录中:例如IndieFlower.ttf 来自here
    2. 按照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)
    

    剧情如下:

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 2019-07-15
      • 2019-01-19
      • 2011-10-08
      • 2011-07-21
      • 2011-07-21
      • 2012-03-22
      • 2013-12-05
      • 2019-06-06
      相关资源
      最近更新 更多