前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于shinydashboard搭建你的仪表板(三)

基于shinydashboard搭建你的仪表板(三)

作者头像
1480
发布2019-05-24 11:23:56
1.3K0
发布2019-05-24 11:23:56
举报
文章被收录于专栏:数据分析1480

前言

前面已经介绍了shinydashboard框架的标题栏和侧边栏的输入项部分,这节介绍一下侧边栏的菜单项(menu items),侧边栏的菜单项主要用于切换不同的主体界面,点击不同的菜单项,主体呈现出不同的界面内容。

【R语言】shinydashboard系列一:标题栏

【R语言】shinydashboard系列二:侧边栏--输入项

菜单项menu items

菜单项分类

侧边栏的菜单项可以分为静态菜单项和动态菜单项,注意这里说的静态和动态说的是书写代码的时候,而不是对于呈现的结果。静态菜单项用到两个函数:sidebarMenu()和tabItems(),动态菜单项用到上一节讲到的一对输出函数:sidebarMenuOutput()和renderMenu()。

注意

静态菜单项:sidebarMenu()函数写在ui脚本dashboardSidebar()中,tabItems()函数写在dashboardBody()中;动态菜单项:输出项sidebarMenuOutput()函数写在ui脚本dashboardSidebar()中,renderMenu()函数写在server脚本中与之对应。

静态菜单项

静态菜单项主要用到两个函数:sidebarMenu()和tabItems(),sidebarMenu()函数写在dashboardSidebar()函数中,tabItems()函数写在dashboardBody()中,两种通过tabName相互对应。例如:

代码语言:javascript
复制
library(shiny)library(shinydashboard)library(ggplot2)library(DT)ui <- dashboardPage(  dashboardHeader(title = "Flash WorkingNotes"),  dashboardSidebar(    sidebarMenu(      menuItem("Data", tabName = "Data", icon = icon("dashboard")),      menuItem("Summary", tabName = "Summary",icon = icon("th"), badgeColor = "green"),      menuItem("Plot", tabName = "Plot", icon = icon("bar-chart-o")),      menuItem("Plot1", tabName = "Plot1", icon = icon("bar-chart-o"))    )),  dashboardBody(    tabItems(      tabItem(tabName = "Data", box(dataTableOutput("Data"), width = 100)),      tabItem(tabName = "Summary",verbatimTextOutput("Summary")),      tabItem(tabName = "Plot", plotOutput("Plot")),      tabItem(tabName = "Plot1",plotOutput("Plot1")))  ))server <- function(input, output) {  set.seed(123)  data = diamonds[sample(1:nrow(diamonds), 10000, replace = F), ]  output$Data <- renderDataTable({datatable(data)})   output$Summary <- renderPrint({str(data)})  output$Plot <- renderPlot({    ggplot(data, aes(x = price, fill = cut)) +       geom_histogram(position = "fill", bins = 30) +      ggtitle("histogram plot") +      theme(plot.title = element_text(hjust = 0.5)) + xlab("")  })  output$Plot1 <- renderPlot({    ggplot(data = data, aes(x = carat, y = price, colour = color)) +      geom_point() +  ggtitle("scatter diagram") +      theme(plot.title = element_text(hjust = 0.5))  })}shinyApp(ui, server)

注意上面sidebarMenu()和tabItems()书写位置,sidebarMenu()中的menuItem与tabItems()中的tabItem成对出现,通过tabname一一对应。

上面4个菜单项:Data菜单项呈现原数据,Summary菜单项查看数据字段类型,Plot菜单项绘制直方图,Plot1菜单项绘制散点图。上述代码运行结果:

动态菜单项

动态菜单项通过sidebarMenuOutput()和renderMenu()实现。sidebarMenuOutput()写在ui中的dashboardSidebar()中,renderMenu()写在server中与之对应,两者通过变量名匹配。

