QuartzMap/docs/source/r.rst

351 lines
7.7 KiB
ReStructuredText
Raw Normal View History

2024-08-25 21:12:30 +00:00
*******
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
2024-08-31 22:56:25 +00:00
R Publishing
2024-08-25 21:12:30 +00:00
===================================
2024-08-31 22:56:25 +00:00
There are three options for publising your R code.
2024-08-25 21:12:30 +00:00
Option 1: FTP.
------------
2024-08-31 22:56:25 +00:00
FTP Uploads are R files you have uploaded directly via FTP.
2024-08-25 21:12:30 +00:00
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
2024-08-31 22:56:25 +00:00
-----------------
2024-08-25 21:12:30 +00:00
Paste your R Leaflet code into the code box.
2024-08-31 22:39:35 +00:00
.. image:: images/R-Paste.png
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
R Leaflet
===================================
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
To create an R Leaflet Map, click on "Add New" button.
.. image:: images/Add-Map.png
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
FTP, Upload, or Paste your code.
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
Give your R code a Name and Description.
Example
--------------
2024-08-31 22:27:24 +00:00
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")
2024-08-31 22:56:25 +00:00
R Plotly
===================================
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
2024-08-25 21:12:30 +00:00
--------------
2024-08-31 22:59:57 +00:00
The three main components are the plotly, ggplot2, and htmlwidgets function.
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
.. code-block:: R
# Main libraries for Plotly
library(plotly)
library(ggplot2)
library(htmlwidgets)
# Your R Code Here
2024-08-25 21:12:30 +00:00
2024-08-31 22:56:25 +00:00
#saveWidget
htmlwidgets::saveWidget(as_widget(p), file="index.html")
2024-08-25 21:12:30 +00:00
2024-08-31 22:59:57 +00:00
An example of a Plotly app is included in the installation. Here, we add the RPostgreSQL library to connect to PostgreSQL.
2024-08-31 22:56:25 +00:00
.. code-block:: R
library(plotly)
library(ggplot2)
2024-08-31 22:59:57 +00:00
library(RPostgreSQL)
2024-08-31 22:56:25 +00:00
library(htmlwidgets)
2024-08-31 22:59:57 +00:00
conn <- RPostgreSQL::dbConnect("PostgreSQL", host = "localhost", dbname = "r_examples", user = "admin1", password = "4eA7hDlgYF")
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')
)
2024-08-31 22:56:25 +00:00
htmlwidgets::saveWidget(as_widget(p), file="index.html")
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
R Standard Plot (PNG)
===================================
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
To create an R Standard Plot (PNG) Map, click on "Add New" button.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Add-Map.png
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
FTP, Upload, or Paste your code.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Give your R code a Name and Description.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Example
--------------
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
The three main components are the R3port and html_plot function.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. code-block:: R
# Main libraries for Plotly
library(R3port)
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
# Your R Code Here
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
#output
html_plot(pl(), out="index.html")
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
An example of a Standard Plot (PNG) is included in the installation.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. code-block:: R
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
library(R3port)
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
set.seed(1919) # Create example data
x1 <- rnorm(1000)
y1 <- x1 + rnorm(1000)
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
group <- rbinom(1000, 1, 0.3) + 1 # Create group variable
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
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)
}
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
html_plot(pl(), out="index.html")
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
R App Options
===================================
Name
2024-08-25 21:12:30 +00:00
--------------
2024-08-31 23:09:55 +00:00
Give your R app a name. The name will appear as the map title on the dashboard.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Name-Desc.png
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Description
--------------
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
The Description is the text that will appear at the bottom of the map link
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Name.png
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Data Update (Cache)
2024-08-25 21:12:30 +00:00
--------------
2024-08-31 23:09:55 +00:00
For dynamic R apps that connect to a databases, you can set the update frequency
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Update.png
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
If you wish to set a custom interval, select custom:
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Update-2.png
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
When Updates are selected, this is the interval at which your app will be recompiled against the database.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
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
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Thumbnail Image:
2024-08-25 21:12:30 +00:00
--------------
2024-08-31 23:09:55 +00:00
Upload a thumbnail image for your map to be displayed on the home page.
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
.. image:: images/Thumbnail.png
2024-08-25 21:12:30 +00:00
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
2024-08-31 23:09:55 +00:00
1. Private
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
Private apps can be viewed by the user logging into your map portal or via Secure Share link (for temporary access)
2024-08-25 21:12:30 +00:00
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
2024-08-31 23:09:55 +00:00
2. Public
2024-08-25 21:12:30 +00:00
2024-08-31 23:09:55 +00:00
You can also tick the “Public” box to make your app public.
2024-08-25 21:12:30 +00:00
.. image:: images/public-users.jpg
If your map is “Public”, you can use the map url to display the map.
2024-08-31 23:09:55 +00:00
By default, the app is full screen. You can also use an iframe like below:
2024-08-25 21:12:30 +00:00
.. image:: images/public-map.png