目标
我想把一个selectInput
和一个actionButton
并排放在我的shinydashboard::box
的页脚里。无论盒子的宽度是多少,按钮都应该“相对靠近”selectInput
。
我到目前为止所做的努力
到目前为止,我尝试了column
、splitLayout
或通过display: inline-block
进行样式设置,但我对以下两种解决方案都不满意:column
:根据盒子的宽度,selectInput
和actionButton
之间的间隙太大(我可以通过将selectInput
宽度扩展到100%来部分解决这个问题,但是宽度太大)splitDesign
:到目前为止是最好的选择,但是cellWidths
需要根据boxwidth
进行调整,并且只能使用100%的selectInput
宽度,对于大的box,第二个分割的宽度似乎太大inline-block
:不能很好地处理一般的CSS
例子
library(shiny)
library(purrr)
library(shinydashboard)
widths <- c(1, 2,3, 4, 6, 12)
makeBoxes <- function(width, method = c("split", "col", "css")) {
method <- match.arg(method)
split <- function(width, count) {
splitLayout(selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS,
width = "100%"),
actionButton(paste("ab", width, count, sep = "_"), icon("trash")),
cellWidths = c("87.5%", "12.5%"),
cellArgs = list(style = "vertical-align: top"))
}
col <- function(width, count) {
fluidRow(column(width = 11,
selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS,
width = "100%")),
column(width = 1,
actionButton(paste("ab", width, count, sep = "_"), icon("trash"))))
}
css <- function(width, count) {
fluidRow(div(selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS),
style = "display: inline-block; vertical-align: top"),
actionButton(paste("ab", width, count, sep = "_"), icon("trash")))
}
wrap <- function(method, ...)
switch(method, split = split(...), col = col(...), css = css(...))
map(seq(1, 12 / width, 1), function(count)
box(solidHeader = TRUE, title = "Box", status = "info", width = width,
footer = wrap(method, width, count)))
}
server <- function(input, output) {
}
ui1 <- dashboardPage(dashboardHeader(), dashboardSidebar(),
dashboardBody(map(widths, ~ fluidRow(makeBoxes(.x, "split")))))
ui2 <- dashboardPage(dashboardHeader(), dashboardSidebar(),
dashboardBody(map(widths, ~ fluidRow(makeBoxes(.x, "col")))))
ui3 <- dashboardPage(dashboardHeader(), dashboardSidebar(),
dashboardBody(map(widths, ~ fluidRow(makeBoxes(.x, "css")))))
shinyApp(ui1, server)
shinyApp(ui2, server)
shinyApp(ui3, server)
最佳答案
我希望这能有用。我把宽度改为11到12,我觉得很好。
这就是你想要的吗?
library(shiny)
library(purrr)
library(shinydashboard)
widths <- c(1, 2,3, 4, 6, 12)
makeBoxes <- function(width, method = c("split", "col", "css")) {
method <- match.arg(method)
split <- function(width, count) {
splitLayout(selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS,
width = "100%"),
actionButton(paste("ab", width, count, sep = "_"), icon("trash")),
cellWidths = c("87.5%", "12.5%"),
cellArgs = list(style = "vertical-align: top"))
}
col <- function(width, count) {
fluidRow(column(width = 12, # width = 11 -> 12
selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS,
width = "100%")),
column(width = 1,
actionButton(paste("ab", width, count, sep = "_"), icon("trash"))))
}
css <- function(width, count) {
fluidRow(div(selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS),
style = "display: inline-block; vertical-align: top"),
actionButton(paste("ab", width, count, sep = "_"), icon("trash")))
}
wrap <- function(method, ...)
switch(method, split = split(...), col = col(...), css = css(...))
map(seq(1, 12 / width, 1), function(count)
box(solidHeader = TRUE, title = "Box", status = "info", width = width,
footer = wrap(method, width, count)))
}
server <- function(input, output) {
}
ui2 <- dashboardPage(dashboardHeader(), dashboardSidebar(),
dashboardBody(map(widths, ~ fluidRow(makeBoxes(.x, "col")))))
shinyApp(ui2, server)