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

app.R 26KB

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