R Shiny server not rendering correct ggplot font family

末鹿安然 提交于 2019-12-07 01:52:26

问题


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.


回答1:


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

})


来源:https://stackoverflow.com/questions/31859911/r-shiny-server-not-rendering-correct-ggplot-font-family

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!