Update docs/source/r.rst
This commit is contained in:
parent
8088614f0a
commit
0c073e3ccb
|
@ -143,7 +143,7 @@ The output should look as below:
|
||||||
R Plotly
|
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
|
.. image:: images/Add-Map.png
|
||||||
|
|
||||||
|
@ -156,14 +156,18 @@ Give your R code a Name and Description.
|
||||||
Example
|
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
|
.. code-block:: R
|
||||||
|
|
||||||
# Main libraries for Plotly
|
# Main libraries for Plotly
|
||||||
library(plotly)
|
library(dplyr)
|
||||||
library(ggplot2)
|
library(plotly)
|
||||||
library(htmlwidgets)
|
library(htmlwidgets)
|
||||||
|
|
||||||
# Your R Code Here
|
# 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
|
.. code-block:: R
|
||||||
|
|
||||||
library(plotly)
|
#load library
|
||||||
library(ggplot2)
|
library(dplyr)
|
||||||
library(RPostgreSQL)
|
library(plotly)
|
||||||
library(htmlwidgets)
|
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";');
|
#create map
|
||||||
sensor_readings <- as.data.frame(query_res);
|
p <- plot_geo(df, locationmode = 'world') %>%
|
||||||
# sensor_readings$timestamp <- as.Date(sensor_readings$timestamp)
|
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) %>%
|
#export as html file
|
||||||
layout(
|
htmlwidgets::saveWidget(p, file = "index.html")
|
||||||
plot_bgcolor='#e5ecf6',
|
|
||||||
xaxis = list( matches='x',
|
|
||||||
zerolinecolor = '#ffff',
|
|
||||||
zerolinewidth = 2,
|
The output should look at below:
|
||||||
gridcolor = 'ffff',
|
|
||||||
range = list( min(sensor_readings$timestamp),
|
|
||||||
max(sensor_readings$timestamp))
|
.. image:: images/R-Animated.png
|
||||||
),
|
|
||||||
yaxis = list(
|
|
||||||
zerolinecolor = '#ffff',
|
|
||||||
zerolinewidth = 2,
|
|
||||||
gridcolor = 'ffff')
|
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")
|
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)
|
R Standard Plot (PNG)
|
||||||
===================================
|
===================================
|
||||||
|
|
Loading…
Reference in New Issue