How to use different fonts in an R Shiny plot

人走茶凉 提交于 2020-01-15 09:07:01

问题


I would like to create a plot in Shiny that the user can then download as a pdf using a custom font specified as a user input.

To be specific, I would like to use a pdf function such as pdf("plot.pdf", width = 5, height = 5, family = font.family), where the value of font.family is specified by the user.

Here is a simple example below: If I run the example on my machine, it works fine. However, when it is hosted on the RStudio shiny servers, I receive an error saying that the specified font family cannot be found. I think the problem is that the fonts I want are not accessible on the RStudio shiny servers, but is there a way I can include them?

server.R

shinyServer(function(input, output) {

output$distPlot <- renderPlot({

plot(1, xlim = c(0, 1), ylim = c(0, 1))
text(.5, .5, "Custom Font!!"
})

output$downloadPlot <- downloadHandler(

  filename = function() {paste('CustomFont.pdf')}, 
  content = function(file){

font.family <- input$font.family

pdf(file, width = 11, height= 8.5, family = font.family)

plot(1, xlim = c(0, 1), ylim = c(0, 1))
text(.5, .5, fonts(), cex = 10)


dev.off()
}, contentType = "image/pdf"
)
})

ui.R

 shinyUI(fluidPage(

 sidebarLayout(
 sidebarPanel(
  selectInput("font.family", "Choose Font", 
              choices = c("Helvetica Neue", "Times New Roman", "Arial")
  ),
  downloadButton("downloadPlot", "Download Plot as PDF")
),

# Show a plot of the plot
mainPanel(
  plotOutput("distPlot", width = "800px", height = "800px")
  ))))

回答1:


I had a similar problem. To solve that, much of the renderPlot() functionality was recreated using renderImage(), as described in this Shiny tutorial article. Font rendering then worked as desired.

This is the code which solved that question; it might also solve this one.

ui.R amend 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/28034091/how-to-use-different-fonts-in-an-r-shiny-plot

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