繁体   English   中英

在R Shiny中更改d3heatmapOutput()的高度

[英]Changing height of d3heatmapOutput() in R Shiny

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

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

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

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") 不幸的是,这些选项都不起作用,这使我想知道我是否忽略了一个更简单,更优雅的选择。

在此示例中,您可以使用滑块控制绘图的高度。 这个想法是在服务器端渲染地图,并使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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