0

I am trying to run the example shiny app (I added the options(encoding = 'UTF-8')):

options(encoding = 'UTF-8')

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Old Faithful Geyser Data"),
   
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

and the image has not encoded the names (titles).

enter image description here

UPDATE

I tried to use par(family ="Ubuntu Mono") in the R script and it works. But, it doesn't work in the shiny app (tried it inside render plot).

George
  • 5,808
  • 15
  • 83
  • 160
  • Does this happen only with Shiny output, or also in your regular graphics device if you just do `hist(faithful[, 2])` in your R session? – Mikko Marttila Mar 17 '22 at 13:16
  • @MikkoMarttila:Oh, yes! The same result also! – George Mar 17 '22 at 13:19
  • Ah in that case this looks like a similar issue: https://stackoverflow.com/q/59796585/4550695 – Mikko Marttila Mar 17 '22 at 13:27
  • @MikkoMarttila:If I use `par(family ="Ubuntu Mono")` in the regular session, it works. But not in shiny app. – George Mar 17 '22 at 14:11
  • Try setting that inside the `renderPlot()` function. `par()` needs to be called after the graphics device that renders the image has been opened. – Mikko Marttila Mar 17 '22 at 14:20
  • @MikkoMarttila: Nothing , happened.still the same – George Mar 17 '22 at 14:21
  • That's strange. Would you update your question to show what exactly you are trying? – Mikko Marttila Mar 17 '22 at 14:24
  • Sorry, I meant showing the code that you tried. If I put a `par(family = "serif")` first thing inside the expression in `renderPlot()`, the font in the output changes accordingly. I don't see why this would be any different. – Mikko Marttila Mar 17 '22 at 14:50
  • @MikkoMarttila:Hmm.as I can see, yes it works but I am trying with ggplot (inside render plot) which doesn't work. But, I am not sure that is a permanent solution. Typing in every plot call the `par` – George Mar 17 '22 at 14:56
  • Looks like you can also pass the option as a named argument to `renderPlot()`, so you could do `renderPlot({ ... }, family = "Ubuntu Mono")` and it should also work. That might be a bit more feasible. For a permanent solution I think you'd have to solve the font issue on your machine, which I have no idea how to do. – Mikko Marttila Mar 17 '22 at 15:01
  • @MikkoMarttila:Yes! That works! In shiny. In normal session , If I use ggplot , how ca I set it globally? Because with `par` it doesn't work. – George Mar 17 '22 at 15:07
  • Yeah, ggplot doesn't take font values from `par`. You'd need to add something like `theme(text = element_text(family = "Ubuntu Sans"))` to your plot. See more about changing fonts in ggplot here: https://stackoverflow.com/q/34522732/4550695 – Mikko Marttila Mar 17 '22 at 15:11
  • @MikkoMarttila:Basically, this `theme_set(theme_gray(base_size = 20, base_family = 'Font Name' ))` for globally – George Mar 17 '22 at 15:19
  • @MikkoMarttila:ok , thanks.make it an answer – George Mar 17 '22 at 15:19

1 Answers1

1

This looks like a font conflict. You could circumvent it by setting a custom font available on your system that does get rendered correctly.

For base graphics, use par(family = "serif") or renderPlot(..., family = "serif") in Shiny. For ggplot2 graphics, set theme(text = element_text(family = "serif")) for an individual plot, or use base_family in supported themes, e.g. theme_gray(base_family = "serif").

In all of the above, replace "serif" with the font you want to use. For example, you specifically mentioned "Ubuntu Mono" rendering correctly in your case.

Mikko Marttila
  • 10,972
  • 18
  • 31