R包shiny开发网页--6.shinydashboard自定义

小洁写于2018.9.26 想了想同一个系列超过十篇估计就没人看了。所以一股脑把三篇合成了一篇,想想就肉疼呀。豆豆蛰伏几天后复出,把我的档期全吃掉了。记仇中。我没偷懒啊我学shiny呢!
本文包括侧边栏、正文部分的box和页面的美化。

Part1 侧边栏sidebar

1.1.菜单栏与选项卡模版

library(shiny)
library(shinydashboard)
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Widgets", icon = icon("th"), tabName = "widgets",
             badgeLabel = "new", badgeColor = "green")
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard",
            h2("Dashboard tab content")
    ),
    
    tabItem(tabName = "widgets",
            h2("Widgets tab content")
    )
  )
)

ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  sidebar,
  body
)
server <- function(input, output) {
}
shinyApp(ui, server)

1.2.侧边栏输入

(1)搜索框

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarSearchForm(textId = "searchText", buttonId = "searchButton",
                      label = "Search...")
  ),
  dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)

(2)再加上滑动输入和文本输入

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarSearchForm(textId = "searchText", buttonId = "searchButton",
                      label = "Search..."),
    sliderInput("integer", "Integer:", 
                min=0, max=1000, value=500),
    textInput("text","textInput:")
  ),
  dashboardBody()
)
server <- function(input, output) { }
shinyApp(ui, server)

(3)停用侧边栏

dashboardSidebar(disable = TRUE)

Part2 正文-body

参考学习:http://rstudio.github.io/shinydashboard/structure.html#boxes

2.1.基本框

shinydashboard基本构建块是box。box()可以创建基本框,框里的内容可以是大多数的UI控件。


在同一行放两个box:一个文本输入框,一个滑块
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(textInput("text", "Text input:")),
      box(
        "Box content here", br(), "More box content",
        sliderInput("slider", "Slider input:", 1, 100, 50)
      )
    )
  )
)
server <- function(input, output) { }
shinyApp(ui, server)

2.2.设置标题(title)和标题栏(header bar)颜色(status)

在这里status = "primary"显示了蓝色,status = "warning"显示了橙黄色

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(title = "box1", status = "primary",
        textInput("text", "Text input:")),
      box(title = "box2", status = "warning",
        "Box content here", br(), "More box content",
        sliderInput("slider", "Slider input:", 1, 100, 50)
      )
    )
  )
)
server <- function(input, output) { }
shinyApp(ui, server)

2.3.实体标题栏、可折叠box

solidHeader = TRUE可以设置这种格式,collapsible = TRUE可以设置box可折叠。

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar()
  dashboardBody(
    fluidRow(
      box(title ="box1",status = "primary",solidHeader = TRUE,
          collapsible = TRUE,
        textInput("text", "Text input:")),
      box(title ="box2",status = "warning",solidHeader = TRUE,
        sliderInput("slider", "Slider input:", 1, 100, 50)
      )
    )
  )
)
server <- function(input, output) { }
shinyApp(ui, server)

2.4.带背景色的box

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      box(title = "box1", background = "maroon",
        textInput("text", "Text input:")
      ),
      box(title = "box2", background = "black",
        sliderInput("slider", "Slider input:", 1, 100, 50)
      )
    )
  )
)
server <- function(input, output) { }
shinyApp(ui, server)

2.5.标签box

library(shiny)
library(shinydashboard)
body <- dashboardBody(
  fluidRow(
    tabBox(
      title = "First tabBox",
      # The id lets us use input$tabset1 on the server to find the current tab
      id = "tabset1", height = "250px",
      tabPanel("Tab1", "First tab content"),
      tabPanel("Tab2", "Tab content 2")
    ),
    tabBox(
      side = "right", height = "250px",
      selected = "Tab3",
      tabPanel("Tab1", "Tab content 1"),
      tabPanel("Tab2", "Tab content 2"),
      tabPanel("Tab3", "Note that when side=right, the tab order is reversed.")
    )
  ),
  fluidRow(
    tabBox(
      # Title can include an icon
      title = tagList(shiny::icon("gear"), "tabBox status"),
      tabPanel("Tab1",
               "Currently selected tab from first box:",
               verbatimTextOutput("tabset1Selected")
      ),
      tabPanel("Tab2", "Tab content 2")
    )
  )
)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  body)
server <- function(input, output) {
  output$tabset1Selected <- renderText({
    input$tabset1
  })
}
shinyApp(ui, server)

2.6.infobox

一种特殊的box,用于显示简单的数字或文本值,带有图标。
第一行是无填充的,第二行是有填充。


