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

app.R 26KB

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