Reshapes a data frame of cells (probably the output of sheets_cells()) into another data frame, i.e., puts it back into the shape of the source spreadsheet. At the moment, this function exists primarily for testing reasons. The flagship function read_sheet() is what most users are looking for. It is basically sheets_cells() + spread_sheet().

spread_sheet(df, col_names = TRUE, col_types = NULL, na = "",
  trim_ws = TRUE, guess_max = min(1000, max(df$row)))

Arguments

df

A data frame with one row per (nonempty) cell, integer variables row and column (probably referring to location within the spreadsheet), and a list-column cell of SHEET_CELL objects.

col_names

TRUE to use the first row as column names, FALSE to get default names, or a character vector to provide column names directly. In all cases, names are processed through tibble::tidy_names(). If user provides col_types, col_names can have one entry per column or one entry per unskipped column.

col_types

Column types. Either NULL to guess all from the spreadsheet or a string of readr-style shortcodes, with one character or code per column. If exactly one col_type is specified, it is recycled. See Details 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?

guess_max

Maximum number of data rows to use for guessing column types.

Value

A tibble in the shape of the original spreadsheet, but enforcing user's wishes regarding column names, column types, NA strings, and whitespace trimming.

Examples

df <- sheets_cells(sheets_example("mini-gap"))
#> Reading from 'test-gs-mini-gapminder'
#> Range "'Africa'"
spread_sheet(df)
#> # A tibble: 5 x 6 #> country continent year lifeExp pop gdpPercap #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 Algeria Africa 1952 43.1 9279525 2449. #> 2 Angola Africa 1952 30.0 4232095 3521. #> 3 Benin Africa 1952 38.2 1738315 1063. #> 4 Botswana Africa 1952 47.6 442308 851. #> 5 Burkina Faso Africa 1952 32.0 4469979 543.
# ^^ gets same result as ... read_sheet(sheets_example("mini-gap"))
#> Reading from 'test-gs-mini-gapminder'
#> Range "'Africa'"
#> # A tibble: 5 x 6 #> country continent year lifeExp pop gdpPercap #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 Algeria Africa 1952 43.1 9279525 2449. #> 2 Angola Africa 1952 30.0 4232095 3521. #> 3 Benin Africa 1952 38.2 1738315 1063. #> 4 Botswana Africa 1952 47.6 442308 851. #> 5 Burkina Faso Africa 1952 32.0 4469979 543.