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

app.R 25KB

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