我正在使用R的d3heatmap库构建热图:https://cran.r-project.org/web/packages/d3heatmap/d3heatmap.pdf
我希望能够允许用户在height =
函数中自由调整(通过UI)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"
是非数字参数,因此numericInput()
等UI工具不起作用。同样,selectInput()
也不起作用,例如:
selectInput("foo", "Bar:", c("400px", "700px", "1000px"))
其中d3heatmapOutput("heatmap", height = "foo")
。不幸的是,这些选项都不起作用,这让我想知道我是否可能忽略了一个更简单,更优雅的选择。
答案 0 :(得分:2)
在此示例中,您可以使用滑块控制绘图的高度。我们的想法是在服务器端渲染地图,并使用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)