R Shiny server not rendering correct ggplot font family

后端 未结 1 1883
栀梦
栀梦 2021-01-12 11:01

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 cor

相关标签:
1条回答
  • 2021-01-12 12:01

    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
    
    })
    
    0 讨论(0)
提交回复
热议问题