Skip to content

Instantly share code, notes, and snippets.

@wfkwan
wfkwan / global.R
Created October 18, 2020 09:17
Connect to Amazon Elasticsearch Service
library(reticulate)
use_python("<PYTHONPATH>")
Connect Amazon Elasticsearch Service
py_awsauth <- import("requests_aws4auth")
credentials <- data.frame(list('<key>', '<secret>', '<region>', '<service>'))
authr <- py_awsauth$AWS4Auth(credentials$key, credentials$secret, credentials$region, credentials$service)
colnames(credentials) <- c("key", "secret", "region", "service")
hosts = c("<domain>:<port>")
@wfkwan
wfkwan / ui.R
Created October 18, 2020 07:56
Example box
box(
title = "Airline Flight Destination Weather",
sliderInput("wordcloud_size", "Size of the Wordcloud:",
min=0.1,
max=0.6,
value=0.6
),
wordcloud2Output("dest_weather", height = 200),
)
@wfkwan
wfkwan / server.R
Last active October 18, 2020 07:47
create word cloud using wordcloud2
output$dest_weather <- renderWordcloud2({
data <- setNames(annotate_agg1_es_result((es$search("kibana_sample_data_flights", dest_weather_query))),
c("weather", "ndest"))
wordcloud2(data, size = input$wordcloud_size)
})
output$delay_time <- renderPlot({
data <- setNames(annotate_agg2_es_result((es$search("kibana_sample_data_flights", delay_type_query))),
c("time", "airline", "ndelays"))
data$time <- as.POSIXct(data$time, format="%Y-%m-%dT%H:%M:%S")
data <- data[(data$time >= input$delay_time_range[1]) & (data$time <= input$delay_time_range[2]), ]
ggplot(data, aes(x=time, y=ndelays, fill=airline)) + geom_bar(stat="identity") + theme_minimal()
})
@wfkwan
wfkwan / server.R
Created October 18, 2020 06:50
Transform aggregated data and create pie plot using plotly
output$airline_flights <- renderPlotly({
data <- setNames(annotate_agg1_es_result((es$search("kibana_sample_data_flights", airline_carrier_query))),
c("airline", "nflights"))
colors <- c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')
plot_ly(data, labels = ~airline, values = ~nflights, type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1)),
@wfkwan
wfkwan / server.R
Created October 18, 2020 05:54
Data transform in R
annotate_agg1_es_result <- function(result, include_others=FALSE) {
name <- c()
count <- c()
aggregation_result <- result$aggregations[[1]]
if (include_others){
if ("key_as_string" %in% names(aggregation_result)) name <- c(name, aggregation_result$key_as_string)
else name <- c(name, aggregation_result$key)
name <- c(name, "Others")
count <- c(count, aggregation_result$sum_other_doc_count)
}
@wfkwan
wfkwan / global.R
Last active October 18, 2020 05:43
Demo Elasticsearch Queries
library(DBI)
library(reticulate)
use_python("<PYTHONPATH>")
# Connect Amazon Elasticsearch Service
# py_awsauth <- import("requests_aws4auth")
# credentials <- data.frame(list('<key>', '<secret>', '<region>', '<service>'))
# authr <- py_awsauth$AWS4Auth(credentials$key, credentials$secret, credentials$region, credentials$service)
# colnames(credentials) <- c("key", "secret", "region", "service")