eval_viz <- FALSE if(Sys.getenv("GLOBAL_EVAL") != "") eval_viz <- Sys.getenv("GLOBAL_EVAL")
library(connections) library(dplyr) library(dbplyr) library(dbplot) library(ggplot2) library(leaflet) library(config)
Practice pushing the calculations to the database
Load the connections
, dplyr
, dbplyr
, and config
libraries
r
library(connections)
library(dplyr)
library(dbplyr)
library(config)
Use connection_open()
to open a Database connection
r
con <- connection_open(
RPostgres::Postgres(),
host = "localhost",
user = get("user", config = "dev"),
password = get("pwd", config = "dev"),
port = 5432,
dbname = "postgres",
bigint = "integer"
)
Use tbl()
to create a pointer to the v_orders table
r
orders <-
Use collect()
bring back the aggregated results into a "pass-through" variable called by_year
r
by_year <- orders %>%
count(date_year) %>%
collect()
Preview the by_year
variable
```r
```
Load the ggplot2
library
r
library(ggplot2)
Plot results using ggplot2
r
ggplot(by_year) +
geom_col(aes(date_year, n))
Using the code in this section, create a single piped code set which also creates the plot ```r
```
Practice going from dplyr
to ggplot2
without using pass-through variable, great for EDA
Summarize the order totals in a new variable called sales
r
orders %>%
summarise(sales = sum(order_total))
Summarize the order totals grouped by date_year
in a new variable called sales
r
orders %>%
group_by(date_year) %>%
summarise(sales = sum(order_total))
Summarize the order totals grouped by date_year
in a new variable called sales
and plot the results
```r
```
Switch the calculation to reflect the average of the order sale total ```r
```
Use the dbplot
package to easily create a histogram
Load the dbplot
package
r
library(dbplot)
Use the dbplot_histogram()
to build the histogram
r
orders %>%
dbplot_histogram(order_total)
Adjust the binwidth
to 10
```r
```
Use dbplot
's raster graph
Use a dbplot_raster()
to visualize order_qty
versus order_total
r
orders %>%
dbplot_raster(order_qty, order_total)
Change the plot's resolution to 10 ```r
```
compute
functionsUse the db_compute_raster()
function to get the underlying results that feed the plot
r
locations <- orders %>%
db_compute_raster2(customer_lon, customer_lat, resolution = 10)
Preview the locations
variable
r
locations
Load the leaflet
library
r
library(leaflet)
Pipe location
into the leaflet()
function, and then pipe that into the addTiles()
function
r
locations %>%
leaflet() %>%
addTiles()
Add the addRectangles()
function using the longitude and latitude variables
locations %>% leaflet() %>% addTiles() %>% addRectangles( ~customer_lon, ~customer_lat, ~customer_lon_2, ~customer_lat_2 )
fillOpacity
argument to the addRectangles()
step, use n()
as the value for itlocations %>% leaflet() %>% addTiles() %>% addRectangles( ~customer_lon, ~customer_lat, ~customer_lon_2, ~customer_lat_2, fillOpacity = ~`n()` )
fillOpacity
to be calculated as a percentage against the maximum number of orderslocations %>% leaflet() %>% addTiles() %>% addRectangles( ~customer_lon, ~customer_lat, ~customer_lon_2, ~customer_lat_2, fillOpacity = ~(`n()` / max(`n()`)) )
popup
argument with the following instruction as its value: ~paste0("<p>No of orders: ",
n(),"</p>")
locations %>% leaflet() %>% addTiles() %>% addRectangles( ~customer_lon, ~customer_lat, ~customer_lon_2, ~customer_lat_2, fillOpacity = ~(`n()` / max(`n()`)), popup = ~paste0("<p>No of orders: ", `n()`,"</p>") )
connection_close
r
connection_close(con)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.