From 0c073e3ccb598ee3152bf3ec5b1e560f1dca4454 Mon Sep 17 00:00:00 2001 From: AcuGIS Date: Sun, 1 Sep 2024 15:01:20 +0000 Subject: [PATCH] Update docs/source/r.rst --- docs/source/r.rst | 134 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 105 insertions(+), 29 deletions(-) diff --git a/docs/source/r.rst b/docs/source/r.rst index 62f5eb6..23456e9 100644 --- a/docs/source/r.rst +++ b/docs/source/r.rst @@ -143,7 +143,7 @@ The output should look as below: R Plotly =================================== -To create an R Leaflet Map, click on "Add New" button. +To create an R Plotly App, click on "Add New" button. .. image:: images/Add-Map.png @@ -156,14 +156,18 @@ Give your R code a Name and Description. Example -------------- -The three main components are the plotly, ggplot2, and htmlwidgets function. +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(plotly) - library(ggplot2) - library(htmlwidgets) + library(dplyr) + library(plotly) + library(htmlwidgets) # Your R Code Here @@ -176,36 +180,108 @@ An example of a Plotly app is included in the installation. Here, we add the RP .. code-block:: R - library(plotly) - library(ggplot2) - library(RPostgreSQL) - library(htmlwidgets) + #load library + library(dplyr) + library(plotly) + library(htmlwidgets) - conn <- RPostgreSQL::dbConnect("PostgreSQL", host = "localhost", dbname = "r_examples", user = "admin1", password = "4eA7hDlgYF") + #load data + df <- read.csv("graph.csv") - query_res <- dbGetQuery(conn, 'SELECT * FROM "sensor_readings";'); - sensor_readings <- as.data.frame(query_res); - # sensor_readings$timestamp <- as.Date(sensor_readings$timestamp) + #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) - 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') - ) - + #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) ===================================