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

app.R 24KB

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