This is the main "read" function of the googlesheets4 package. It goes by two names, because we want it to make sense in two contexts:
read_sheet()evokes other table-reading functions, likereadr::read_csv()andreadxl::read_excel(). Thesheetin this case refers to a Google (spread)Sheet.range_read()is the right name according to the naming convention used throughout the googlesheets4 package.
read_sheet() and range_read() are synonyms and you can use either one.
Usage
range_read(
ss,
sheet = NULL,
range = NULL,
col_names = TRUE,
col_types = NULL,
na = "",
trim_ws = TRUE,
skip = 0,
n_max = Inf,
guess_max = min(1000, n_max),
.name_repair = "unique"
)
read_sheet(
ss,
sheet = NULL,
range = NULL,
col_names = TRUE,
col_types = NULL,
na = "",
trim_ws = TRUE,
skip = 0,
n_max = Inf,
guess_max = min(1000, n_max),
.name_repair = "unique"
)Arguments
- ss
Something that identifies a Google Sheet:
its file id as a string or
drive_ida URL from which we can recover the id
a one-row
dribble, which is how googledrive represents Drive filesan instance of
googlesheets4_spreadsheet, which is whatgs4_get()returns
Processed through
as_sheets_id().- sheet
Sheet to read, in the sense of "worksheet" or "tab". You can identify a sheet by name, with a string, or by position, with a number. Ignored if the sheet is specified via
range. If neither argument specifies the sheet, defaults to the first visible sheet.- range
A cell range to read from. If
NULL, all non-empty cells are read. Otherwise specifyrangeas described in Sheets A1 notation or using the helpers documented in cell-specification. Sheets uses fairly standard spreadsheet range notation, although a bit different from Excel. Examples of valid ranges:"Sheet1!A1:B2","Sheet1!A:A","Sheet1!1:2","Sheet1!A5:A","A1:B2","Sheet1". Interpreted strictly, even if the range forces the inclusion of leading, trailing, or embedded empty rows or columns. Takes precedence overskip,n_maxandsheet. Noterangecan be a named range, like"sales_data", without any cell reference.- col_names
TRUEto use the first row as column names,FALSEto get default names, or a character vector to provide column names directly. If user providescol_types,col_namescan have one entry per column or one entry per unskipped column.- col_types
Column types. Either
NULLto guess all from the spreadsheet or a string of readr-style shortcodes, with one character or code per column. If exactly onecol_typeis specified, it is recycled. See Column Specification for more.- na
Character vector of strings to interpret as missing values. By default, blank cells are treated as missing data.
- trim_ws
Logical. Should leading and trailing whitespace be trimmed from cell contents?
- skip
Minimum number of rows to skip before reading anything, be it column names or data. Leading empty rows are automatically skipped, so this is a lower bound. Ignored if
rangeis given.- n_max
Maximum number of data rows to parse into the returned tibble. Trailing empty rows are automatically skipped, so this is an upper bound on the number of rows in the result. Ignored if
rangeis given.n_maxis imposed locally, after reading all non-empty cells, so, if speed is an issue, it is better to userange.- guess_max
Maximum number of data rows to use for guessing column types.
- .name_repair
Handling of column names. By default, googlesheets4 ensures column names are not empty and are unique. There is full support for
.name_repairas documented intibble::tibble().
Value
A tibble
Column Specification
Column types must be specified in a single string of readr-style short codes, e.g. "cci?l" means "character, character, integer, guess, logical". This is not where googlesheets4's col spec will end up, but it gets the ball rolling in a way that is consistent with readr and doesn't reinvent any wheels.
Shortcodes for column types:
_or-: Skip. Data in a skipped column is still requested from the API (the high-level functions in this package are rectangle-oriented), but is not parsed into the data frame output.?: Guess. A type is guessed for each cell and then a consensus type is selected for the column. If no atomic type is suitable for all cells, a list-column is created, in which each cell is converted to an R object of "best" type. If no column types are specified, i.e.col_types = NULL, all types are guessed.l: Logical.i: Integer. This type is never guessed from the data, because Sheets have no formal cell type for integers.dorn: Numeric, in the sense of "double".D: Date. This type is never guessed from the data, because date cells are just serial datetimes that bear a "date" format.t: Time of day. This type is never guessed from the data, because time cells are just serial datetimes that bear a "time" format. Not implemented yet; returns POSIXct.T: Datetime, specifically POSIXct.c: Character.C: Cell. This type is unique to googlesheets4. This returns raw cell data, as an R list, which consists of everything sent by the Sheets API for that cell. Has S3 type of"CELL_SOMETHING"and"SHEETS_CELL". Mostly useful internally, but exposed for those who want direct access to, e.g., formulas and formats.L: List, as in "list-column". Each cell is a length-1 atomic vector of its discovered type.Still to come: duration (code will be
:) and factor (code will bef).
Examples
if (FALSE) { # gs4_has_token()
ss <- gs4_example("deaths")
read_sheet(ss, range = "A5:F15")
read_sheet(ss, range = "other!A5:F15", col_types = "ccilDD")
read_sheet(ss, range = "arts_data", col_types = "ccilDD")
read_sheet(gs4_example("mini-gap"))
read_sheet(
gs4_example("mini-gap"),
sheet = "Europe",
range = "A:D",
col_types = "ccid"
)
}
