# Copyright (C) 2022-2022 Koen Derks # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . .shiny_ui <- shiny::fluidPage( shiny::tags$head(shiny::HTML("R City Views")), shiny::fluidRow(align = "center", shiny::titlePanel(shiny::HTML("

R City Views

"))), shiny::hr(), shiny::fluidRow( shiny::column(3, style = "border: 4px double black;", shiny::fluidRow(align = "center", shiny::HTML("

Instructions

")), shiny::HTML("

Step 1: Drag the map, click Randomize, or use the search tool to find the area that you want to render.

Step 2: Adjust the name of the city under City name and the country under Country.

Step 3: Click the Preview button to preview the map in the panel below (this can take a while depending on the size of the area).

Step 4: Once the image is displayed you can click the Download button to export it as an .svg file, or right-click + save as on the image to export in .png format.

Note: Previewing a large area may disconnect you from the app due to the data limit for free Shiny subscriptions. Check out rcityviews on GitHub for info on how to run this app locally.

"), ), shiny::column(4, offset = 1, shiny::fluidRow(align = "center", leaflet::leafletOutput(outputId = "osm", width = "500px", height = "410px")) ), shiny::column(3, offset = 1, shiny::fluidRow(align = "center", shiny::textInput(inputId = "plotTitle", label = "City name", value = "")), shiny::fluidRow(align = "center", shiny::textInput(inputId = "countryTitle", label = "Country", value = "")), shiny::fluidRow( align = "center", shiny::column(width = 6, shiny::selectInput(inputId = "theme", label = "Theme", choices = c("Vintage", "Modern", "Bright", "Delftware", "Comic", "Rouge", "Original", "Midearth", "Batik", "Vice"))), shiny::column(width = 6, shiny::selectInput(inputId = "border", label = "Border", choices = c("None", "Circle", "Rhombus", "Square", "Hexagon", "Octagon", "Decagon"))) ), shiny::fluidRow( align = "center", shiny::column(width = 6, shiny::checkboxInput(inputId = "legend", label = "Legend", value = FALSE)), shiny::column(width = 6, shiny::checkboxInput(inputId = "license", label = "License", value = TRUE)) ), shiny::fluidRow( align = "center", shiny::actionButton(inputId = "randomize", label = "Randomize", icon = shiny::icon("dice"), style = "color: #000000; background-color: #edb92e; border-color: #000000"), shiny::actionButton(inputId = "run", label = "Preview", icon = shiny::icon("camera"), style = "color: #000000; background-color: #f85931; border-color: #000000"), shiny::downloadButton(outputId = "downloadPlot", label = "Download", style = "color: #000000; background-color: #009989; border-color: #000000") ), shiny::HTML("
"), shiny::fluidRow( align = "center", shiny::tags$a(href = "https://koenderks.shinyapps.io/rcityviews/", "Tweet", class = "twitter-share-button"), shiny::includeScript("http://platform.twitter.com/widgets.js") ) ) ), shiny::hr(), shiny::column(1), shiny::mainPanel( width = 10, style = "border: 4px double black;", shiny::fluidRow(align = "center", shiny::plotOutput(outputId = "plotObject", width = "1055px", height = "1055px")) ), shiny::column(1) ) .shiny_server <- function(input, output, session) { city <- rcityviews:::.randomCity(sample.int(100000, size = 1)) shiny::updateTextInput(session, "plotTitle", value = city[["name"]]) shiny::updateTextInput(session, "countryTitle", value = city[["country"]]) output[["osm"]] <- leaflet::renderLeaflet({ leaflet::leaflet() |> leaflet::addTiles() |> leaflet::setView(lng = city[["long"]], lat = city[["lat"]], zoom = 14) |> leaflet.extras::addSearchOSM() }) shiny::observeEvent(input[["randomize"]], { city <- rcityviews:::.randomCity(sample.int(100000, size = 1)) shiny::updateTextInput(session, "plotTitle", value = city[["name"]]) shiny::updateTextInput(session, "countryTitle", value = city[["country"]]) output[["osm"]] <- leaflet::renderLeaflet({ leaflet::leaflet() |> leaflet::addTiles() |> leaflet::setView(lng = city[["long"]], lat = city[["lat"]], zoom = 14) |> leaflet.extras::addSearchOSM() }) }) output[["plotObject"]] <- shiny::renderPlot(NULL) shiny::observeEvent(input[["run"]], { themeOptions <- rcityviews:::.themeOptions(tolower(input[["theme"]])) long <- stats::median(c(input[["osm_bounds"]][["east"]], input[["osm_bounds"]][["west"]])) lat <- stats::median(c(input[["osm_bounds"]][["north"]], input[["osm_bounds"]][["south"]])) city <- data.frame("name" = input[["plotTitle"]], "country" = input[["countryTitle"]], lat = lat, long = long) boundaries <- rcityviews:::.getBoundaries(city, tolower(input[["border"]]), input = input) bbox <- osmdata::opq(bbox = boundaries[["panel"]], timeout = 1000) try <- try({ shiny::withProgress(message = "Creating preview", value = 0, min = 0, max = 1, expr = { image <- rcityviews:::.buildCity( city = city, bbox = bbox, zoom = 0.0225 / (city[["lat"]] - boundaries[["panel"]][2]), panel = boundaries[["panel"]], themeOptions = themeOptions, border = tolower(input[["border"]]), halftone = NULL, places = 0, legend = input[["legend"]], cropped = boundaries[["cropped"]], borderPoints = boundaries[["borderPoints"]], verbose = FALSE, license = input[["license"]], ticks = 61, shiny = TRUE ) }) }) if ("try-error" %in% class(try)) { if (try[[1]] == "Error in resp_abort(resp, error_body(req, resp)) : \n HTTP 504 Gateway Timeout.\n") { shiny::showNotification("The overpass server is not able to respond to your request, traffic might be too high.", type = "error", duration = NULL) } else { shiny::showNotification(try[[1]], type = "error", duration = NULL) } return() } output[["plotObject"]] <- shiny::renderPlot(image) output[["downloadPlot"]] <- shiny::downloadHandler( filename = function() { paste0(input[["plotTitle"]], ".svg") }, content = function(file) { ggplot2::ggsave(file, plot = image, height = 500, width = 500, units = "mm") } ) }) } shiny::shinyApp(ui = .shiny_ui, server = .shiny_server)