在我的R闪亮应用中,我想根据数据框的行数调整d3heatmap
(参见包d3heatmap)的高度; height
中有一个参数d3heatmapOutput
来指定它。
但是,我的数据框是在服务器端计算的,那么如何将其行数从服务器端传递到ui端呢?
以下示例反映了我想要做的事情:
runApp(shinyApp(
ui = fluidRow(
selectInput("am", "Automatic (0) or manual (1) transmission?",
choices = c(0,1)),
# How can I have the 'height' argument equal to 'output$height'?
# I cannot use 'textOutput("height")' since it gives html code, not a value.
d3heatmapOutput("heatmap", height = "400px")
),
server = function(input, output) {
mtcars2 = reactive({
mtcars[which(mtcars$am == input$am),]
})
output$height <- renderText({
paste0(15*nrow(mtcars2()), "px")
})
output$heatmap <- renderD3heatmap({
d3heatmap(mtcars2(), scale = "column")
})
}
))
谢谢。
答案 0 :(得分:3)
您可以使用uiOutput
中的ui.R
和renderUI
中的server.R
动态添加d3heatmapOutput
:
library(shiny)
library(d3heatmap)
runApp(shinyApp(
ui = fluidRow(
selectInput("am", "Automatic (0) or manual (1) transmission?",
choices = c(0,1)),
uiOutput("ui_heatmap")
),
server = function(input, output) {
mtcars2 = reactive({
mtcars[which(mtcars$am == input$am),]
})
output$ui_heatmap <- renderUI({
d3heatmapOutput("heatmap", height = paste0(15*nrow(mtcars2()), "px"))
})
output$heatmap <- renderD3heatmap({
d3heatmap(mtcars2(), scale = "column")
})
}
))
然后,您可以在应用的服务器端设置热图的高度。