library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Info boxes"),
  dashboardSidebar(),
  dashboardBody(
    # 无填充的box
    fluidRow(
      # 静止
      infoBox("New Orders", 10 * 2, icon = icon("credit-card")),
      # 动态
      infoBoxOutput("progressBox"),
      infoBoxOutput("approvalBox")
    ),
    
    # 有填充的框
    fluidRow(
      infoBox("New Orders", 10 * 2, icon = icon("credit-card"), fill = TRUE),
      infoBoxOutput("progressBox2"),
      infoBoxOutput("approvalBox2")
    ),
    
    fluidRow(
      # 点一次加一个数
      box(width = 4, actionButton("count", "Increment progress"))
    )
  )
)

server <- function(input, output) {
  output$progressBox <- renderInfoBox({
    infoBox(
      "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
      color = "purple"
    )
  })
  output$approvalBox <- renderInfoBox({
    infoBox(
      "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow"
    )
  })
  
  # Same as above, but with fill=TRUE
  output$progressBox2 <- renderInfoBox({
    infoBox(
      "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
      color = "purple", fill = TRUE
    )
  })
  output$approvalBox2 <- renderInfoBox({
    infoBox(
      "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow", fill = TRUE
    )
  })
}

shinyApp(ui, server)

2.7.valueBox
和info的区别好像是图标嵌入?


library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Value boxes"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      # A static valueBox
      valueBox(10 * 2, "New Orders", icon = icon("credit-card")),
      
      # Dynamic valueBoxes
      valueBoxOutput("progressBox"),
      
      valueBoxOutput("approvalBox")
    ),
    fluidRow(
      # Clicking this will increment the progress amount
      box(width = 4, actionButton("count", "Increment progress"))
    )
  )
)

server <- function(input, output) {
  output$progressBox <- renderValueBox({
    valueBox(
      paste0(25 + input$count, "%"), "Progress", icon = icon("list"),
      color = "purple"
    )
  })
  
  output$approvalBox <- renderValueBox({
    valueBox(
      "80%", "Approval", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow"
    )
  })
}

shinyApp(ui, server)

Part3 外观美化

本部分包括调节皮肤、box颜色、图标、标题和侧边栏加宽的代码。
整理自:https://rstudio.github.io/shinydashboard/appearance.html#logout-panel

3.1.皮肤:skin

指定主题颜色,主要是标题栏的颜色
默认是:dashboardPage(skin = "blue")
还有“blue”, “black”, “purple”, “green”, “red”, “yellow”可选,好玩的是,选black标题栏就变成白色了。


白色丑哭了

绿色蛮顺眼
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody()
ui <- dashboardPage(skin = "green",
  header, sidebar, body)
server= function(input, output) { }
shinyApp(ui = ui, server=server )

3.2.box颜色:status或color

status

color

3.3.图标

图标来自Font-Awesome和Glyphicons。所有可用图标列表:

"doudou:", icon("calendar"),
"huahua:", icon("cog", lib = "glyphicon")

以上两行代码分别是这两个网站对应的图标使用方法。
举例:


header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(box("doudou:", icon("calendar")),
                      box("huahua:", icon("cog", lib = "glyphicon")))
ui <- dashboardPage(skin = "black",
  header, sidebar, body)
server= function(input, output) { }
shinyApp(ui = ui, server=server )

3.4.给侧边栏和标题栏加宽

  ui = dashboardPage(
    dashboardHeader(
      title = "Title and sidebar 350 pixels wide",
      titleWidth = 350
    ),
    dashboardSidebar(
      width = 350,
      sidebarMenu(
        menuItem("Menu Item")
      )
    ),
    dashboardBody()
  )
  server = function(input, output) { }

  shinyApp(ui,server)

微信公众号生信星球同步更新我的文章,欢迎大家扫码关注!


我们有为生信初学者准备的学习小组,点击查看◀️
想要参加我的线上线下课程,也可加好友咨询🔼
如果需要提问,请先看生信星球答疑公告

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    X先生_未知数的X阅读 15,937评论 3 118
  • 1、有什么比有一个贤惠的老婆的更好的事情吗? 有两个贤惠的老婆。 2、我真是不小心打破了一个花瓶,我小心翼翼的说着...
    1e81dcf20286阅读 250评论 0 0
  • 在大学图书馆尘土飞扬的旧书库中,一本古旧薄薄,画满优美插图的小书——法国作家圣艾修伯里著的《小王子》将我带入了那个...
    灿烂千阳Ymir阅读 4,455评论 0 2
  • 1.《想念》 我躲在屋里哪里也不想去 美食和风景都失去了意义 因为想念你比它们重要多了 2.《背影》 我明明知道那...
    端木婉清阅读 6,304评论 28 75