简体   繁体   English

Shiny + CSS:在shinydashboard侧边栏中对齐actionButtons

[英]Shiny + CSS: Aligning actionButtons in shinydashboard sidebar

I am trying to place some actionButton s in a shinydashboard sidebar, and need to style them to be centered within the sidebar and horizontally distributed within the space that is allocated to them. 我试图将一些actionButton放在shinydashboard侧边栏中,并且需要将它们设置为在侧边栏中居中并在分配给它们的空间内水平分布。

Here is an MWE: 这是一个MWE:

library(shiny)
library(shinydashboard)

foo_body = dashboardBody()
foo_header = dashboardHeader()
foo_sidebar = dashboardSidebar(
  sidebarMenu(
    menuItem(
      "A Dashboard", 
      tabName = "tab_overview",
      icon = icon("gamepad")  
    )
  ),
  # add some buttons
  actionButton(inputId = "button1", label = "B 1", icon = icon("paper-plane")),
  actionButton(inputId = "button2", label = "B 2", icon = icon("paper-plane")),
  actionButton(inputId = "button3", label = "B 3", icon = icon("paper-plane"))
)

foo_page = dashboardPage(
  header = foo_header,
  sidebar = foo_sidebar,
  body = foo_body,
  title = "A Dashboard"
)

shinyApp(
  ui = foo_page,
  server = function(input, output) {}
)

Here is the relevant part of the app that I need to re-style: 以下是我需要重新设计的应用程序的相关部分:

在此输入图像描述

Any ideas, general or specific, would be welcome. 任何一般或具体的想法都会受到欢迎。

You can add style to your buttons, like so. 您可以为按钮添加样式,如此。 I left 1% margin for sides between the buttons 我在按钮之间留下了1%的边距

library(shiny)
library(shinydashboard)

foo_body = dashboardBody()
foo_header = dashboardHeader()
foo_sidebar = dashboardSidebar(
  sidebarMenu(
    menuItem(
      "A Dashboard", 
      tabName = "tab_overview",
      icon = icon("gamepad")  
    )
  ),
  # add some buttons  
  div(style="display:inline-block;width:32%;text-align: center;",actionButton("button1", label = "B 1", icon = icon("paper-plane"))),
  div(style="display:inline-block;width:32%;text-align: center;",actionButton("button2", label = "B 2", icon = icon("paper-plane"))),
  div(style="display:inline-block;width:32%;text-align: center;",actionButton("button3", label = "B 3", icon = icon("paper-plane")))

)

foo_page = dashboardPage(
  header = foo_header,
  sidebar = foo_sidebar,
  body = foo_body,
  title = "A Dashboard"
)

shinyApp(
  ui = foo_page,
  server = function(input, output) {}
)

在此输入图像描述

You can arrange the button with just fluidRow and column , which is easier to use and can respond to resizing better. 您可以使用fluidRowcolumn排列按钮,这样更容易使用,并且可以更好地响应调整大小。

Adjust the column width and offset if you need to change the arrangement, just like regular Shiny layout. 如果需要更改排列,请调整列宽和偏移,就像常规的Shiny布局一样。 Note the shiny dashboard style may have evolved as your code is showing differently in my machine. 请注意,闪亮的仪表板样式可能已经发展,因为您的代码在我的机器中显示的方式不同。

library(shiny)
library(shinydashboard)

foo_body = dashboardBody()
foo_header = dashboardHeader()
foo_sidebar = dashboardSidebar(
  sidebarMenu(
    menuItem(
      "A Dashboard", 
      tabName = "tab_overview",
      icon = icon("gamepad")  
    )
  ),
  # add some buttons
  fluidRow(
    column(3, offset = 0, 
           actionButton(inputId = "button1", label = "B 1", icon = icon("paper-plane"))),
    column(3, offset = 0, 
           actionButton(inputId = "button2", label = "B 2", icon = icon("paper-plane"))),
    column(3, offset = 0, 
           actionButton(inputId = "button3", label = "B 3", icon = icon("paper-plane")))
  )
)

foo_page = dashboardPage(
  header = foo_header,
  sidebar = foo_sidebar,
  body = foo_body,
  title = "A Dashboard"
)

shinyApp(
  ui = foo_page,
  server = function(input, output) {}
)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM