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

app.R 26KB

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