Use R with BigQuery
This page describes how to load data from BigQuery into an R
by using the
These steps are written for use in a Jupyter notebook
within a Vertex AI Workbench user-managed notebooks instance.
This page is an example of one way to use R to interact with
BigQuery data. You can use other methods available
bigrquery package or other packages, such as
Before you begin
Before you begin, create an R framework user-managed notebooks instance.
Open a JupyterLab notebook
To open a user-managed notebooks instance, complete the following steps:
In the Google Cloud console, go to the User-managed notebooks page.
Select the instance that you want to open.
Click Open JupyterLab.
Your user-managed notebooks instance opens JupyterLab.
In JupyterLab, select File > New > Notebook, and then select the R kernel.
Load the bigrquery R package
To load the bigrquery R package, complete the following steps:
In the notebook file's first code cell, enter the following code:
# Load the package library(bigrquery)
ClickRun the selected cells and advance.
R loads the package.
Load data from BigQuery
To load BigQuery data into a
bigrquery R package, complete the following steps.
To add a code cell, click the notebook file'sInsert a cell below button.
In the new code cell, enter the following.
# Store the project ID projectid = "PROJECT_ID" # Set your query sql <- "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_current` LIMIT 10" # Run the query; this returns a bq_table object that you can query further tb <- bq_project_query(projectid, sql) # Store the first 10 rows of the data in a tibble sample <-bq_table_download(tb, n_max = 10) # Print the 10 rows of data sample
Replace PROJECT_ID with your Google Cloud project ID.
Run the cell to view 10 rows of data from one of BigQuery's public datasets.
To learn more about how to use BigQuery data in your R notebooks, read the bigrquery documentation and Welcome to bigQueryR.