代码之家  ›  专栏  ›  技术社区  ›  thothal

将按钮放在selectInput旁边

  •  5
  • thothal  · 技术社区  · 8 年前

    球门

    我想放一个 selectInput 还有 actionButton 并排在我的 shinydashboard::box .按钮应“相对靠近”屏幕 选择输入 无关的 盒子的宽度。

    到目前为止我都试过了

    到目前为止我试过了 column , splitLayout 或者通过 display: inline-block ,但我对以下两种解决方案都不满意:

    • :根据盒子的宽度,盒子之间的间隙 选择输入 动作按钮 太大了(我可以通过扩展 选择输入 宽度为100%,但宽度太大)
    • splitDesign :目前为止最好的选择,但是 cellWidths 基于box的需求调整 width 而且只适用于100% 选择输入 宽度对于大盒子来说,第二次分割的宽度似乎太大了
    • 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)
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Alex Yahiaoui Martinez    8 年前

    我希望这会有用。我把宽度=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)
    
    推荐文章