« back

Setup

MySQL Configuration

Create a ~/.my.cnf file with the following contents:

[client]
user=<username copied from stat2:/etc/mysql/conf.d/analytics-research-client.cnf>
password=<password copied from stat2:/etc/mysql/conf.d/analytics-research-client.cnf>

SSH Tunnel

In a Terminal window, use the following command to create an SSH tunnel (assuming this SSH configuration):

ssh -N stat3 -L 3307:analytics-store.eqiad.wmnet:3306

Do not close that window/connection until you’re done.

Connecting in R

Note that we set a default for the connection chunk option in the setup chunk, so that it is not necessary to specify on each individual chunk.

library(RMySQL)
con <- dbConnect(MySQL(), host = "127.0.0.1", group = "client", dbname = "log", port = 3307)
knitr::opts_chunk$set(connection = "con")

Example

Refer to this documentation for details on the SQL engine and chunk options in RMarkdown.

start_date <- as.character(Sys.Date() - 1 - 7, format = "%Y%m%d")
end_date <- as.character(Sys.Date() - 1, format = "%Y%m%d")
SELECT
  DATE(LEFT(timestamp, 8)) AS `date`,
  CASE WHEN (
         INSTR(userAgent, 'Android') > 0
         OR INSTR(userAgent, 'Mobile') > 0
         OR INSTR(userAgent, 'iOS') > 0
         OR INSTR(userAgent, 'Phone') > 0
       ) THEN 'Mobile'
       ELSE 'Desktop' END AS device,
  CASE WHEN INSTR(event_destination, 'itunes.apple.com') > 0 THEN 'iOS App link'
       WHEN INSTR(event_destination, 'play.google.com') > 0 THEN 'Android app link'
       ELSE 'List of apps' END AS clicked,
  COUNT(*) AS clicks
FROM WikipediaPortal_15890769
WHERE
  LEFT(timestamp, 8) >= ?start_date AND LEFT(timestamp, 8) <= ?end_date
  AND event_cohort = 'baseline'
  AND event_event_type = 'clickthrough'
  AND event_section_used = 'other projects'
  AND (
    INSTR(event_destination, 'itunes.apple.com') > 0
    OR INSTR(event_destination, 'play.google.com') > 0
    OR event_destination = 'https://en.wikipedia.org/wiki/List_of_Wikipedia_mobile_applications'
  )
GROUP BY `date`, device, clicked;

For this chunk, we set output.var = "events" to to assign the results of the SQL query to an R data frame. Note that when the results of a SQL query are assigned to a data frame no records are printed within the document.

library(tidyverse)
events %>%
  group_by(device, clicked) %>%
  summarize(clicks = sum(clicks)) %>%
  spread(clicked, clicks)
dbDisconnect(con)
[1] TRUE
LS0tCnRpdGxlOiAiUXVlcnlpbmcgQW5hbHl0aWNzLVN0b3JlIExvY2FsbHkiCm91dHB1dDoKICBodG1sX25vdGVib29rOiAKICAgIHRoZW1lOiByZWFkYWJsZQogIGh0bWxfZG9jdW1lbnQ6IGRlZmF1bHQKLS0tCgojIyBTZXR1cAoKIyMjIE15U1FMIENvbmZpZ3VyYXRpb24KCkNyZWF0ZSBhICoqfi8ubXkuY25mKiogZmlsZSB3aXRoIHRoZSBmb2xsb3dpbmcgY29udGVudHM6CgpgYGAKW2NsaWVudF0KdXNlcj08dXNlcm5hbWUgY29waWVkIGZyb20gc3RhdDI6L2V0Yy9teXNxbC9jb25mLmQvYW5hbHl0aWNzLXJlc2VhcmNoLWNsaWVudC5jbmY+CnBhc3N3b3JkPTxwYXNzd29yZCBjb3BpZWQgZnJvbSBzdGF0MjovZXRjL215c3FsL2NvbmYuZC9hbmFseXRpY3MtcmVzZWFyY2gtY2xpZW50LmNuZj4KYGBgCgojIyMgU1NIIFR1bm5lbAoKSW4gYSBUZXJtaW5hbCB3aW5kb3csIHVzZSB0aGUgZm9sbG93aW5nIGNvbW1hbmQgdG8gY3JlYXRlIGFuIFtTU0ggdHVubmVsXShodHRwczovL2hlbHAudWJ1bnR1LmNvbS9jb21tdW5pdHkvU1NIL09wZW5TU0gvUG9ydEZvcndhcmRpbmcpIChhc3N1bWluZyBbdGhpcyBTU0ggY29uZmlndXJhdGlvbl0oaHR0cHM6Ly9vZmZpY2Uud2lraW1lZGlhLm9yZy93aWtpL0Rpc2NvdmVyeV9BbmFseXRpY3MjU1NIX0NvbmZpZ3VyYXRpb24pKToKCmBgYGJhc2gKc3NoIC1OIHN0YXQzIC1MIDMzMDc6YW5hbHl0aWNzLXN0b3JlLmVxaWFkLndtbmV0OjMzMDYKYGBgCgpEbyBub3QgY2xvc2UgdGhhdCB3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