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

app.R 24KB

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