The googlesheets4 R package lets you read, write, and update Google Sheets directly from R, so you can pull live data into an analysis or push results back to a shared spreadsheet without manual exports. You install it from CRAN, authorize it once with your Google account, then use functions like read_sheet() and write_sheet() on any sheet you can access. You need R, an internet connection, and a Google account; no paid Google Cloud project is required for typical use.
What You'll Need
- R installed (version 3.6 or newer is a safe baseline) and preferably RStudio for a smoother authorization experience.
- The
googlesheets4package, installed from CRAN. - A Google account with access to the spreadsheet you want to use. For shared sheets, the account you authorize must already have edit or view permission in Google Sheets.
- The spreadsheet's ID or its full URL. The ID is the long string between
/d/and/editin the URL. - An internet connection and a browser for the one-time OAuth consent screen.
Step-by-Step
- Install the package. In an R console, run
install.packages("googlesheets4"). If you also want to work with Google Drive files broadly, installgoogledriveas well, but it is not required for reading and writing sheets. - Load the library. Run
library(googlesheets4). The package will print a message about authentication if you have not authorized yet. - Authorize access. Run
gs4_auth(). A browser window opens asking you to sign in to Google and grant the requested permissions. Choose the account that owns or can edit the target sheet. After you approve, the browser shows a confirmation and you can close it. In RStudio the token is cached; in a plain R session it may be cached in your user profile. If you are on a headless server, usegs4_auth(email = "you@example.com")and follow the console instructions, or set up a service account (see Tips). - Find your sheet. You can list sheets with
gs4_find()or open the sheet in a browser and copy the URL. The sheet ID is the part between/d/and/edit. For example, inhttps://docs.google.com/spreadsheets/d/abc123/editthe ID isabc123. - Read data. Use
read_sheet("abc123")to read the first worksheet, orread_sheet("abc123", sheet = "Sheet2")to pick a tab by name. You can also pass a range likerange = "A1:D10". The result is a tibble, which you can convert withas.data.frame()if you prefer. - Write data. Use
write_sheet(my_data, ss = "abc123", sheet = "Results"). If the tab does not exist, the package creates it. Writing replaces the content of the target range by default; to append rows instead, usesheet_append("abc123", my_data, sheet = "Log"). - Update specific cells. Use
range_write("abc123", data = my_data, sheet = "Sheet1", range = "B2")to write starting at a cell without clearing the rest of the sheet. Userange_clear()to clear a range when needed. - Create a new sheet. Run
gs4_create("My new sheet", sheets = my_data). This creates a spreadsheet in your Google Drive and returns its ID, which you can store for later use. - Manage access. If you need to share the sheet, use
sheet_share()with an email address and role such as"reader"or"writer". The account you authorized must have permission to change sharing settings. - Deauthorize when done. Run
gs4_deauth()to clear the cached token, especially on a shared computer. This does not revoke access at Google; to revoke fully, visit your Google Account permissions page.
Common Mistakes
- Using the full URL where an ID is expected. Most functions accept a URL, but some older examples use
key =or expect an ID. If a call fails, extract the ID and pass it as the first argument or asss =. - Authorizing the wrong Google account. If you have multiple Google accounts signed in, the browser may default to the wrong one. Sign out of other accounts or choose the correct one on the consent screen. A sheet that exists but returns a permission error usually means the authorized account cannot see it.
- Assuming writes append.
write_sheet()overwrites the target range. Usesheet_append()for new rows andrange_write()for targeted updates. - Ignoring column types. Google Sheets stores everything as text or numbers loosely. A column of mixed values may come back as character. Clean types after reading with
type.convert()or by specifyingcol_typesinread_sheet(). - Leaving tokens on shared machines. Cached credentials can let others act as you. Run
gs4_deauth()after use on any shared or public computer. - Expecting real-time sync. The package reads and writes on demand; it does not stream changes. Re-run your read to see updates made in the browser.
Tips
- Store the sheet ID in an environment variable or a config file rather than hard-coding it in scripts you share.
- For scheduled or server jobs, use a Google Cloud service account and
gs4_auth(path = "path/to/service-account.json"). Share the sheet with the service account's email address so it has access. This avoids interactive browser prompts. - Read only the range you need with
range =to speed up large sheets. - Use
gs4_get()to inspect sheet metadata such as tab names and dimensions before reading. - Wrap write operations in
tryCatch()so a failed network call does not abort a long script silently. - Keep a local CSV backup of important sheets before bulk writes; overwriting a range is easy to do by accident.
Troubleshooting
If authorization fails, first check that your system clock is correct, then sign out of Google in your browser and run gs4_auth() again. If the browser does not open, copy the URL printed in the console into a browser manually. If you see a message about an expired or invalid token, run gs4_deauth() and then gs4_auth() to start fresh.
If a read returns an error about permissions, confirm the authorized account can open the sheet in a browser and that the sheet ID is correct. If a write fails, check that the target tab exists or let the package create it, and confirm the account has edit access. For service account setups, verify the JSON key path and that the sheet is shared with the service account email.
If the package cannot be installed, check your R version and CRAN repository settings. On corporate networks, a proxy may block CRAN or Google endpoints; ask your network administrator. If data comes back with unexpected types, inspect the raw sheet and clean the column, or pass explicit col_types.
Frequently Asked Questions
Do I need a Google Cloud project to use googlesheets4?
No for normal interactive use. The package ships with default OAuth credentials for typical cases. A Cloud project and service account are useful for automated or server-side jobs.
Can I read a sheet that is shared with me but not owned by me?
Yes, as long as the Google account you authorize has at least view access. The package uses that account's permissions.
Does write_sheet overwrite the whole spreadsheet?
No. It writes to the specified sheet (tab) and range. By default it replaces the content of the target range on that tab, not other tabs.
How do I append rows without overwriting?
Use sheet_append() with the sheet ID, the data, and the tab name. It adds rows after the last row with data.
Can I use googlesheets4 on a server without a browser?
Yes. Use a service account JSON key with gs4_auth(path = ...), or follow the console-based OAuth flow. Share the target sheet with the service account email.
Is googlesheets4 the same as googlesheets?
No. googlesheets4 is the current package built on the Sheets API v4. The older googlesheets package is deprecated; use googlesheets4 for new work.