r - Shiny仪表板中的相同高度框
问题描述
在创建闪亮的仪表板时,我认为如果盒子的高度相等,它们的顶部和底部就会对齐。不是这种情况。这里的顶部很好地对齐,而底部则没有:
如何确保顶部和底部对齐?
注意:即使两个框填充了完全相同的 ggplot ,也会发生相同的底部错位。
这些说明暗示这很容易。
可以通过设置高度来强制所有盒子的高度相同。与使用 12 宽 Bootstrap 网格设置的宽度相比,高度以像素为单位指定。
示例代码
## app.R ##
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- dashboardPage(
dashboardHeader(title = "Box alignmnent test"),
dashboardSidebar(),
dashboardBody(
# Put boxes in a row
fluidRow(
box(tableOutput("pop_num"), height = 350),
box(plotOutput("speed_distbn", height = 350))
)
)
)
server <- function(input, output) {
# Population numbers
output$pop_num <- renderTable({
df <- tibble(
x = c(1,2,3,4),
y = c(5, 6, 7, 8)
)
})
# Population distribution graph
output$speed_distbn <- renderPlot({
ggplot(data = cars, aes(x = speed, y = dist)) +
geom_point()
})
}
shinyApp(ui, server)
解决方案
请注意,当您设置高度时,第一个350
应用于box
函数,而第二个350
作为参数传递给plotOutput
函数。
只要确保两个box
函数都传递了相同的高度参数;另请注意,如果绘图(可能包括一些额外的填充/边距)的总高度大于封闭框的高度,它将溢出底部。所以为了安全起见,将参数height
传递给and函数:plotOutput
box
box_height = "20em"
plot_height = "16em"
ui <- dashboardPage(
dashboardHeader(title = "Box alignmnent test"),
dashboardSidebar(),
dashboardBody(
# Put boxes in a row
fluidRow(
box(tableOutput("pop_num"), height = box_height),
box(plotOutput("speed_distbn",height = plot_height), height = box_height)
)
)
)
请注意,绘图的高度较小。可能有一种更聪明的方法可以自动执行此操作(当然,如果您执行一些自定义 CSS!),但出于说明目的,这是可行的。
推荐阅读
- javascript - 创建了一个新对象,然后在循环中放置一个数组以修补属性,它可以工作,但我不知道为什么?
- java - 为什么 Javafx 本机图像不适用于 javafx.scene.media.MediaPlayer 类?
- c# - 斐波那契函数在应该为正时返回负值
- android - 更新到 Android Studio 版本 4.1.3 后出现 Gradle 问题
- cassandra - 如何增加 Cassandra 中非计数器列的值?
- copy - 将记录从子表单上的一个字段复制到另一个子表单
- python - 未创建 Canvas 的图像
- java - Mono.subscribe() 向客户端抛出异常
- python - Python 中 switch/case 的语法等价物是什么?
- reactjs - Reactjs 不加载文本字体