Use R with BigQuery

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.

  1. Go to the AI Platform Notebooks page in the Google Cloud Console.

    Go to the AI Platform Notebooks page

  2. Select Open JupyterLab for the R instance that you want to open.

    Open JupyterLab

  3. Select File -> New -> Notebook, and then select the R kernel.

    Add an R notebook

Load the bigrquery R package

Follow these steps to load the bigrquery R package.

  1. In the notebook's first code cell, enter the following:

    # Load the package
  2. Click the run button to run the command. R loads the package.

    The run button

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.

  1. Click the notebook's + button to add a code cell to the notebook.

    The + button

  2. 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
  3. Run the cell to view 10 rows of data from one of BigQuery's public datasets.

What's next

Read bigrquery documentation to learn more about how you can use BigQuery data in your R notebooks.