Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #
  2. # This Shiny web application demonstrates the use of custom image files
  3. # in place of icons for value boxes in Shiny Dashboard by overriding two
  4. # functions:
  5. #
  6. # 'icon' from the shiny package and 'valueBox' from the shinydashboard package.
  7. #
  8. # Each function adds minimal, specific additional handling of image files.
  9. # Note: A custom css file must also be included so that value boxes can
  10. # display the icons. For that reason, do not expect images in place of icons to
  11. # work elsewhere in shiny or shinydashboard.
  12.  
  13. # Motivation: libraries like font awesome and glyphicon cannot be expected to
  14. # provide a substantial suite of icons tailored to probability and statistics
  15. # or many other subjects. Examples here use seven custom icons for inspiration,
  16. # which are simply tiny png files of native R plots. These png files must be
  17. # placed in the app's www/ directory.
  18. #
  19.  
  20. library(shiny)
  21. library(shinydashboard)
  22. library(purrr)
  23.  
  24. ui <- dashboardPage(
  25. dashboardHeader(title="Custom Icons"),
  26. dashboardSidebar(
  27. sidebarMenu(
  28. menuItem("Light icons", tabName = "light"),
  29. menuItem("Dark icons", tabName = "dark")
  30. )
  31. ),
  32. dashboardBody(
  33. tags$head( # must include css
  34. tags$style(HTML("
  35. .img-local {
  36. }
  37.  
  38. .small-box .img-local {
  39. position: absolute;
  40. top: auto;
  41. bottom: 5px;
  42. right: 5px;
  43. z-index: 0;
  44. font-size: 70px;
  45. color: rgba(0, 0, 0, 0.15);
  46. }"
  47. ))
  48. ),
  49. tabItems(
  50. tabItem(tabName = "light",
  51. fluidRow(valueBoxOutput("distLight", width=3)),
  52. fluidRow(
  53. box(plotOutput("hist1"), status="primary", width=6),
  54. box(uiOutput("vBoxesLight"), status="primary", width=6)
  55. )
  56. ),
  57. tabItem(tabName = "dark",
  58. fluidRow(valueBoxOutput("distDark", width=3)),
  59. fluidRow(
  60. box(plotOutput("hist2"), status="primary", width=6),
  61. box(uiOutput("vBoxesDark"), status="primary", width=6)
  62. )
  63. )
  64. )
  65. ),
  66. title="Custom icons"
  67. )
  68.  
  69. server <- function(input, output) {
  70. source("override.R", local = TRUE) # override 'icon' and 'valueBox'
  71. clrs <- c("yellow", "orange", "purple", "red", "blue", "navy")
  72. pTextSize <- function(x, value) tags$p(x, style=paste0("font-size: ", value, "%;"))
  73.  
  74. # image files
  75. fileparts <- c(paste0("normal_", c("mean", "sd", "min", "max", "median"), "_"), "boxplot_iqr_")
  76. files_white <- paste0("stat_icon_", fileparts, "white.png")
  77. files_black <- paste0("stat_icon_", fileparts, "black.png")
  78.  
  79. # data
  80. set.seed(1)
  81. x <- rnorm(1000, 100, 10)
  82. val <- round(c(mean(x), sd(x), min(x), max(x), median(x)))
  83. val <- c(val, paste(round(quantile(x, probs = c(0.25, 0.75))), collapse=" - "))
  84. val <- map2(val, c(rep(100, 5), 75), ~pTextSize(.x, .y))
  85. text <- map(c("Mean", "Std Dev", "Min", "Max", "Median", "IQR"), ~pTextSize(.x, 150))
  86.  
  87. output$vBoxesLight <- renderUI({
  88. vb <- map(1:6, ~valueBox(
  89. val[[.x]], text[[.x]],
  90. icon=icon(list(src=files_white[.x], width="80px"), lib="local"),
  91. color=clrs[.x], width=NULL)
  92. )
  93.  
  94. fluidRow(
  95. tags$head(tags$style(HTML(".small-box {height: 100px}"))),
  96. column(6, vb[[1]], vb[[5]], vb[[3]]),
  97. column(6, vb[[2]], vb[[6]], vb[[4]])
  98. )
  99. })
  100.  
  101. output$vBoxesDark <- renderUI({
  102. vb <- map(1:6, ~valueBox(
  103. val[[.x]], text[[.x]],
  104. icon=icon(list(src=files_black[.x], width="80px"), lib="local"),
  105. color=clrs[.x], width=NULL)
  106. )
  107.  
  108. fluidRow(
  109. tags$head(tags$style(HTML(".small-box {height: 100px}"))),
  110. column(6, vb[[1]], vb[[5]], vb[[3]]),
  111. column(6, vb[[2]], vb[[6]], vb[[4]])
  112. )
  113. })
  114.  
  115. output$distLight <- renderValueBox({
  116. x <- "stat_icon_normal_dist_white.png"
  117. valueBox("Data", "light image icon color",
  118. icon=icon(list(src=x, width="80px"), lib="local"),
  119. color="black", width=NULL)
  120. })
  121.  
  122. output$distDark <- renderValueBox({
  123. x <- "stat_icon_normal_dist_black.png"
  124. valueBox("Data", "dark image icon color",
  125. icon=icon(list(src=x, width="80px"), lib="local"),
  126. color="aqua", width=NULL)
  127. })
  128.  
  129. output$hist1 <- renderPlot({ hist(x) })
  130. output$hist2 <- renderPlot({ hist(x) })
  131. }
  132.  
  133. # Run the application
  134. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement