432 lines
9.3 KiB
ReStructuredText
432 lines
9.3 KiB
ReStructuredText
*******
|
|
R
|
|
*******
|
|
|
|
QuartzMap supports creating of a number of R HTML applications:
|
|
|
|
1. R Leaflet: https://github.com/rstudio/leaflet/
|
|
|
|
2. Plotly: https://plotly.com/r/
|
|
|
|
3. Standard Plots
|
|
|
|
.. contents:: Table of Contents
|
|
|
|
|
|
R Publishing
|
|
===================================
|
|
|
|
There are three options for publising your R code.
|
|
|
|
Option 1: FTP.
|
|
------------
|
|
|
|
FTP Uploads are R files you have uploaded directly via FTP.
|
|
|
|
It can also maps you uploaded via any FTP client.
|
|
|
|
.. image:: images/Map-2.png
|
|
|
|
|
|
Option 2: Archive (Upload)
|
|
------------
|
|
|
|
Archive is a zipped archive file you can upload.
|
|
|
|
|
|
.. image:: images/Map-3.png
|
|
|
|
|
|
Option 3: Paste
|
|
-----------------
|
|
|
|
Paste your R Leaflet code into the code box.
|
|
|
|
|
|
.. image:: images/R-Paste.png
|
|
|
|
|
|
|
|
R Leaflet
|
|
===================================
|
|
|
|
To create an R Leaflet Map, click on "Add New" button.
|
|
|
|
.. image:: images/Add-Map.png
|
|
|
|
|
|
FTP, Upload, or Paste your code.
|
|
|
|
Give your R code a Name and Description.
|
|
|
|
|
|
Example
|
|
--------------
|
|
|
|
The two main components are the libraries and saveWidget function
|
|
|
|
.. code-block:: R
|
|
|
|
#libraries
|
|
library(leaflet)
|
|
library(leaflet.extras)
|
|
require(sf)
|
|
library(htmlwidgets)
|
|
|
|
# Your R Code Here
|
|
|
|
#saveWidget
|
|
saveWidget(m, file = "index.html")
|
|
|
|
An example Choropleth map is included in the installation.
|
|
|
|
|
|
.. code-block:: R
|
|
|
|
library(leaflet)
|
|
library(leaflet.extras)
|
|
require(sf)
|
|
library(htmlwidgets)
|
|
|
|
|
|
# From https://leafletjs.com/examples/choropleth/us-states.js
|
|
states <- sf::read_sf("https://rstudio.github.io/leaflet/json/us-states.geojson")
|
|
|
|
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
|
|
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)
|
|
|
|
labels <- sprintf(
|
|
"<strong>%s</strong><br/>%g people / mi<sup>2</sup>",
|
|
states$name, states$density
|
|
) %>% lapply(htmltools::HTML)
|
|
|
|
m <- leaflet(states) %>%
|
|
setView(-96, 37.8, 4) %>%
|
|
addPolygons(
|
|
fillColor = ~pal(density),
|
|
weight = 2,
|
|
opacity = 1,
|
|
color = "white",
|
|
dashArray = "3",
|
|
fillOpacity = 0.7,
|
|
highlightOptions = highlightOptions(
|
|
weight = 5,
|
|
color = "#666",
|
|
dashArray = "",
|
|
fillOpacity = 0.7,
|
|
bringToFront = TRUE),
|
|
label = labels,
|
|
labelOptions = labelOptions(
|
|
style = list("font-weight" = "normal", padding = "3px 8px"),
|
|
textsize = "15px",
|
|
direction = "auto")) %>%
|
|
addLegend(pal = pal, values = ~density, opacity = 0.7, title = NULL,
|
|
position = "bottomright") %>%
|
|
addTiles(group="OpenStreetMap") %>%
|
|
addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery") %>%
|
|
addLayersControl(baseGroups=c("OpenStreetMap", "Esri World Imagery"), options=layersControlOptions(collapsed=FALSE)) %>%
|
|
addMeasurePathToolbar(options = measurePathOptions(imperial = FALSE, showDistances = TRUE)) %>%
|
|
addDrawToolbar(
|
|
targetGroup = "draws",
|
|
editOptions = editToolbarOptions(
|
|
selectedPathOptions = selectedPathOptions()))
|
|
|
|
saveWidget(m, file = "index.html")
|
|
|
|
|
|
The output should look as below:
|
|
|
|
.. image:: images/R-Choropleth.png
|
|
|
|
|
|
|
|
R Plotly
|
|
===================================
|
|
|
|
To create an R Plotly App, click on "Add New" button.
|
|
|
|
.. image:: images/Add-Map.png
|
|
|
|
|
|
FTP, Upload, or Paste your code.
|
|
|
|
Give your R code a Name and Description.
|
|
|
|
|
|
Example
|
|
--------------
|
|
|
|
The example is animated Plotyl map with Play button.
|
|
|
|
The three main components in this example are the plotly, dplyr, and htmlwidgets function.
|
|
|
|
|
|
|
|
.. code-block:: R
|
|
|
|
# Main libraries for Plotly
|
|
library(dplyr)
|
|
library(plotly)
|
|
library(htmlwidgets)
|
|
|
|
# Your R Code Here
|
|
|
|
#saveWidget
|
|
htmlwidgets::saveWidget(as_widget(p), file="index.html")
|
|
|
|
|
|
An example of a Plotly app is included in the installation. Here, we add the RPostgreSQL library to connect to PostgreSQL.
|
|
|
|
|
|
.. code-block:: R
|
|
|
|
#load library
|
|
library(dplyr)
|
|
library(plotly)
|
|
library(htmlwidgets)
|
|
|
|
#load data
|
|
df <- read.csv("graph.csv")
|
|
|
|
#create map
|
|
p <- plot_geo(df, locationmode = 'world') %>%
|
|
add_trace( z = ~df$new_cases_per_million, locations = df$code, frame=~df$start_of_week, color = ~df$new_cases_per_million)
|
|
|
|
#export as html file
|
|
htmlwidgets::saveWidget(p, file = "index.html")
|
|
|
|
|
|
|
|
The output should look at below:
|
|
|
|
|
|
.. image:: images/R-Animated.png
|
|
|
|
|
|
|
|
|
|
R Plotly Dynamic Data
|
|
===================================
|
|
|
|
To create an R Plotyl App with Dynamic Data, click on "Add New" button.
|
|
|
|
.. image:: images/Add-Map.png
|
|
|
|
|
|
FTP, Upload, or Paste your code.
|
|
|
|
Give your R code a Name and Description.
|
|
|
|
|
|
Example
|
|
--------------
|
|
|
|
The example is animated Plotyl map with Play button.
|
|
|
|
The main components in this example are the plotly, ggplot2, RPostgreSQL, and htmlwidgets function.
|
|
|
|
|
|
|
|
.. code-block:: R
|
|
|
|
# Main libraries for Plotly
|
|
library(plotly)
|
|
library(ggplot2)
|
|
library(RPostgreSQL)
|
|
library(htmlwidgets)
|
|
|
|
# Your R Code Here
|
|
|
|
#saveWidget
|
|
htmlwidgets::saveWidget(as_widget(p), file="index.html")
|
|
|
|
|
|
An example of a Plotly app is included in the installation. Here, we add the RPostgreSQL library to connect to PostgreSQL.
|
|
|
|
|
|
.. code-block:: R
|
|
|
|
library(plotly)
|
|
library(ggplot2)
|
|
library(RPostgreSQL)
|
|
library(htmlwidgets)
|
|
|
|
conn <- RPostgreSQL::dbConnect("PostgreSQL", host = "localhost", dbname = "$DB_NAME", user = "$DB_USER", password = "$DB_PASS")
|
|
|
|
query_res <- dbGetQuery(conn, 'SELECT * FROM "sensor_readings";');
|
|
sensor_readings <- as.data.frame(query_res);
|
|
# sensor_readings$timestamp <- as.Date(sensor_readings$timestamp)
|
|
|
|
p <- plot_ly(sensor_readings, x=~timestamp, y=~humidity, text=~paste("Sensor: ", sensor_name), mode="markers", color=~humidity, size=~humidity) %>%
|
|
layout(
|
|
plot_bgcolor='#e5ecf6',
|
|
xaxis = list( matches='x',
|
|
zerolinecolor = '#ffff',
|
|
zerolinewidth = 2,
|
|
gridcolor = 'ffff',
|
|
range = list( min(sensor_readings$timestamp),
|
|
max(sensor_readings$timestamp))
|
|
),
|
|
yaxis = list(
|
|
zerolinecolor = '#ffff',
|
|
zerolinewidth = 2,
|
|
gridcolor = 'ffff')
|
|
)
|
|
|
|
htmlwidgets::saveWidget(as_widget(p), file="index.html")
|
|
|
|
|
|
|
|
The output should look at below:
|
|
|
|
|
|
.. image:: images/R-Sensor.png
|
|
|
|
|
|
R Standard Plot (PNG)
|
|
===================================
|
|
|
|
To create an R Standard Plot (PNG) Map, click on "Add New" button.
|
|
|
|
.. image:: images/Add-Map.png
|
|
|
|
|
|
FTP, Upload, or Paste your code.
|
|
|
|
Give your R code a Name and Description.
|
|
|
|
|
|
Example
|
|
--------------
|
|
|
|
The three main components are the R3port and html_plot function.
|
|
|
|
.. code-block:: R
|
|
|
|
# Main libraries for Plotly
|
|
library(R3port)
|
|
|
|
# Your R Code Here
|
|
|
|
#output
|
|
html_plot(pl(), out="index.html")
|
|
|
|
|
|
An example of a Standard Plot (PNG) is included in the installation.
|
|
|
|
.. code-block:: R
|
|
|
|
library(R3port)
|
|
|
|
set.seed(1919) # Create example data
|
|
x1 <- rnorm(1000)
|
|
y1 <- x1 + rnorm(1000)
|
|
|
|
group <- rbinom(1000, 1, 0.3) + 1 # Create group variable
|
|
|
|
pl <- function() {
|
|
plot(x1, y1, # Create plot with groups
|
|
main = "This is my Plot",
|
|
xlab = "X-Values",
|
|
ylab = "Y-Values",
|
|
col = group,
|
|
pch = group)
|
|
|
|
legend("topleft", # Add legend to plot
|
|
legend = c("Group 1", "Group 2"),
|
|
col = 1:2,
|
|
pch = 1:2)
|
|
}
|
|
|
|
html_plot(pl(), out="index.html")
|
|
|
|
|
|
|
|
R App Options
|
|
===================================
|
|
|
|
Name
|
|
--------------
|
|
|
|
Give your R app a name. The name will appear as the map title on the dashboard.
|
|
|
|
.. image:: images/Name-Desc.png
|
|
|
|
|
|
Description
|
|
--------------
|
|
|
|
The Description is the text that will appear at the bottom of the map link
|
|
|
|
.. image:: images/Name.png
|
|
|
|
|
|
|
|
Data Update (Cache)
|
|
--------------
|
|
|
|
For dynamic R apps that connect to a databases, you can set the update frequency
|
|
|
|
|
|
.. image:: images/Update.png
|
|
|
|
If you wish to set a custom interval, select custom:
|
|
|
|
.. image:: images/Update-2.png
|
|
|
|
|
|
When Updates are selected, this is the interval at which your app will be recompiled against the database.
|
|
|
|
If you have enabled Updates, but wish to Update immediately, you can do so by clicking the Clear Cache icon on the Map page:
|
|
|
|
.. image:: images/clear-cache.png
|
|
|
|
|
|
Thumbnail Image:
|
|
--------------
|
|
|
|
Upload a thumbnail image for your map to be displayed on the home page.
|
|
|
|
.. image:: images/Thumbnail.png
|
|
|
|
|
|
Info Box.
|
|
--------------
|
|
|
|
The InfoBox is a modal information box you can display to map users.
|
|
|
|
.. image:: images/Info-Box.png
|
|
|
|
|
|
Security
|
|
--------------
|
|
|
|
Maps can be Private or Public.
|
|
|
|
The Security section is where you assign permissions to your map.
|
|
|
|
Security is Group based, so any users belonging to the Group will be able to view the map.
|
|
|
|
.. image:: images/users-3.jpg
|
|
|
|
1. Private
|
|
|
|
Private apps can be viewed by the user logging into your map portal or via Secure Share link (for temporary access)
|
|
|
|
For example, since we gave access to the Group containing user Jane Doe, when she logs in she will see only the two maps she has permissions to
|
|
|
|
.. image:: images/users-2.jpg
|
|
|
|
2. Public
|
|
|
|
You can also tick the “Public” box to make your app public.
|
|
|
|
.. image:: images/public-users.jpg
|
|
|
|
If your map is “Public”, you can use the map url to display the map.
|
|
|
|
By default, the app is full screen. You can also use an iframe like below:
|
|
|
|
.. image:: images/public-map.png
|
|
|