Shiny同页面渲染highchartOutput与wordcloud2Output时Highchart不显示问题
问题原因
该问题是wordcloud2与highcharter两个R包的前端JavaScript依赖冲突导致。wordcloud2组件渲染时会自动加载一份自带的旧版本jQuery,覆盖了highcharter运行依赖的jQuery环境,导致highchart的渲染逻辑执行失败。单独使用highcharter时无冲突,所以可以正常运行。
解决方法
以下两种方案均可解决该问题:
方案1:屏蔽wordcloud2的重复jQuery依赖(最稳定)
在UI定义中添加suppressDependencies("jquery"),屏蔽wordcloud2自带的jQuery加载,直接使用Shiny全局自带的jQuery即可,修改后的完整可运行代码如下:
library(shiny) library(wordcloud2) library(highcharter) library(data.table) ui <- fluidPage( # 关键行:屏蔽重复加载的jQuery依赖 suppressDependencies("jquery"), navbarPage( "Try put Wordcloud2 and Highcharter in the same page", id = "main_navbar", tabPanel( "MytabPanel", fluidRow( width = 12, column(width = 7, wordcloud2Output("tdb1_wordcloud", height = 620)), column(width = 5, highchartOutput("tdb1_myplot", height = 620)) ) ) ) ) server <- function(input, output, session) { output$tdb1_wordcloud <- renderWordcloud2({ wordcloud2(data.table(word = c("email","phone","visit"), freq = c(10,15,4))) }) output$tdb1_myplot <- renderHighchart({ x <- c(rnorm(10000), rnorm(1000, 4, 0.5)) hchart(x, name = "data") }) } runApp(list(ui = ui, server = server),launch.browser = TRUE)
方案2:调整组件声明顺序
如果不想修改依赖配置,也可以直接调换两个output在UI中的声明顺序,先写highchartOutput,再写wordcloud2Output,让highcharter的依赖优先加载完成,避免被覆盖,仅需修改fluidRow内的代码即可:
fluidRow( width = 12, column(width = 5, highchartOutput("tdb1_myplot", height = 620)), column(width = 7, wordcloud2Output("tdb1_wordcloud", height = 620)) )
内容的提问来源于stack exchange,提问作者Martin RDL
相关产品推荐
相关产品推荐

