内容简介:除了shiny包,RStudio还开发了一个shinydashboard扩展包,它呈现数据的方式就是专门用于概览或检测数据。接下来的例子意在说明创建一个简易的仪表板非常简单,这个仪表盘可以按每周和每月的时间刻度显示CRAN上下载量最多的最受欢迎的R扩展包。数据源由
除了shiny包,RStudio还开发了一个shinydashboard扩展包,它呈现数据的方式就是专门用于概览或检测数据。
接下来的例子意在说明创建一个简易的仪表板非常简单,这个仪表盘可以按每周和每月的时间刻度显示CRAN上下载量最多的最受欢迎的R扩展包。
数据源由 cranlogs
包提供,先下载安装下面的包:
install.packages(c("shinydashboard", "cranlogs"))
快速查看下CRAN下载数据的数据源:
library(cranlogs) cran_top_downloads() #> No encoding supplied: defaulting to UTF-8. #> rank package count from to #> 1 1 Rcpp 27553 2018-08-29 2018-08-29 #> 2 2 stringi 25057 2018-08-29 2018-08-29 #> 3 3 rlang 23974 2018-08-29 2018-08-29 #> 4 4 digest 22301 2018-08-29 2018-08-29 #> 5 5 ggplot2 21735 2018-08-29 2018-08-29 #> 6 6 glue 21575 2018-08-29 2018-08-29 #> 7 7 stringr 21008 2018-08-29 2018-08-29 #> 8 8 tibble 19051 2018-08-29 2018-08-29 #> 9 9 pillar 18980 2018-08-29 2018-08-29 #> 10 10 fansi 18803 2018-08-29 2018-08-29
cran_top_downloads("last-week")
#> No encoding supplied: defaulting to UTF-8.
#> rank package count from to
#> 1 1 Rcpp 148615 2018-08-23 2018-08-29
#> 2 2 stringi 133327 2018-08-23 2018-08-29
#> 3 3 rlang 131022 2018-08-23 2018-08-29
#> 4 4 digest 127712 2018-08-23 2018-08-29
#> 5 5 glue 115525 2018-08-23 2018-08-29
#> 6 6 ggplot2 114323 2018-08-23 2018-08-29
#> 7 7 stringr 108922 2018-08-23 2018-08-29
#> 8 8 fansi 100226 2018-08-23 2018-08-29
#> 9 9 pillar 99345 2018-08-23 2018-08-29
#> 10 10 tibble 98540 2018-08-23 2018-08-29
为了充分利用shinydashboard包,最好提前浏览下 https://rstudio.github.io/shinydashboard/structure.html 。
与创建shiny应用程序类似,我们从用户界面开始,使用 dashboardPage
、 dashboardSidebar
和 dashboardBody
这3个函数。在仪表板中,我们想要展示扩展包的下载动态,以及每周和每月最受欢迎的扩展包。
我们把月度和周度的菜单放到侧栏中,这样用户可以选择需要查看的数据。在每个标签页,把绘图和表格放在一起。
library(shiny)
library(shinydashboard)
#>
#> 载入程辑包:'shinydashboard'
#> The following object is masked from 'package:graphics':
#>
#> box
library(formattable)
library(cranlogs)
ui = dashboardPage(
dashboardHeader(title = "CRAN Downloads"),
dashboardSidebar(sidebarMenu(
menuItem("Last week",
tabName = "last_week", icon = icon("list")),
menuItem("Last month",
tabName = "last_month", icon = icon("list"))
)),
dashboardBody(tabItems(
tabItem(tabName = "last_week",
fluidRow(tabBox(title = "Total downloads",
tabPanel("Total", formattableOutput("last_week_table"))),
tabBox(title = "Top downloads",
tabPanel("Top", formattableOutput("last_week_top_table"))))),
tabItem(tabName = "last_month",
fluidRow(tabBox(title = "Top downloads",
tabPanel("Total", plotOutput("last_month_barplot"))),
tabBox(title = "Top downloads",
tabPanel("Top", formattableOutput("last_month_top_table")))))
))
)
注意, plotOutput()
是shiny包中的函数,而 formattableOutput()
函数则由 formattable
包提供。开发人员可以创建各种类型的HTML小工具,只要包恰当地定义了 render*
函数和 Output
函数来生成正确的HTML代码,我们就可以把小 工具 嵌入shiny应用程序。
接下来我们定义服务器逻辑。因为输出结果完全依赖于数据源,在调用函数 formattable()
和 plot()
之前要先下载数据。
server = function(input, output){
output$last_week_table = renderFormattable({
data = cran_downloads(when = "last-week")
formattable(data, list(count = color_bar("lightblue")))
})
output$last_week_top_table = renderFormattable({
data = cran_top_downloads("last-week")
formattable(data, list(count = color_bar("lightblue"),
package = formatter("span",
style = "font-family: monospace;")))
})
output$last_month_barplot = renderPlot({
data = subset(cran_downloads(when = "last-month"), count > 0)
with(data, barplot(count, names.arg = date),
main = "Last month downloads")
})
output$last_month_top_table = renderFormattable({
data = cran_top_downloads("last-month")
formattable(data, list(count = color_bar("lightblue"),
package = formatter("span",
style = "font-family: monospace;")))
})
}
如果数据持续更新,我们可以创建一个动态的仪表板,其中的表格和图表会定期更新。使用 reactiveTimer
和 reactive
是实现该功能的关键。如果有需要请查看帮助文档。
现在我们可以运行应用程序了:
runApp(shinyApp(ui, server))
以上所述就是小编给大家介绍的《使用shinydashboard》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Websites with Joomla!
H Graf / Packt Publishing / 2006-01-20 / USD 44.99
This book is a fast paced tutorial to creating a website using Joomla!. If you've never used Joomla!, or even any web content management system before, then this book will walk you through each step i......一起来看看 《Building Websites with Joomla!》 这本书的介绍吧!