R+plotly+Shiny:乳胶渲染的问题

R+plotly+Shiny: trouble with latex rendering

请看一下post末尾的代码。 这是一个简单的闪亮应用程序,它使用 plotly 来生成直方图。 我想使用一些乳胶语法来标记 x 轴,但出于某种原因我看到了 $n_1/(n_1+n_2)$ 而不是正确的乳胶。知道出了什么问题吗?

我尝试按照链接中的程序进行操作

https://plotly-r.com/mathjax.html

https://plotly.com/r/LaTeX/

但是有些东西不起作用。

感谢任何帮助!

library(shiny)
library(plotly)
library(tidyverse)

set.seed(1234)

x <- runif(2000)

df_frag_norm <- tibble(n1_norm = x)

ui <- fluidPage(plotlyOutput("myplot1"))

server <- function(input, output) {
  output$myplot1 <- renderPlotly({
    fig <- plot_ly(
        df_frag_norm,
        x = ~ n1_norm,
        ## y = ~decessi_giornalieri,
        type = 'histogram',
        xbins = list(size = 0.05)
      ) %>%
      layout(bargap = 0.1,
             xaxis = list(
               range = c(0, 1),
               title = TeX("n_1/(n_1+n_2)")
             ))
    
    fig <- fig %>% config(mathjax = 'cdn')
  })
  
}

shinyApp(ui, server)

这里是 sessionInfo()

的输出
R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 11 (bullseye)

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.13.so

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

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

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.8     purrr_0.3.4    
 [5] readr_2.1.2     tidyr_1.2.0     tibble_3.1.6    tidyverse_1.3.1
 [9] plotly_4.10.0   ggplot2_3.3.5   shiny_1.7.1    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8        lubridate_1.8.0   assertthat_0.2.1  digest_0.6.29    
 [5] utf8_1.2.2        mime_0.12         R6_2.5.1          cellranger_1.1.0 
 [9] backports_1.4.1   reprex_2.0.1      httr_1.4.2        pillar_1.7.0     
[13] rlang_1.0.2       lazyeval_0.2.2    readxl_1.3.1      rstudioapi_0.13  
[17] data.table_1.14.2 jquerylib_0.1.4   htmlwidgets_1.5.4 munsell_0.5.0    
[21] broom_0.7.12      compiler_4.1.3    httpuv_1.6.5      modelr_0.1.8     
[25] pkgconfig_2.0.3   htmltools_0.5.2   tidyselect_1.1.2  fansi_1.0.2      
[29] viridisLite_0.4.0 crayon_1.5.0      tzdb_0.2.0        dbplyr_2.1.1     
[33] withr_2.5.0       later_1.3.0       grid_4.1.3        jsonlite_1.8.0   
[37] xtable_1.8-4      gtable_0.3.0      lifecycle_1.0.1   DBI_1.1.2        
[41] magrittr_2.0.2    scales_1.1.1      cachem_1.0.6      cli_3.2.0        
[45] stringi_1.7.6     fs_1.5.2          promises_1.2.0.1  xml2_1.3.3       
[49] bslib_0.3.1       ellipsis_0.3.2    generics_0.1.2    vctrs_0.3.8      
[53] tools_4.1.3       glue_1.6.2        crosstalk_1.2.0   hms_1.1.1        
[57] fastmap_1.1.0     yaml_2.3.5        colorspace_2.0-3  rvest_1.0.2      
[61] haven_2.4.3       sass_0.4.0       

plotly::config()

的文档中所述

IMPORTANT: plotly uses SVG-based mathjax rendering which doesn't play nicely with HTML-based rendering (e.g., rmarkdown documents and shiny apps).

您可以在 fluidPage 中添加 withMathJax() 来完成这项工作。请注意,在这种情况下您不需要 config(mathjax = 'cdn')

代码:

library(shiny)
library(plotly)
library(tidyverse)

set.seed(1234)

x <- runif(2000)

df_frag_norm <- tibble(n1_norm = x)

ui <- fluidPage(
  withMathJax(),
  plotlyOutput("myplot1")
)

server <- function(input, output) {
  output$myplot1 <- renderPlotly({
    fig <- plot_ly(
        df_frag_norm,
        x = ~ n1_norm,
        ## y = ~decessi_giornalieri,
        type = 'histogram',
        xbins = list(size = 0.05)
      ) %>%
      layout(bargap = 0.1,
             xaxis = list(
               range = c(0, 1),
               title = TeX("n_1/(n_1+n_2)")
             ))
  })
  
}

shinyApp(ui, server)