This page describes how to load data from BigQuery into an R data frame, using the bigrquery R package.
Before you begin
Before you begin, create a new AI Platform Notebooks instance for R.
Open a JupyterLab notebook
Follow these steps to open an AI Platform Notebooks instance.
Go to the AI Platform Notebooks page in the Google Cloud Console.
Select Open JupyterLab for the R instance that you want to open.
Select File -> New -> Notebook, and then select the R kernel.
Load the bigrquery R package
Follow these steps to load the bigrquery R package.
In the notebook's first code cell, enter the following:
# Load the package library(bigrquery)
Click the run button to run the command. R loads the package.
Load data from BigQuery
Follow these steps to load BigQuery data into a
data frame using the
bigrquery R package. Since you are running this from
an AI Platform Notebooks instance, you are already authenticated.
Click the notebook's + button to add a code cell to the notebook.
In the new code cell, enter the following. Replace PROJECT_ID with your Google Cloud project ID.
To get your project ID, click the drop-down arrow next to your project name in the Google Cloud Console.
# 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 and store the data in a dataframe df <- query_exec(sql, projectid, use_legacy_sql = FALSE) # Print the query result df
Run the cell to view 10 rows of data from one of BigQuery's public datasets.
Read bigrquery documentation to learn more about how you can use BigQuery data in your R notebooks.