Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample notebook for AGOL interaction and Sample Shiny Application #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added image/NJ_Demographics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

4,113 changes: 4,113 additions & 0 deletions notebooks/02-workflows/Search and Visualize ArcGIS Online Content.ipynb

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions shiny/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Shiny Applications
This folder contains sample [Shiny](https://shiny.rstudio.com/) applications that can use ArcGIS Online or ArcGIS Enterprise. You should be able to clone this repo and open up any of the Shiny projects in [RStudio](https://posit.co/download/rstudio-desktop/) and be off and running!

# Samples
Below are decriptions to some sample applications created using Shiny.

## [search-and-display-agol](https://github.com/gbrunner/R-Bridge-Tutorial-Notebooks/blob/master/shiny/search-and-display-agol)
This application searches ArcGIS Online or ArcGIS Enterprise for feature services, reads them as GeoJSON, and adds them to a Leaflet map. It uses a sample R package called ```arcgis``` hosted at https://github.com/gbrunner/arcgis-r-demos to interface with ArcGIS Online or ArcGIS Enterprise via the ArcGIS REST API and the [```httr```](https://cran.r-project.org/web/packages/httr/index.html) package. The UI contains a label, a search text box, a search button, and a check box for searching only your content or all of AGOL.
Before you run the application, make sure to update the URL, username, and password in the code.
```
# Create a new GIS object
gis <- new("GIS",
url="https://learngis.maps.arcgis.com/",
username = "gbrunner_LearnGIS",
password = "####")
```
Also, conider updating the default search term to something other than [**NJ_Demographics**](https://github.com/gbrunner/R-Bridge-Tutorial-Notebooks/blob/master/shiny/search-and-display-agol/app.R#L43).
```
textInput("searchTerm", "Search for Feature Layers", value = "NJ_Demographics",
```
Here are a few screenshots of the application.
### Search Term: NJ Demographics
![](search_display_nj_demographics.png)
### Search Term: Pasadena Traffic
![](search_display_pasadena_traffic.png)
### Search Term: Philadelphia Crime
![](search_display_philly_crime.png)
Binary file added shiny/search-and-display-agol/.RData
Binary file not shown.
5 changes: 5 additions & 0 deletions shiny/search-and-display-agol/.Rhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
1 change: 1 addition & 0 deletions shiny/search-and-display-agol/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sortOrder": [
{
"columnIndex": 2,
"ascending": true
}
],
"path": "~/IPython Notebooks/R-Bridge-Tutorial-Notebooks/shiny/search-and-display-agol"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"activeTab": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"left": {
"splitterpos": 390,
"topwindowstate": "NORMAL",
"panelheight": 937,
"windowheight": 975
},
"right": {
"splitterpos": 585,
"topwindowstate": "NORMAL",
"panelheight": 937,
"windowheight": 975
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"TabSet1": 0,
"TabSet2": 0,
"TabZoom": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
activeEnvironmentName="package:arcgis"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"active_set":"","sets":[]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "4B2A2995",
"path": "~/IPython Notebooks/R-Bridge-Tutorial-Notebooks/shiny/search-and-display-agol/app.R",
"project_path": "app.R",
"type": "r_source",
"hash": "3471929845",
"contents": "",
"dirty": false,
"created": 1671648782342.0,
"source_on_save": false,
"relative_order": 1,
"properties": {
"source_window_id": "",
"Source": "Source",
"cursorPosition": "59,29",
"scrollLine": "44"
},
"folds": "",
"lastKnownWriteTime": 1671650062,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1671650062790,
"read_only": false,
"read_only_alternatives": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
if(!require(leaflet)){
install.packages("leaflet")
library(leaflet)
}
if(!require(shiny)){
install.packages("shiny")
library(shiny)
}
if(!require(RColorBrewer)){
install.packages("RColorBrewer")
library(RColorBrewer)
}
if(!require(devtools)){
install.packages("devtools")
library(devtools)
}
# A custom package for interfacing with ArcGIS Online or Enterprise
if(!require(arcgis)){
devtools::install_github("gbrunner/arcgis-r-demos",
force=TRUE)
library(arcgis)
}
if(!require(httr)){
install.packages("httr")
library(httr)
}
if(!require(geojsonio)){
install.packages("geojsonio")
library(geojsonio)
}
if(!require(jsonlite)){
install.packages("jsonlite")
library(jsonlite)
}

# Layout for the Shiny UI
# Contains a text label, an editble text box, a search button, and a
# check box to search for only content in your account
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
textInput("searchTerm", "Search for Feature Layers", value = "NJ_Demographics",
width = NULL, placeholder = NULL
),
actionButton("searchButton", "Search"),
checkboxInput("owner", "Your Content?", TRUE)
)
)

