[英]Changing height of d3heatmapOutput() in R Shiny
I'm building a heatmap using R's d3heatmap library: https://cran.r-project.org/web/packages/d3heatmap/d3heatmap.pdf 我正在使用R的d3heatmap库构建一个热图: https ://cran.r-project.org/web/packages/d3heatmap/d3heatmap.pdf
I'd like to be able to allow a user to freely adjust (through the UI) the height =
argument in the d3heatmapOutput()
function. 我希望能够允许用户自由地(通过UI)调整
d3heatmapOutput()
函数中的height =
参数。
Compare the following two code snippets (just copy/paste them directly into R Studio), where the only difference between them is the value of the height =
argument in the d3heatmapOutput()
: 比较以下两个代码片段(只需将它们直接复制/粘贴到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. 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)
I'd like to allow the user to choose this value of height =
themselves. 我想允许用户选择
height =
自己的这个值。 However, because "400px"
is a non-numeric argument, UI tools such as numericInput()
don't work. 但是,由于
"400px"
是一个非数字参数,因此UI工具(例如numericInput()
不起作用。 Likewise, selectInput()
doesn't work either, eg: 同样,
selectInput()
也不起作用,例如:
selectInput("foo", "Bar:", c("400px", "700px", "1000px"))
where d3heatmapOutput("heatmap", height = "foo")
. 其中
d3heatmapOutput("heatmap", height = "foo")
。 Unfortunately, neither of these options work, which makes me wonder if I may have overlooked a simpler, more elegant option. 不幸的是,这些选项都不起作用,这使我想知道我是否忽略了一个更简单,更优雅的选择。
In this example you can control the hight of the plot using a slider. 在此示例中,您可以使用滑块控制绘图的高度。 The idea is to render the map on the server side and to use
paste0
function to set desired size in pixels. 这个想法是在服务器端渲染地图,并使用
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.