Skip to content

Instantly share code, notes, and snippets.

@DustinAlandzes
Last active October 29, 2019 03:21
Show Gist options
  • Save DustinAlandzes/850ec130f83a13ef240a4e312a598b12 to your computer and use it in GitHub Desktop.
Save DustinAlandzes/850ec130f83a13ef240a4e312a598b12 to your computer and use it in GitHub Desktop.
How to load bigquery data with r, you need to create a gcp project (here, mine is called "public-bigquery") and copy the data over before running this: https://console.cloud.google.com/bigquery
---
title: "R Notebook"
output: html_notebook
---
# install bigrquery if needed
install.packages("httpuv")
install.packages("gargle")
install.packages("bigrquery")
library(httpuv)
library(gargle)
library(bigrquery)
# load libraries
library(bigrquery)
library(DBI)
# authenticate with google
bq_auth(use_oob = gargle::gargle_oob_default())
# connect to epa_historical_air_quality in BigQuery
connection <- dbConnect(
bigrquery::bigquery(),
project = "public-bigquery",
dataset = "epa_historical_air_quality",
)
# list tables
dbListTables(connection)
# perform a query
query = "SELECT * FROM `bigquery-public-data.epa_historical_air_quality.air_quality_annual_summary` LIMIT 1000"
df <- dbGetQuery(connection, query, n = 10)
write.csv(df, 'test.csv')
@DustinAlandzes
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment