gpt4 book ai didi

r - 带有自定义字体的ggplot在shinyapps.io上没有正确显示

转载 作者:行者123 更新时间:2023-12-03 23:52:45 28 4
gpt4 key购买 nike

我可以在 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"))

这呈现为:

enter image description here

有关该主题的更多信息,例如 herehere

我还可以将该绘图和 extrafont 集成到一个 Shiny 的应用程序中,该应用程序在本地运行,如下所示:
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 时,出现错误:

The application failed to start (exited with code 1).

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



我试图通过结合来自 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)

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

enter image description here

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

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

最佳答案

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

开始了:

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

    情节如下:

    enter image description here

    关于r - 带有自定义字体的ggplot在shinyapps.io上没有正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55100069/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com