R有光泽的服务器不能渲染正确的ggplot字体系列 [英] R Shiny server not rendering correct ggplot font family

查看:178
本文介绍了R有光泽的服务器不能渲染正确的ggplot字体系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个漂亮的字体应用到在Shiny应用中呈现的ggplot。

使用family =[fontname]在RStudio(在同一台服务器上)中设置所需字体可正常工作。这里提供了一个衬线字体字体系列:



在rstudio中正确显示ggplot字体系列的图像

然而,当ggplot被嵌入到Shiny renderPlot({})函数中时,字体系列不会从默认值更改。这里已经请求了相同的serif字体系列:

在Shiny应用程序中不正确的ggplot字体系列渲染图像



字体大小和字体(粗体,斜体)的更改按预期工作。我已经使用RStudio中的fonts()和pdfFonts()以及闪亮的应用程序检查了安装的字体名称,然后尝试列出的以及serif,sans和mono,都无济于事。我也尝试了loadfonts()。



一个简单的例子:

server.R

  require(ggplot2)
require(ggthemes)
require(extrafont)

shinyServer(函数(输入,输出){
df< - data.frame(a = rnorm(100),b = rnorm(100))

输出$ the_plot< - renderPlot
p <-ggplot(df,aes(x = a,y = b),environment = environment())+
xlab(Alpha)+
ylab(Beta) +
geom_point()+
主题(text = element_text(family =serif,size = 16))

print(p)
})




$ b $ u $ R

$ $
$ sidebarLayout(
sidebarPanel(
h6(Font test)$)$ $ $ $ $ $ $ $ $
$闪亮UI b $ b),

mainPanel(
plotOutput(the_plot)


))

编辑: 一个类似的未解答的问题,但是寻求PDF而不是PNG输出。现在已经尝试了R基本图形而不是ggplot,结果相同。

解决方案

作为一种解决方法,我重新创建了大部分renderPlot()函数使用renderImage(),如这篇闪亮的教程文章中所述。令人高兴的是,这使得抗锯齿字体变得丰富多彩。希望这对其他人有用。



ui.R修正为

  mainPanel(
imageOutput(myImage)

服务器.R

  shinyServer(函数(输入,输出,会话){

#A动态调整大小plot
output $ myImage< - renderImage({
#读取myImage的宽度和高度,这些是反应值,所以这个
#表达式会在它们改变时重新运行
width< - session $ clientData $ output_myImage_width
height< - session $ clientData $ output_myImage_height

#对于高分辨率显示器,这将大于1
pixelratio< - session $ clientData $ pixelratio

#一个用于保存输出的临时文件
outfile< - tempfile(fileext ='。png')

#生成图像文件
png(outfile,width = width * pixelratio,height = height * pixelratio,
res = 72 * pixelratio)
pl ot(rnorm(100),rnorm(100),family =serif)
dev.off()

#返回一个包含文件名的列表
list(src = outfile,
width = width,
height = height,
alt =这是替代文本)
},deleteFile = TRUE)#完成时删除临时文件

})


I'm trying to apply a nice font to a ggplot rendered in a Shiny app.

Setting the desired font in RStudio (on the same server) using family="[fontname]" works correctly. Here a "serif" font family has been requested:

Image of correct ggplot font family rendering in rstudio

However, when ggplot is then embedded in the Shiny renderPlot({}) function, the font family doesn't change from the default. Here the same "serif" font family has been requested:

Image of incorrect ggplot font family rendering in Shiny app

Changes to the font size and font face (bold, italic) work as expected. I've checked the installed font names using fonts() and pdfFonts() within RStudio and within the shiny app, and then tried those listed as well as "serif", "sans", and "mono", all to no avail. I've also tried loadfonts().

A minimal example:

server.R

require(ggplot2)
require(ggthemes)
require(extrafont)

shinyServer(function(input, output) {
  df <- data.frame(a=rnorm(100), b=rnorm(100))

  output$the_plot <- renderPlot({
    p <- ggplot(df, aes(x=a, y=b), environment=environment()) + 
      xlab("Alpha") + 
      ylab("Beta") +
      geom_point() +
      theme(text=element_text(family="serif", size=16))

    print(p)
  })
})

ui.R

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      h6("Font test")
    ),

    mainPanel(
      plotOutput("the_plot")
    )
  )
))

Edit: There is a similar unanswered question but seeking pdf rather than png output. Have now also tried R base graphics instead of ggplot, with the same result.

解决方案

As a workaround, I recreated much of the renderPlot() functionality using renderImage(), as described in this Shiny tutorial article. Happily this renders antialiased fonts galore. Hope this is of use to someone else.

ui.R amended to

    mainPanel(
      imageOutput("myImage")
    )

server.R

shinyServer(function(input, output, session) {

  # A dynamically-sized plot
  output$myImage <- renderImage({
    # Read myImage's width and height. These are reactive values, so this
    # expression will re-run whenever they change.
    width  <- session$clientData$output_myImage_width
    height <- session$clientData$output_myImage_height

    # For high-res displays, this will be greater than 1
    pixelratio <- session$clientData$pixelratio

    # A temp file to save the output.
    outfile <- tempfile(fileext='.png')

    # Generate the image file
    png(outfile, width=width*pixelratio, height=height*pixelratio,
        res=72*pixelratio)
    plot(rnorm(100), rnorm(100), family="serif")
    dev.off()

    # Return a list containing the filename
    list(src = outfile,
         width = width,
         height = height,
         alt = "This is alternate text")
  }, deleteFile = TRUE) # delete the temp file when finished

})

这篇关于R有光泽的服务器不能渲染正确的ggplot字体系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