Use R with BigQuery

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

This page is an example of one way to use R to interact with BigQuery data. You can use other methods available in the bigrquery package or other packages, such as bigQueryR.

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:

  1. In the Google Cloud console, go to the User-managed notebooks page.

    Go to User-managed notebooks

  2. Select the instance that you want to open.

  3. Click Open JupyterLab.

    Your user-managed notebooks instance opens JupyterLab.

  4. 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:

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

    # 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

To load BigQuery data into a tibble using the bigrquery R package, complete the following steps.

  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; 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.

  3. Run the cell to view 10 rows of data from one of BigQuery's public datasets.

What's next

To learn more about how to use BigQuery data in your R notebooks, read the bigrquery documentation and Welcome to bigQueryR.