Member-only story
BigQuery Basics: Connecting to BigQuery in R
If you’re working with data stored in BigQuery and R is your thing, luckily it is very simple to work with BigQuery data in R.
Basic Syntax
First, we need to install (if it is not installed already) and load the bigrquery package.
install.packages("bigrquery")
library(bigrquery)
Then, to make it easier to reference throughout, I like to store my BigQuery project name in a variable.
project <- "your_bigquery_project_name"
Afterwards, store the SQL query you need for extracting data in a variable (I used the variable “sql”).
sql <- "SELECT columnA,
COUNT(*)
FROM table
GROUP by columnA"
Then, run the query and store the query’s output in an r dataframe with the following code.
tb <- bq_project_query(billing, sql)
df <- bq_table_download(tb)
The first time you run the code, you will need to verify your account information (the account that has BigQuery permissions). This will be a super simple Google login screen that gives you an authentication code to enter into the R console.
Once verification is complete, the output of your query will be stored in the dataframe “df” which you…