# The app
server <- function(input, output, session) {

v <- reactiveValues(data = NULL)

# Create a new GIS object
gis <- new("GIS",
url="https://learngis.maps.arcgis.com/",
username = "gbrunner_LearnGIS",
password = "####")

# Log into ArcGIS Online
gis <- login(gis)

observeEvent(input$searchButton, {
v$col <- 1
ind <- 1

# Create the ArcGIS search query
if (input$owner==TRUE) {
# Search only my content
search_term <- paste("owner:",gis@username, " AND ", input$searchTerm," AND type:feature service", set="")
}
else{
# Search all ArcGIS Online
search_term <- paste(input$searchTerm," AND type:feature service")
}
print(input$searchTerm)

# Run the search
items <- search_gis(gis, search_term)
print(items$results)

# Convert results to spatial dataframe
v$spdf <- get_sdf(gis, items$results[[1]], 0)

# Infer column in dataframe to render on map
ind <- get_column_to_render(v$spdf) #v$spdf$TOTPOP_CY #
v$col <- v$spdf@data[,ind]
v$colname <- colnames(v$spdf@data)[ind]
print(v$colname)

}, ignoreNULL = FALSE)

pal <- colorNumeric("viridis", NULL)

# https://rstudio.github.io/leaflet/markers.html

output$map <- renderLeaflet({
if (is.null(v$spdf)){
leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap)
}
else{
if (class(v$spdf) == "SpatialPolygonsDataFrame") {
m <- add_polygons_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialPointsDataFrame") {
m <- add_points_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialLinesDataFrame") {
m <- add_polylines_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialMultiPointsDataFrame") {
m <- add_multipoints_layer(v$spdf, v$colname)
}
else {
leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addMarkers()
}
}
})


}

# Run the app
shinyApp(ui, server)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"source_window_id": "",
"Source": "Source",
"cursorPosition": "59,29",
"scrollLine": "44"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
~%2FIPython%20Notebooks%2FR-Bridge-Tutorial-Notebooks%2Fshiny%2Fsearch-and-display-agol%2Fapp.R="137732D4"
129 changes: 129 additions & 0 deletions shiny/search-and-display-agol/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
if(!require(leaflet)){
install.packages("leaflet")
library(leaflet)
}
if(!require(shiny)){
install.packages("shiny")
library(shiny)
}
if(!require(RColorBrewer)){
install.packages("RColorBrewer")
library(RColorBrewer)
}
if(!require(devtools)){
install.packages("devtools")
library(devtools)
}
# A custom package for interfacing with ArcGIS Online or Enterprise
if(!require(arcgis)){
devtools::install_github("gbrunner/arcgis-r-demos",
force=TRUE)
library(arcgis)
}
if(!require(httr)){
install.packages("httr")
library(httr)
}
if(!require(geojsonio)){
install.packages("geojsonio")
library(geojsonio)
}
if(!require(jsonlite)){
install.packages("jsonlite")
library(jsonlite)
}

# Layout for the Shiny UI
# Contains a text label, an editble text box, a search button, and a
# check box to search for only content in your account
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
textInput("searchTerm", "Search for Feature Layers", value = "NJ_Demographics",
width = NULL, placeholder = NULL
),
actionButton("searchButton", "Search"),
checkboxInput("owner", "Your Content?", TRUE)
)
)

# The app
server <- function(input, output, session) {

v <- reactiveValues(data = NULL)

# Create a new GIS object
gis <- new("GIS",
url="https://learngis.maps.arcgis.com/",
username = "gbrunner_LearnGIS",
password = "####")

# Log into ArcGIS Online
gis <- login(gis)

observeEvent(input$searchButton, {
v$col <- 1
ind <- 1

# Create the ArcGIS search query
if (input$owner==TRUE) {
# Search only my content
search_term <- paste("owner:",gis@username, " AND ", input$searchTerm," AND type:feature service", set="")
}
else{
# Search all ArcGIS Online
search_term <- paste(input$searchTerm," AND type:feature service")
}
print(input$searchTerm)

# Run the search
items <- search_gis(gis, search_term)
print(items$results)

# Convert results to spatial dataframe
v$spdf <- get_sdf(gis, items$results[[1]], 0)

# Infer column in dataframe to render on map
ind <- get_column_to_render(v$spdf) #v$spdf$TOTPOP_CY #
v$col <- v$spdf@data[,ind]
v$colname <- colnames(v$spdf@data)[ind]
print(v$colname)

}, ignoreNULL = FALSE)

pal <- colorNumeric("viridis", NULL)

# https://rstudio.github.io/leaflet/markers.html

output$map <- renderLeaflet({
if (is.null(v$spdf)){
leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap)
}
else{
if (class(v$spdf) == "SpatialPolygonsDataFrame") {
m <- add_polygons_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialPointsDataFrame") {
m <- add_points_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialLinesDataFrame") {
m <- add_polylines_layer(v$spdf, v$colname)
}
else if (class(v$spdf) == "SpatialMultiPointsDataFrame") {
m <- add_multipoints_layer(v$spdf, v$colname)
}
else {
leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addMarkers()
}
}
})


}

# Run the app
shinyApp(ui, server)
Loading