There and Back Again competition entry https://shiny.petras.space/commute/
rstats
rshiny
census
competition
leaflet
javascript
stats-nz

app.R 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. library(shiny)
  2. library(shinyjs)
  3. library(leaflet)
  4. library(rgdal)
  5. library(dplyr)
  6. library(leaflet.extras)
  7. library(shinyWidgets)
  8. source("leafletfunctions.R")
  9. source("extras.R")
  10. # work_travel <- read_csv("../travel-work.csv")
  11. load(file="datasets.RData")
  12. shpf <- readOGR(dsn="sa20025WGSfil")
  13. sa.in.dest <- shpf@data$SA22018_V1 %in% work_to$work_code
  14. sa.in.home <- shpf@data$SA22018_V1 %in% work_from$res_code
  15. transport.t <- c("Private car", "Passenger in car",
  16. "Walk", "Bicycle", "Company car", "Bus", "Train",
  17. "Ferry", "Other", "(Work at home)", "None/Unknown")
  18. edu.t <- c("Drive self", "Passenger in car", "Walk", "Bicycle",
  19. "School bus", "Public bus", "Train", "Ferry",
  20. "Other", "(Study at home)", "None/Unknown")
  21. cols.labs <- c(transport.t[1:10], "Total")
  22. cols.edu.labs <- c(edu.t[1:10], "Total")
  23. codelist <- shpf@data %>%
  24. mutate(sa2_code = as.numeric(as.character(SA22018_V1))) %>%
  25. select(sa2_code)
  26. startcols.res <- codelist %>% left_join(work_from, by = c("sa2_code" = "res_code"))
  27. startcols.res <- tencols[startcols.res$MAX]
  28. startcols.res <- ifelse(is.na(startcols.res), "#808080", startcols.res)
  29. startcols.work <- codelist %>% left_join(work_to, by = c("sa2_code" = "work_code"))
  30. startcols.work <- tencols[startcols.work$MAX]
  31. startcols.work <- ifelse(is.na(startcols.work), "#808080", startcols.work)
  32. startcols.edures <- codelist %>% left_join(edu_from,
  33. by = c("sa2_code" = "res_code"))
  34. startcols.edures <- tencols[startcols.edures$MAX]
  35. startcols.edures <- ifelse(is.na(startcols.edures), "#808080", startcols.edures)
  36. startcols.edu <- codelist %>% left_join(edu_to, by = c("sa2_code" = "edu_code"))
  37. startcols.edu <- tencols[startcols.edu$MAX]
  38. startcols.edu <- ifelse(is.na(startcols.edu), "#808080", startcols.edu)
  39. hrstr <- "<hr/>"
  40. # Define UI
  41. ui <- fillPage(
  42. useShinyjs(),
  43. leafletjs,
  44. keyboardjs,
  45. shiny::tags$title("How did Kiwis commute in 2018?"),
  46. tags$style(type = "text/css", extracss),
  47. leafletOutput("map"),
  48. absolutePanel(top = 10, right = 10, id="mapcontrol",
  49. div(
  50. radioGroupButtons("radioeduemp",
  51. label = "Commuters (age 15+) travelling to",
  52. status = "default",
  53. choiceNames = list(
  54. HTML("Em<span class='shortcut'>p</span>loyment"),
  55. HTML("E<span class='shortcut'>d</span>ucation")
  56. ),
  57. choiceValues = list(
  58. "Employment", "Education"
  59. )
  60. ),
  61. radioGroupButtons("radioinout", label="Show commuters who",
  62. choiceNames = list(
  63. HTML("Commute <span class='shortcut'>f</span>rom selected area"),
  64. HTML("Commute <span class='shortcut'>t</span>o selected area")),
  65. choiceValues = list(
  66. "res",
  67. "work"
  68. ),
  69. direction="vertical",
  70. justified = TRUE,
  71. status = "default",
  72. checkIcon = list(yes = icon("check"),
  73. no = icon("check"))
  74. ),
  75. radioGroupButtons("radiocolour",
  76. label = "Colour by",
  77. choiceNames = list(
  78. HTML("M<span class='shortcut'>o</span>st common commute method"),
  79. HTML("N<span class='shortcut'>u</span>mber of commuters")
  80. ),
  81. choiceValues = list(
  82. "type",
  83. "number"
  84. ),
  85. direction = "vertical",
  86. checkIcon = list(yes = icon("check"),
  87. no = icon("check")),
  88. status = "default",
  89. justified = TRUE),
  90. div(class="locinfo",
  91. htmlOutput("lochtml")),
  92. div(id="loc2"),
  93. htmlOutput("secondarylochtml"))
  94. ),
  95. absolutePanel(top=13, right=13, id="infobuttoncontainer",
  96. title="Toggle (I)nformation",
  97. prettyToggle("mapinfobutton",
  98. label_on = NULL,
  99. label_off = NULL,
  100. icon_on=icon("times"),
  101. icon_off = icon("info"),
  102. animation = "pulse",
  103. inline = TRUE,
  104. bigger=TRUE,
  105. status_on = "danger",
  106. status_off = "primary",
  107. value = TRUE)
  108. ),
  109. absolutePanel(top = 45, right = 7, id="control2", title="Toggle pane(L)",
  110. prettySwitch("controlswitch", value=TRUE, label = NULL,
  111. slim = FALSE,
  112. inline=TRUE, status="info",
  113. fill=TRUE, bigger=FALSE)),
  114. absolutePanel(bottom = 26, right = 10, id="loading",
  115. p("Loading...")),
  116. absolutePanel(bottom=26, right=10, left=10, top=10, id="infopanel",
  117. infotext)
  118. )
  119. # Define server logic
  120. server <- function(input, output) {
  121. sel.SA2.code <- reactiveVal(0)
  122. attribupdate <- FALSE
  123. mouseover <- reactive({
  124. lastover <- input$map_shape_mouseover$id
  125. lastover <- ifelse(is.null(lastover), 0, lastover)
  126. lastout <- input$map_shape_mouseout$id
  127. lastout <- ifelse(is.null(lastout), 0, lastout)
  128. ifelse(lastout == lastover, 0, lastover)
  129. })
  130. output$map <- renderLeaflet({
  131. leaf <- leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13,
  132. crs = NULL)) %>%
  133. addPolygons(color="#000", opacity = 1, weight=1,
  134. fillColor = startcols.res,
  135. layerId = ~SA22018_V1,
  136. label = shpf@data$SA22018__1,
  137. fillOpacity = 1, group = "polys") %>%
  138. setView(174, -41, 6) %>%
  139. addResetMapButton() %>%
  140. addSearchFeatures("polys",
  141. options = searchFeaturesOptions(
  142. hideMarkerOnCollapse = TRUE,
  143. autoCollapse = FALSE,
  144. openPopup = FALSE,
  145. zoom=11,
  146. position="topleft")) %>%
  147. addLegend(position = "topleft",
  148. colors = c(tencols, "#808080"),
  149. labels = transport.t, opacity = 1,
  150. title = HTML("Most Common<br/>Commute Method"))
  151. shinyjs::hideElement(selector="#loading p", asis = TRUE,
  152. anim=TRUE, animType = "slide", time=10)
  153. leaf
  154. })
  155. updateMap <- function() {
  156. shinyjs::showElement(selector="#loading p", asis = TRUE,
  157. anim=TRUE, animType = "slide")
  158. selcode <- sel.SA2.code()
  159. selcode <- ifelse(is.na(selcode), 0, selcode)
  160. psel <- selcode %in% shpf@data$SA22018_V1
  161. if (input$radioeduemp == "Employment") {
  162. if (input$radiocolour == "type") {
  163. if (input$radioinout == "work") {
  164. fcols <- startcols.work
  165. if (psel) {
  166. codvs <- work_simp %>% filter(work_code == selcode)
  167. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  168. codvs <- tencols[codvs$MAX]
  169. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  170. }
  171. } else {
  172. fcols <- startcols.res
  173. if (psel) {
  174. codvs <- work_simp %>% filter(res_code == selcode)
  175. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "work_code"))
  176. codvs <- tencols[codvs$MAX]
  177. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  178. }
  179. }
  180. lp <- leafletProxy("map", data = shpf) %>%
  181. setShapeStyle(layerId = ~SA22018_V1, fillColor = fcols) %>%
  182. clearControls() %>%
  183. addLegend(position = "topleft",
  184. colors = c(tencols, "#808080"),
  185. labels = transport.t, opacity = 1,
  186. title = HTML("Most Common<br/>Commute Method")
  187. ) %>%
  188. clearGroup("hpoly")
  189. } else {
  190. if (input$radioinout == "work") {
  191. if (psel) {
  192. codvs <- work_simp %>% filter(work_code == selcode)
  193. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  194. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  195. } else {
  196. codvs <- codelist %>%
  197. left_join(work_to, by = c("sa2_code" = "work_code"))
  198. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  199. }
  200. } else {
  201. if (psel) {
  202. codvs <- work_simp %>% filter(res_code == selcode)
  203. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "work_code"))
  204. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  205. } else {
  206. codvs <- codelist %>%
  207. left_join(work_from, by = c("sa2_code" = "res_code"))
  208. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  209. }
  210. }
  211. cvr <- range(cvals, na.rm = TRUE)
  212. binner <- colorBin(c("white", "red"), cvr, bins = 7, pretty = TRUE)
  213. lp <- leafletProxy("map", data = shpf) %>%
  214. setShapeStyle(layerId = ~SA22018_V1, fillColor = binner(cvals)) %>%
  215. clearControls() %>%
  216. addLegend(position = "topleft",
  217. pal = binner,
  218. values = cvals, opacity = 1,
  219. na.label = "None",
  220. title = "Number of commuters") %>%
  221. clearGroup("hpoly")
  222. }
  223. } else {
  224. # Education code
  225. if (input$radiocolour == "type") {
  226. if (input$radioinout == "work") {
  227. fcols <- startcols.edu
  228. if (psel) {
  229. codvs <- edu_simp %>% filter(edu_code == selcode)
  230. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  231. codvs <- tencols[codvs$MAX]
  232. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  233. }
  234. } else {
  235. fcols <- startcols.edures
  236. if (psel) {
  237. codvs <- edu_simp %>% filter(res_code == selcode)
  238. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "edu_code"))
  239. codvs <- tencols[codvs$MAX]
  240. fcols <- ifelse(is.na(codvs), "#808080", codvs)
  241. }
  242. }
  243. lp <- leafletProxy("map", data = shpf) %>%
  244. setShapeStyle(layerId = ~SA22018_V1, fillColor = fcols) %>%
  245. clearControls() %>%
  246. addLegend(position = "topleft",
  247. colors = c(tencols, "#808080"),
  248. labels = edu.t, opacity = 1,
  249. title = HTML("Most Common<br/>Commute Method")
  250. ) %>%
  251. clearGroup("hpoly")
  252. } else {
  253. if (input$radioinout == "work") {
  254. if (psel) {
  255. codvs <- edu_simp %>% filter(edu_code == selcode)
  256. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "res_code"))
  257. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  258. } else {
  259. codvs <- codelist %>%
  260. left_join(edu_to, by = c("sa2_code" = "edu_code"))
  261. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  262. }
  263. } else {
  264. if (psel) {
  265. codvs <- edu_simp %>% filter(res_code == selcode)
  266. codvs <- codelist %>% left_join(codvs, by=c("sa2_code" = "edu_code"))
  267. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  268. } else {
  269. codvs <- codelist %>%
  270. left_join(edu_from, by = c("sa2_code" = "res_code"))
  271. cvals <- ifelse(codvs$total == 0, NA, codvs$total)
  272. }
  273. }
  274. cvr <- range(cvals, na.rm = TRUE)
  275. binner <- colorBin(c("white", "red"), cvr, bins = 7, pretty = TRUE)
  276. lp <- leafletProxy("map", data = shpf) %>%
  277. setShapeStyle(layerId = ~SA22018_V1, fillColor = binner(cvals)) %>%
  278. clearControls() %>%
  279. addLegend(position = "topleft",
  280. pal = binner,
  281. values = cvals, opacity = 1,
  282. na.label = "None",
  283. title = "Number of commuters") %>%
  284. clearGroup("hpoly")
  285. }
  286. }
  287. if (psel) {
  288. lp %>% addPolygons(group = "hpoly",
  289. weight = 4,
  290. data = shpf[which(shpf@data$SA22018_V1 == selcode),],
  291. color = "#000000",
  292. fill = FALSE, opacity = 1)
  293. }
  294. shinyjs::hideElement(selector="#loading p", asis=TRUE,
  295. anim=TRUE, animType = "slide",
  296. time = 1)
  297. }
  298. observeEvent(input$map_click, ignoreInit = TRUE, {
  299. cursel <- sel.SA2.code()
  300. p <- input$map_click
  301. pdat <- data.frame(Longitude = p$lng,
  302. Latitude =p$lat)
  303. coordinates(pdat) <- ~ Longitude + Latitude
  304. proj4string(pdat) <- proj4string(shpf)
  305. ppoly <- over(pdat, shpf)
  306. codetmp <- as.numeric(as.character(ppoly[1,"SA22018_V1"]))
  307. codetmp <- ifelse(is.na(codetmp), 0, codetmp)
  308. newsl <- ifelse(sel.SA2.code() == codetmp, 0, codetmp)
  309. if (newsl != cursel) {
  310. sel.SA2.code(newsl)
  311. updateMap()
  312. }
  313. })
  314. observeEvent(input$map_zoom, once=TRUE, {
  315. if (!attribupdate) {
  316. shinyjs::html(selector=".leaflet-control-attribution.leaflet-control",
  317. html = attribhtml)
  318. attribupdate <<- TRUE
  319. }
  320. })
  321. observeEvent(input$map_shape_mouseover, once=TRUE,{
  322. # Backup
  323. if (!attribupdate) {
  324. shinyjs::html(selector=".leaflet-control-attribution.leaflet-control",
  325. html = attribhtml)
  326. attribupdate <<- TRUE
  327. }
  328. })
  329. observeEvent(input$radioeduemp, ignoreInit = TRUE, {
  330. updateMap()
  331. })
  332. observeEvent(input$radioinout, ignoreInit = TRUE, {
  333. updateMap()
  334. })
  335. observeEvent(input$radiocolour, ignoreInit = TRUE, {
  336. updateMap()
  337. })
  338. observeEvent(input$controlswitch, ignoreInit = TRUE, {
  339. if (input$controlswitch) {
  340. shinyjs::showElement("mapcontrol", anim=TRUE,
  341. time = 0.5)
  342. } else {
  343. shinyjs::hideElement("mapcontrol", anim=TRUE,
  344. time = 0.5)
  345. }
  346. })
  347. observeEvent(input$mapinfobutton, ignoreInit = TRUE, {
  348. if (input$mapinfobutton) {
  349. shinyjs::showElement("infopanel", anim=TRUE,
  350. time = 0.5)
  351. } else {
  352. shinyjs::hideElement("infopanel", anim=TRUE,
  353. time = 0.5)
  354. shinyjs::runjs("document.getElementById('map').focus()")
  355. }
  356. })
  357. output$lochtml <- renderUI({
  358. seled <- sel.SA2.code()
  359. seled <- ifelse(is.na(seled), 0, seled)
  360. if (!(seled %in% shpf@data$SA22018_V1)) {
  361. HTML("")
  362. } else {
  363. namesel <- shpf@data$SA22018__1[shpf@data$SA22018_V1 == seled]
  364. if (input$radioeduemp == "Employment") {
  365. if (input$radiocolour == "type") {
  366. str <- sprintf("<b>%s</b>", namesel)
  367. if (input$radioinout == "work") {
  368. str <- sprintf("<p>Commuting method of people who <b>work</b> in
  369. <u>%s</u></p>", str)
  370. vals <- as.numeric(work_to[work_to$work_code == seled, 5:15])
  371. vals <- ifelse(is.na(vals), 0, vals)
  372. vals <- ifelse(vals < 0, "~0", as.character(vals))
  373. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  374. vals),
  375. collapse="")
  376. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  377. } else {
  378. str <- sprintf("<p>Commuting method of people who <b>live</b> in
  379. <u>%s</u></p>", str)
  380. vals <- as.numeric(work_from[work_from$res_code == seled, 5:15])
  381. vals <- ifelse(is.na(vals), 0, vals)
  382. vals <- ifelse(vals < 0, "~0", as.character(vals))
  383. listi <- paste0(sprintf("<li>%s: %s</li>", cols.labs,
  384. vals),
  385. collapse="")
  386. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  387. }
  388. HTML(str)
  389. } else {
  390. str <- hrstr
  391. if (input$radioinout == "work") {
  392. val <- as.numeric(work_to[work_to$work_code == seled, 15])
  393. val <- ifelse(is.na(val), 0, ifelse(val < 0, 0, val))
  394. str <- sprintf("%s<p>%d people commute <b>to</b> employment in
  395. <b><u>%s</u></b></p>", str, val, namesel)
  396. if (val > 0) {
  397. subs <- work_simp %>% filter(work_code == seled) %>%
  398. arrange(desc(total)) %>% head(10)
  399. listi <- paste0(sprintf("<li>%s: %s</li>", subs$res_name,
  400. subs$total),
  401. collapse="")
  402. str <- sprintf("%s<p>Top areas to commute from:<p>
  403. <ul>%s</ul>", str, listi)
  404. }
  405. } else {
  406. val <- as.numeric(work_from[work_from$res_code == seled, 15])
  407. val <- ifelse(is.na(val), 0, ifelse(val < 0, 0, val))
  408. str <- sprintf("%s<p>%d people commute to employment <b>from</b>
  409. <b><u>%s</u></b></p>", str, val, namesel)
  410. if (val > 0) {
  411. subs <- work_simp %>% filter(res_code == seled) %>%
  412. arrange(desc(total)) %>% head(10)
  413. listi <- paste0(sprintf("<li>%s: %s</li>", subs$work_name,
  414. subs$total),
  415. collapse="")
  416. str <- sprintf("%s<p>Top areas to commute to:<p>
  417. <ul>%s</ul>", str, listi)
  418. }
  419. }
  420. HTML(str)
  421. }
  422. } else {
  423. if (input$radiocolour == "type") {
  424. str <- sprintf("<b>%s</b>", namesel)
  425. if (input$radioinout == "work") {
  426. str <- sprintf("<p>Commuting method of people who commute to
  427. <b>education</b> in
  428. <u>%s</u></p>", str)
  429. vals <- as.numeric(edu_to[edu_to$edu_code == seled, 5:15])
  430. vals <- ifelse(is.na(vals), 0, vals)
  431. vals <- ifelse(vals < 0, "~0", as.character(vals))
  432. listi <- paste0(sprintf("<li>%s: %s</li>", cols.edu.labs,
  433. vals),
  434. collapse="")
  435. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  436. } else {
  437. str <- sprintf("<p>Commuting method to education
  438. of people who <b>live</b> in
  439. <u>%s</u></p>", str)
  440. vals <- as.numeric(edu_from[edu_from$res_code == seled, 5:15])
  441. vals <- ifelse(is.na(vals), 0, vals)
  442. vals <- ifelse(vals < 0, "~0", as.character(vals))
  443. listi <- paste0(sprintf("<li>%s: %s</li>", cols.edu.labs,
  444. vals),
  445. collapse="")
  446. str <- paste0(hrstr, str, "<ul>", listi, "</ul>")
  447. }
  448. } else {
  449. str <- hrstr
  450. if (input$radioinout == "work") {
  451. val <- as.numeric(edu_to[edu_to$edu_code == seled, 15])
  452. val <- ifelse(is.na(val), 0, ifelse(val < 0, 0, val))
  453. str <- sprintf("%s<p>%d people commute <b>to</b> education in
  454. <b><u>%s</u></b></p>", str, val, namesel)
  455. if (val > 0) {
  456. subs <- edu_simp %>% filter(edu_code == seled) %>%
  457. arrange(desc(total)) %>% head(10)
  458. listi <- paste0(sprintf("<li>%s: %s</li>", subs$res_name,
  459. subs$total),
  460. collapse="")
  461. str <- sprintf("%s<p>Top areas to commute from:<p>
  462. <ul>%s</ul>", str, listi)
  463. }
  464. } else {
  465. val <- as.numeric(edu_from[edu_from$res_code == seled, 15])
  466. val <- ifelse(is.na(val), 0, ifelse(val < 0, 0, val))
  467. str <- sprintf("%s<p>%d people commute to education <b>from</b>
  468. <b><u>%s</u></b></p>", str, val, namesel)
  469. if (val > 0) {
  470. subs <- edu_simp %>% filter(res_code == seled) %>%
  471. arrange(desc(total)) %>% head(10)
  472. listi <- paste0(sprintf("<li>%s: %s</li>", subs$edu_name,
  473. subs$total),
  474. collapse="")
  475. str <- sprintf("%s<p>Top areas to commute to:<p>
  476. <ul>%s</ul>", str, listi)
  477. }
  478. }
  479. }
  480. HTML(str)
  481. }
  482. }
  483. })
  484. output$secondarylochtml <- renderUI({
  485. curshp <- mouseover()
  486. cursel <- sel.SA2.code()
  487. if (curshp == 0) {
  488. if (cursel == 0) {
  489. HTML(paste0(hrstr,
  490. "<p><em>No area selected. Click on
  491. an area for more information.</em></p>"))
  492. } else {
  493. HTML("")
  494. }
  495. } else {
  496. shpname <- shpf@data$SA22018__1[curshp == shpf@data$SA22018_V1]
  497. if (cursel == 0) {
  498. if (input$radioeduemp == "Employment") {
  499. if (input$radioinout == "res") {
  500. fdf <- work_from %>% filter(res_code == curshp)
  501. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  502. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  503. pmp <- ""
  504. if (ttype != 0) {
  505. pmp <- sprintf("Most common mode of transport: %s",
  506. transport.t[ttype])
  507. }
  508. HTML(sprintf("%s<p><em>%d people commute to employment from
  509. %s. %s</em></p>", hrstr, tot, shpname,
  510. pmp))
  511. } else {
  512. fdf <- work_to %>% filter(work_code == curshp)
  513. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  514. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  515. pmp <- ""
  516. if (ttype != 0) {
  517. pmp <- sprintf("Most common mode of transport: %s",
  518. transport.t[ttype])
  519. }
  520. HTML(sprintf("%s<p><em>%d people commute to employment in
  521. %s. %s</em></p>", hrstr, tot, shpname,
  522. pmp))
  523. }
  524. } else {
  525. if (input$radioinout == "res") {
  526. fdf <- edu_from %>% filter(res_code == curshp)
  527. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  528. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  529. pmp <- ""
  530. if (ttype != 0) {
  531. pmp <- sprintf("Most common mode of transport: %s",
  532. edu.t[ttype])
  533. }
  534. HTML(sprintf("%s<p><em>%d people commute to education from
  535. %s. %s</em></p>", hrstr, tot, shpname,
  536. pmp))
  537. } else {
  538. fdf <- edu_to %>% filter(edu_code == curshp)
  539. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  540. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  541. pmp <- ""
  542. if (ttype != 0) {
  543. pmp <- sprintf("Most common mode of transport: %s",
  544. edu.t[ttype])
  545. }
  546. HTML(sprintf("%s<p><em>%d people commute to education in
  547. %s. %s</em></p>", hrstr, tot, shpname,
  548. pmp))
  549. }
  550. }
  551. } else {
  552. shpname.0 <- shpf@data$SA22018__1[cursel == shpf@data$SA22018_V1]
  553. if (input$radioeduemp == "Employment") {
  554. if (input$radioinout == "res") {
  555. fdf <- work_simp %>% filter(res_code == cursel,
  556. work_code == curshp)
  557. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  558. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  559. pmp <- ""
  560. if (ttype != 0) {
  561. pmp <- sprintf("Most common mode of transport: %s",
  562. transport.t[ttype])
  563. }
  564. HTML(sprintf("%s<p><em>%d people commute to employment
  565. in %s from %s. %s</em></p>", hrstr, tot, shpname,
  566. shpname.0, pmp))
  567. } else {
  568. fdf <- work_simp %>% filter(work_code == cursel,
  569. res_code == curshp)
  570. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  571. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  572. pmp <- ""
  573. if (ttype != 0) {
  574. pmp <- sprintf("Most common mode of transport: %s",
  575. transport.t[ttype])
  576. }
  577. HTML(sprintf("%s<p><em>%d people commute to employment
  578. in %s from %s. %s</em></p>", hrstr, tot, shpname.0,
  579. shpname, pmp))
  580. }
  581. } else {
  582. if (input$radioinout == "res") {
  583. fdf <- edu_simp %>% filter(res_code == cursel,
  584. edu_code == curshp)
  585. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  586. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  587. pmp <- ""
  588. if (ttype != 0) {
  589. pmp <- sprintf("Most common mode of transport: %s",
  590. edu.t[ttype])
  591. }
  592. HTML(sprintf("%s<p><em>%d people commute to education
  593. in %s from %s. %s</em></p>", hrstr, tot, shpname,
  594. shpname.0, pmp))
  595. } else {
  596. fdf <- edu_simp %>% filter(edu_code == cursel,
  597. res_code == curshp)
  598. tot <- ifelse(nrow(fdf) == 0, 0, fdf$total)
  599. ttype <- ifelse(is.na(fdf$MAX) || nrow(fdf) == 0, 0, fdf$MAX)
  600. pmp <- ""
  601. if (ttype != 0) {
  602. pmp <- sprintf("Most common mode of transport: %s",
  603. edu.t[ttype])
  604. }
  605. HTML(sprintf("%s<p><em>%d people commute to education
  606. in %s from %s. %s</em></p>", hrstr, tot, shpname.0,
  607. shpname, pmp))
  608. }
  609. }
  610. }
  611. }
  612. })
  613. }
  614. # Run the application
  615. shinyApp(ui = ui, server = server)