Use R with BigQuery

This page describes how to load data from BigQuery into an R tibble, using the bigrquery R package.

Before you begin

Before you begin, create an R framework Notebooks instance.

Open a JupyterLab notebook

Follow these steps to open a Notebooks instance.

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

    Go to the Notebooks page

  2. Next to the R instance that you want to open, click Open JupyterLab.

    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 file's first code cell, enter the following:

    # Load the package
    library(bigrquery)
    
  2. Click Run the selected cells and advance.

    The Run the selected cells and advance button

    R loads the package.

Load data from BigQuery

Follow these steps to load BigQuery data into a tibble using the bigrquery R package. Since you are running this from a Notebooks instance, you are already authenticated.

  1. To add a code cell, click the notebook file's Insert a cell below button.

    The Insert a cell below button

  2. 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 and store the data in a tibble
    tb <- bq_project_query(projectid, sql)
    
    # Print 10 rows of the data
    bq_table_download(tb, max_results = 10)
    

    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.

  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.