3

我正在使用 R 的 d3heatmap 库构建热图:https ://cran.r-project.org/web/packages/d3heatmap/d3heatmap.pdf

我希望能够允许用户自由调整(通过 UI)height =函数中的参数d3heatmapOutput()

比较以下两个代码片段(只需将它们直接复制/粘贴到 R Studio 中),它们之间的唯一区别height =d3heatmapOutput():

library(d3heatmap)
library(shiny)
ui <- fluidPage(
  h1("A heatmap demo"),
  selectInput("palette", "Palette", c("YlOrRd", "RdYlBu", "Greens", "Blues")),
  checkboxInput("cluster", "Apply clustering"),
  d3heatmapOutput("heatmap", height = "400px")
  )
server <- function(input, output, session) {
  output$heatmap <- renderD3heatmap({
    d3heatmap(
      scale(mtcars),
      colors = input$palette,
      dendrogram = if (input$cluster) "both" else "none"
) })
    }
    shinyApp(ui, server)

VS。

library(d3heatmap)
library(shiny)
ui <- fluidPage(
  h1("A heatmap demo"),
  selectInput("palette", "Palette", c("YlOrRd", "RdYlBu", "Greens", "Blues")),
  checkboxInput("cluster", "Apply clustering"),
  d3heatmapOutput("heatmap", height = "1000px")
  )
server <- function(input, output, session) {
  output$heatmap <- renderD3heatmap({
    d3heatmap(
      scale(mtcars),
      colors = input$palette,
      dendrogram = if (input$cluster) "both" else "none"
) })
    }
    shinyApp(ui, server)

我想让用户自己选择这个值height =。但是,由于"400px"是非数字参数,因此 UI 工具等numericInput()不起作用。同样,selectInput()也不起作用,例如:

selectInput("foo", "Bar:", c("400px", "700px", "1000px"))

哪里d3heatmapOutput("heatmap", height = "foo")。不幸的是,这些选项都不起作用,这让我想知道我是否可能忽略了一个更简单、更优雅的选项。

4

1 回答 1

4

在此示例中,您可以使用滑块控制绘图的高度。这个想法是在服务器端渲染地图并使用paste0函数来设置所需的像素大小。

library(d3heatmap)
library(shiny)
ui <- fluidPage(
  h1("A heatmap demo"),


  sliderInput("pixels", "size", value = 400, min = 100, max = 1000),

  selectInput("palette", "Palette", c("YlOrRd", "RdYlBu", "Greens", "Blues")),
  checkboxInput("cluster", "Apply clustering"),
  uiOutput("dynamic")
)
server <- function(input, output, session) {
  output$heatmap <- renderD3heatmap({
    d3heatmap(
      scale(mtcars),
      colors = input$palette,
      dendrogram = if (input$cluster) "both" else "none"
    ) })

  output$dynamic <- renderUI({

    d3heatmapOutput("heatmap", height = paste0(input$pixels, "px"))
  })

}
shinyApp(ui, server)
于 2016-05-18T14:41:11.677 回答