代码语言:javascript
复制
library(shiny)library(shinydashboard)library(ggplot2)library(DT)
ui <- dashboardPage(  dashboardHeader(title = "Flash WorkingNotes"),  dashboardSidebar(sidebarMenuOutput("menu")),  dashboardBody(     tabItems(    tabItem(tabName = "Data", box(dataTableOutput("Data"), width = 100)),    tabItem(tabName = "Summary",verbatimTextOutput("Summary")),    tabItem(tabName = "Plot", plotOutput("Plot")),    tabItem(tabName = "Plot1",plotOutput("Plot1")))))
server <- function(input, output) {  set.seed(123)  data = diamonds[sample(1:nrow(diamonds), 10000, replace = F), ]  output$menu <- renderMenu({    sidebarMenu(      menuItem("Data", tabName = "Data", icon = icon("dashboard")),      menuItem("Summary", icon = icon("th"), tabName = "Summary",badgeColor = "green"),      menuItem("Plot", tabName = "Plot", icon = icon("bar-chart-o")),      menuItem("Plot1", tabName = "Plot1", icon = icon("bar-chart-o"))    )  })  output$Author <- renderText({paste("The Author:", input$text)})  output$Data <- renderDataTable({datatable(data)})  output$Summary <- renderPrint({str(data)})  output$Plot <- renderPlot({    ggplot(data, aes(x = price, fill = cut)) +       geom_histogram(position = "fill", bins = 30) +      ggtitle("histogram plot") +      theme(plot.title = element_text(hjust = 0.5)) + xlab("")  })  output$Plot1 <- renderPlot({    ggplot(data = data, aes(x = carat, y = price, colour = color)) +      geom_point() +  ggtitle("scatter diagram") +      theme(plot.title = element_text(hjust = 0.5))  })}shinyApp(ui, server)

注意上面sidebarMenuOutput()和renderMenu()的书写位置,两者通过menu变量名匹配。

总结

恭喜勇士,进入西决!!!将侧边栏的输入项和菜单项介绍完整。菜单项用于切换主体呈现的界面,输入项用于改变主体呈现的内容,书写代码的时候菜单项有静态菜单项和动态菜单项。重点注意菜单项和输入项以及对应的输出项函数的书写位置,即可灵活使用。最后上传一下文章开头动态图的代码。

代码语言:javascript
复制
library(shiny)library(shinydashboard)library(ggplot2)library(DT)ui <- dashboardPage(  dashboardHeader(title = "Flash WorkingNotes"),  dashboardSidebar(    sidebarMenu(      menuItem("Data", tabName = "Data", icon = icon("dashboard")),      menuItem("Summary", icon = icon("th"), tabName = "Summary",badgeColor = "green"),      menuItem("Plot", tabName = "Plot", icon = icon("bar-chart-o")),      menuItem("Plot1", tabName = "Plot1", icon = icon("bar-chart-o"))    )),  dashboardBody(    tabItems(      tabItem(tabName = "Data", box(dataTableOutput("Data"), width = 100)),      tabItem(tabName = "Summary",verbatimTextOutput("Summary")),      tabItem(tabName = "Plot", plotOutput("Plot")),      tabItem(tabName = "Plot1",plotOutput("Plot1")))  ))
server <- function(input, output) {  set.seed(123)  data = diamonds[sample(1:nrow(diamonds), 10000, replace = F), ]    output$Author <- renderText({paste("The Author:", input$text)})  output$Data <- renderDataTable({datatable(data)})  output$Summary <- renderPrint({str(data)})  output$Plot <- renderPlot({    ggplot(data, aes(x = price, fill = cut)) +       geom_histogram(position = "fill", bins = 30) +      ggtitle("histogram plot") +      theme(plot.title = element_text(hjust = 0.5)) + xlab("")  })  output$Plot1 <- renderPlot({    ggplot(data = data, aes(x = carat, y = price, colour = color)) +      geom_point() +  ggtitle("scatter diagram") +      theme(plot.title = element_text(hjust = 0.5))  })}shinyApp(ui, server)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据分析1480 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档