Update Grist table with new data, updating existing rows or adding new ones, matching rows on the given key columns. (This method does not remove rows from Grist.)

sync_table(api, table_id, new_data, key_cols)

Arguments

api

A 'gristapi::grist_api'-object

table_id

Normalized table name (see id in listtables method). First character upper. OBLIGATORY

new_data

A data frame holding the records with new and modified values. OBLIGATORY

key_cols

A vector of columns (grist_col_id) constituting a unique key

Value

TRUE if the transaction was sucessfull.

See also

Examples

# Synchronize the table with a dataframe (update or add) on unique key
library(dplyr)
table_name <- paste0("Mtcars_", get_os())
api <- grist_api$new(
  server = 'https://grist.numerique.gouv.fr',
  api_key = Sys.getenv("GRIST_KEY"),
  doc_id = Sys.getenv("GRIST_DOC_TEST")
)
new_data <- fetch_table(api, table_name, filters = 'filter={"cyl": [8]}')
new_data <- new_data |>
  mutate(carb = 4, names = ifelse(names == 'Camaro Z28', 'Camaro Z28 - custom', names))
new_data <- new_data |> select(-id)
# synchronization on the "names" key
sync_table(api, table_name, new_data, c("names"))
#> [1] TRUE