Adds new records to the given table. The data is a data.frame.
add_records(api, table_id, record_dicts, create_or_replace = FALSE)
A 'gristapi::grist_api'-object
Normalized table name (see id
in listtables
method). First character upper. OBLIGATORY
A data frame holding the records with new values. OBLIGATORY
boolean. TRUE, the table is created if it does not exist or replaced with the structure of the new records. Default FALSE
An integer vector of newly created ids.
Other function_gristapi:
countrows()
,
delete_records()
,
fetch_table()
,
listcolumns()
,
listtables()
,
sync_table()
,
update_records()
# Create a table with data
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")
)
add_records( api,
table_id = table_name,
record_dicts = data.frame(names = row.names(mtcars), mtcars),
create_or_replace = TRUE
)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26 27 28 29 30 31 32
# Add data in a table
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
new_row <- fetch_table(api, table_name, filters = 'filter={"names": ["Duster 360"]}')
new_row <- new_row |> mutate(names = paste0(names,' - custom'), mpg = 16.7)
new_row <- new_row |> select(-id)
add_records(api, table_name, new_row)
#> [1] 33