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

app.R 26KB

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