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

app.R 25KB

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