d3heatmap 和 rcharts 在 Shiny 应用程序的不同选项卡中不起作用

d3heatmap and rcharts don't work in different tabs in a Shiny application

我在 Shiny 应用程序的不同选项卡(或者实际上,Shiny 应用程序的同一选项卡)上通过 rCharts 使用 nvd3 库和 d3heatmap 库制作应用程序时遇到了一些问题。

我在这里做了一个小例子来说明这个问题,基本上绘制了树状图但没有绘制热图。如果你摆脱 rCharts 选项卡,虽然它工作正常。两个库之间存在某种冲突?

我可以解决第一个问题(d3.tip 不是函数),注释行以 # HTML 开头,因此它绘制了彩色框,但您无法与它们交互,并且未打印行名(错误:无法读取未定义的 属性 "apply")。

library(d3heatmap)
library(shiny)
ui <- fluidPage(

  # HTML('<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>'),

  sidebarLayout(
    sidebarPanel(
      h1("A heatmap demo")
      ),
    mainPanel(
      tabsetPanel( # set up tabbed output
        tabPanel("First panel", showOutput("barchart", lib =  "nvd3")),
        tabPanel("Second panel", d3heatmapOutput("heatmap"))
      )
    )
  )
)

server <- function(input, output, session) {
  output$heatmap <- renderD3heatmap({
    d3heatmap(
      mtcars, scale = "column",
      colors = "YlOrRd"
    )
  })

  output$barchart <- renderChart({

    hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
    n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
    n1$addParams(dom = 'barchart')

    return(n1)
  })
}
shinyApp(ui, server)

有什么想法吗?

解决方案基于此中的答案。我认为这与先于其他评估的 JavaScript 的评估有关。

rm(list = ls())
library(d3heatmap)
library(shiny)
library(rCharts)

ui <- fluidPage(

  # HTML('<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>'),

  sidebarLayout(
    sidebarPanel(
      h1("A heatmap demo")
    ),
    mainPanel(
      tabsetPanel( # set up tabbed output
        tabPanel("First panel", showOutput("barchart", lib =  "nvd3")),
        tabPanel("Second panel",  uiOutput("ui_heatmap"))
      )
    )
  )
)
d3heatmap(mtcars, scale="column", colors="YlOrR")

server <- function(input, output, session) {

  mtcars2 = reactive({
    mtcars
  })

  output$ui_heatmap <- renderUI({
    d3heatmapOutput("heatmap", height = paste0(15*nrow(mtcars2()), "px")) 
  })    
  output$heatmap <- renderD3heatmap({ 
    d3heatmap(mtcars2(), scale = "column") 
  })

  output$barchart <- renderChart({

    hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
    n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
    n1$addParams(dom = 'barchart')
    return(n1)
  })
}
shinyApp(ui, server)

的确,设置一个响应式 UI 就足够了。对于未来的访问者,这里是完整的代码

library(d3heatmap)
library(shiny)
library(rCharts)

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(
      h1("A heatmap demo")
    ),
    mainPanel(
      tabsetPanel( # set up tabbed output
        tabPanel("First panel", showOutput("barchart", lib =  "nvd3")),
        tabPanel("Second panel", uiOutput("ui_heatmap"))
      )
    )
  )
)

server <- function(input, output, session) {

  output$ui_heatmap <- renderUI({
    d3heatmapOutput("heatmap")
  })

  output$heatmap <- renderD3heatmap({ 
    d3heatmap(mtcars, scale = "column") 
  })

  output$barchart <- renderChart({

    hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
    n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")
    n1$addParams(dom = 'barchart')

    return(n1)
  })
}
shinyApp(ui, server)