如何在R Shiny图中使用不同的字体 [英] How to use different fonts in an R Shiny plot

查看:415
本文介绍了如何在R Shiny图中使用不同的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Shiny中创建一个图,然后用户可以使用指定为用户输入的自定义字体将其下载为pdf.

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.

具体来说,我想使用pdf函数,例如pdf("plot.pdf", width = 5, height = 5, family = font.family),其中font.family的值由用户指定.

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.

这是下面的一个简单示例:如果我在计算机上运行该示例,则可以正常工作.但是,将其托管在RStudio闪亮服务器上时,我收到一条错误消息,指出找不到指定的字体系列.我认为问题是我想要的字体无法在RStudio闪亮的服务器上访问,但是有没有办法我可以将它们包括在内?

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")
  ))))

推荐答案

我有 this闪亮的教程文章.字体渲染然后按预期工作.

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修改为

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 Shiny图中使用不同的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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