简体   繁体   中英

R Shiny: mathematical symbols in plots

Mathematical annotations in plot labels are not rendered in shiny apps. Both with base graphics and ggplot2.

Here is a minimal example using both a plotmath expression and a UTF-8 character. Printing the plots interactively in R or saving through a graphics device such as png() renders the symbols correctly. However, in the shiny app below the symbols are simply not rendered.

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(4, plotOutput("figure1", height = "300px")), 
    column(4, plotOutput("figure2", height = "300px")), 
    column(4, plotOutput("figure3", height = "300px"))
  )
)

server <- function(input, output, session) {
  output$figure1 <- renderPlot(
    plot(1, xlab = "beta")
  )
  output$figure2 <- renderPlot(
    plot(1, xlab = expression(beta))
  )
  output$figure3 <- renderPlot(
    plot(1, xlab = "𝜃")
  )
}

runApp(shinyApp(ui, server))

闪亮不呈现数学符号

Session info:

R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 20.1

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8     LC_MONETARY=fr_FR.UTF-8   
 [6] LC_MESSAGES=en_GB.UTF-8    LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] shiny_1.6.0    devtools_2.4.1 usethis_2.0.1 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        compiler_4.1.0    bslib_0.2.5.1     later_1.2.0       jquerylib_0.1.4   prettyunits_1.1.1 remotes_2.3.0     tools_4.1.0      
 [9] testthat_3.0.2    digest_0.6.27     pkgbuild_1.2.0    pkgload_1.2.1     jsonlite_1.7.2    memoise_2.0.0     lifecycle_1.0.0   rlang_0.4.11     
[17] cli_2.5.0         parallel_4.1.0    fastmap_1.1.0     withr_2.4.2       desc_1.3.0        fs_1.5.0          sass_0.4.0        rprojroot_2.0.2  
[25] glue_1.4.2        R6_2.5.0          processx_3.5.2    bspm_0.3.7        sessioninfo_1.1.1 callr_3.7.0       purrr_0.3.4       magrittr_2.0.1   
[33] ps_1.6.0          promises_1.2.0.1  ellipsis_0.3.2    htmltools_0.5.1.1 mime_0.10         xtable_1.8-4      httpuv_1.6.1      cachem_1.0.5     
[41] crayon_1.4.1      Cairo_1.5-12.2   

The problem was solved by setting

options(shiny.usecairo = FALSE)

which disables graphical rendering by the Cairo package, if it is installed. See https://shiny.rstudio.com/reference/shiny/0.12.0/shiny-options.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM