Guest User

Untitled

a guest
Jan 17th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. ## ui.R ##
  2. library(shinydashboard)
  3.  
  4. dashboardPage(
  5. dashboardHeader(),
  6. dashboardSidebar(),
  7. dashboardBody()
  8. )
  9.  
  10.  
  11. ## app.R ##
  12. library(shiny)
  13. library(shinydashboard)
  14. library(bubbles)
  15. library(dplyr)
  16. library(sunburstR)
  17. library(rvest)
  18. library(stringr)
  19. library(tidyr)
  20. library(networkD3)
  21. library(TraMineR)
  22. library(sunburstR)
  23. library(pipeR)
  24.  
  25. # Define UI
  26. ui <- dashboardPage(skin="blue",
  27. dashboardHeader(title = tags$a(href='http://sailpoint.com',
  28. tags$img(src='sailpoint-logo-wh.png', height=35, width=135))),
  29.  
  30. ## Sidebar content
  31. dashboardSidebar(
  32. sidebarMenu(
  33. menuItem("Bubble plot", tabName = "bubble", icon = icon("group")),
  34. menuItem("Sunburst plot", tabName = "sunburst", icon = icon("sun-o")),
  35. menuItem("Sankey plot", tabName = "plot", icon = icon("tasks"))
  36. )
  37. ),
  38.  
  39. ## Body content
  40. dashboardBody(
  41. tabItems(
  42. # First tab content
  43. tabItem(tabName = "bubble",
  44. fluidRow(
  45. box(
  46. width = 12, status = "info", solidHeader = TRUE,
  47. title = "Peer groups by size",
  48. bubblesOutput("bubble", width = "100%", height = 600)
  49. #plotOutput("bubble_plot", width = "100%", height = 600)
  50. )
  51. ),
  52. fluidRow(
  53. column(
  54. width = 12,
  55. offset = 4,
  56. box(
  57. width = 4, status = "warning", solidHeader = TRUE,
  58. height = 200,
  59. title = "Number of clusters",
  60. sliderInput("slider_bubble", "", 2, 20, 1)
  61. )
  62. )
  63. )
  64. ),
  65.  
  66. # Second tab content
  67. tabItem(tabName = "sunburst",
  68. fluidRow(
  69. box(
  70. width = 12, status = "info", solidHeader = TRUE,
  71. title = "School-to-work transition in Northern Ireland",
  72. sunburstOutput("sunburst", width = "100%", height = 800)
  73. )
  74. )
  75. ),
  76.  
  77. # Third tab content
  78. tabItem(tabName = "plot",
  79. fluidRow(
  80. box(
  81. width = 12,
  82. status = "info", solidHeader = TRUE,
  83. title = "Energy consumption",
  84. plotOutput("plot", width = "100%", height = 800)
  85. )
  86. )
  87. )
  88. )
  89. )
  90. )
  91.  
  92. # Server logic
  93. server <- function(input, output, session){
  94. # output$input <- renderUI({})
  95. # outputOptions(output, "input", suspendWhenHidden = FALSE)
  96. # Bubble plot
  97. output$bubble <- renderBubbles({
  98. bubbles(value = sqrt(exp(seq(0, as.integer(input$slider_bubble) - 1, by=1))),
  99. label = letters[1:as.integer(input$slider_bubble)],
  100. color = paste0("#", lapply(1:as.integer(input$slider_bubble), function(i) paste0(sample(c(0:9,toupper(letters[1:6])), 6 , replace = T),collapse = ''))) %>% unlist())
  101. })
  102.  
  103. # Sunburst plot
  104. output$sunburst <- renderSunburst({
  105. # use example from TraMineR vignette
  106. # data("mvad")
  107. # mvad.alphab <- c(
  108. # "employment", "FE", "HE", "joblessness",
  109. # "school", "training"
  110. # )
  111. # mvad.seq <- seqdef(mvad, 17:86, xtstep = 6, alphabet = mvad.alphab)
  112. #
  113. # # to make this work, we'll compress the sequences with seqdss
  114. # # could also aggregate with dply later
  115. # seqtab( seqdss(mvad.seq), tlim = 0, format = "SPS" ) %>>%
  116. # attr("freq") %>>%
  117. # (
  118. # data.frame(
  119. # # appending "-end" is necessary for this to work
  120. # sequence = paste0(
  121. # gsub(
  122. # x = names(.$Freq)
  123. # , pattern = "(/[0-9]*)"
  124. # , replacement = ""
  125. # , perl = T
  126. # )
  127. # ,"-end"
  128. # )
  129. # ,freq = as.numeric(.$Freq)
  130. # ,stringsAsFactors = FALSE
  131. # )
  132. # ) %>>%
  133. sunburst(readRDS("/Users/mohamed.badawy/projects/IAI_exploration/Shiny/sunburst_df.rds"))
  134. })
  135.  
  136. # Sankey plot
  137. output$sankey <- renderSankeyNetwork({
  138. # Load energy projection data
  139. URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
  140. Energy <- jsonlite::fromJSON(URL)
  141.  
  142. # Now we have 2 data frames: a 'links' data frame with 3 columns (from, to, value), and a 'nodes' data frame that gives the name of each node.
  143. #head( Energy$links )
  144. #head( Energy$nodes )
  145.  
  146. # Thus we can plot it
  147. sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
  148. Target = "target", Value = "value", NodeID = "name",
  149. units = "TWh", fontSize = 12, nodeWidth = 30)
  150. })
  151.  
  152. # test plot
  153. output$plot <- renderPlot({hist(rnorm(1000))})
  154. }
  155.  
  156.  
  157. shinyApp(ui, server)
  158.  
  159. ## app.R ##
  160. library(shiny)
  161. library(shinydashboard)
  162. library(bubbles)
  163. library(dplyr)
  164. library(sunburstR)
  165. library(rvest)
  166. library(stringr)
  167. library(tidyr)
  168. library(networkD3)
  169. library(TraMineR)
  170. library(sunburstR)
  171. library(pipeR)
  172.  
  173. # Define UI
  174. ui <- dashboardPage(skin="blue",
  175. dashboardHeader(title = tags$a(href='http://sailpoint.com',
  176. tags$img(src='sailpoint-logo-wh.png', height=35, width=135))),
  177.  
  178. ## Sidebar content
  179. dashboardSidebar(
  180. sidebarMenu(
  181. menuItem("Bubble plot", tabName = "bubble", icon = icon("group")),
  182. menuItem("Sunburst plot", tabName = "sunburst", icon = icon("sun-o")),
  183. menuItem("Sankey plot", tabName = "sankey", icon = icon("tasks"))
  184. )
  185. ),
  186.  
  187. ## Body content
  188. dashboardBody(
  189. tabItems(
  190. # First tab content
  191. tabItem(tabName = "bubble",
  192. fluidRow(
  193. box(
  194. width = 12, status = "info", solidHeader = TRUE,
  195. title = "Peer groups by size",
  196. bubblesOutput("bubble", width = "100%", height = 600)
  197. #plotOutput("bubble_plot", width = "100%", height = 600)
  198. )
  199. ),
  200. fluidRow(
  201. column(
  202. width = 12,
  203. offset = 4,
  204. box(
  205. width = 4, status = "warning", solidHeader = TRUE,
  206. height = 200,
  207. title = "Number of clusters",
  208. sliderInput("slider_bubble", "", 2, 20, 1)
  209. )
  210. )
  211. )
  212. ),
  213.  
  214. # Second tab content
  215. tabItem(tabName = "sunburst",
  216. fluidRow(
  217. box(
  218. width = 12, status = "info", solidHeader = TRUE,
  219. title = "School-to-work transition in Northern Ireland",
  220. sunburstOutput("sunburst", width = "100%", height = 800)
  221. )
  222. )
  223. ),
  224.  
  225. # Third tab content
  226. tabItem(tabName = "sankey",
  227. fluidRow(
  228. box(
  229. width = 12,
  230. status = "info", solidHeader = TRUE,
  231. title = "Energy consumption",
  232. sankeyNetworkOutput("sankey", width = "100%", height = 800)
  233. )
  234. )
  235. )
  236. )
  237. )
  238. )
  239.  
  240. # Server logic
  241. server <- function(input, output, session){
  242. # output$input <- renderUI({})
  243. # outputOptions(output, "input", suspendWhenHidden = FALSE)
  244. # Bubble plot
  245. output$bubble <- renderBubbles({
  246. bubbles(value = sqrt(exp(seq(0, as.integer(input$slider_bubble) - 1, by=1))),
  247. label = letters[1:as.integer(input$slider_bubble)],
  248. color = paste0("#", lapply(1:as.integer(input$slider_bubble), function(i) paste0(sample(c(0:9,toupper(letters[1:6])), 6 , replace = T),collapse = ''))) %>% unlist())
  249. })
  250.  
  251. # Sunburst plot
  252. output$sunburst <- renderSunburst({
  253. # use example from TraMineR vignette
  254. # data("mvad")
  255. # mvad.alphab <- c(
  256. # "employment", "FE", "HE", "joblessness",
  257. # "school", "training"
  258. # )
  259. # mvad.seq <- seqdef(mvad, 17:86, xtstep = 6, alphabet = mvad.alphab)
  260. #
  261. # # to make this work, we'll compress the sequences with seqdss
  262. # # could also aggregate with dply later
  263. # seqtab( seqdss(mvad.seq), tlim = 0, format = "SPS" ) %>>%
  264. # attr("freq") %>>%
  265. # (
  266. # data.frame(
  267. # # appending "-end" is necessary for this to work
  268. # sequence = paste0(
  269. # gsub(
  270. # x = names(.$Freq)
  271. # , pattern = "(/[0-9]*)"
  272. # , replacement = ""
  273. # , perl = T
  274. # )
  275. # ,"-end"
  276. # )
  277. # ,freq = as.numeric(.$Freq)
  278. # ,stringsAsFactors = FALSE
  279. # )
  280. # ) %>>%
  281. sunburst(readRDS("/Users/mohamed.badawy/projects/IAI_exploration/Shiny/sunburst_df.rds"))
  282. })
  283.  
  284. # Sankey plot
  285. output$sankey <- renderSankeyNetwork({
  286. # Load energy projection data
  287. URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
  288. Energy <- jsonlite::fromJSON(URL)
  289.  
  290. # Now we have 2 data frames: a 'links' data frame with 3 columns (from, to, value), and a 'nodes' data frame that gives the name of each node.
  291. #head( Energy$links )
  292. #head( Energy$nodes )
  293.  
  294. # Thus we can plot it
  295. sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
  296. Target = "target", Value = "value", NodeID = "name",
  297. units = "TWh", fontSize = 12, nodeWidth = 30)
  298. })
  299. }
  300.  
  301. shinyApp(ui, server)
Add Comment
Please, Sign In to add comment