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
})
来源:https://stackoverflow.com/questions/31859911/r-shiny-server-not-rendering-correct-ggplot-font-family