This is a R wrapper for Carto.com API. Carto is a web map provider. I used Carto in my project because:
Carto provide several types of API for different tasks. It’s simple to construct an API call with curl
but also very cumbersome. You also often need to use some parts of the request response, which means a lot of copy/paste. I try to replace all repetitive manual labor with programs as much as possible, so it’s only natural to do this with R.
There are some R package or function available for Carto API but they don’t meet my needs.
cartodb.com
to carto.com
. There is fork that fixed this problem. Though this package is more intended for this kind of usage:change data in databse tables in Carto by inserting and updating
r2cartodb is a function that upload a spatial or non-spatial data frame to your CartoDB account.
I developed my own R functions for every API call I used gradually, then I made it into a R package.
So it’s more focused on data import/sync and time consuming SQL inquiries. I have found it saved me a lot of time.
install.packages("devtools")
devtools::install_github("dracodoc/rCartoAPI")
devtools
need some dependencies to work, like Rtools in windows, Xcode in Mac. If you have trouble installing it, there are also lightweighted alternatives like remotes. After installing remotes
, run remotes::install_github("dracodoc/rCartoAPI")
All the functions in the package currently require an API key from Carto. Without API key you can only do some read only operations with public data. If there is more demand I can add the keyless versions, though I think it will be even better for Carto to just provide API key in free plan.
It’s not easy to save sensitive information securely and conveniently at the same time. After checking this summary and the best practices vignette from httr
, I chose to save them in system environment and minimize the exposure of user name and API key. After reading from system environment, the user name and API key only exist inside the package functions, which are further wrapped in package environment, not visible from global environment.
file.edit("~/.Renviron")
Add these lines:
# for Carto
carto_acc = "your user name"
carto_api_key = "your api key"
Then run setup_key()
.
Note if you want to remove the key and just deleted the lines from~/.Renviron
, the key could still exist in environment. Restart R session to make sure it was removed. For adding key or changing key value, editing and runing setup_key()
is enough.
Many references I found in this usage used .Rprofile
, while I think .Renviron
is more suitable for this need. If you want to update variables and reload them, you don’t need to touch the other part in .Rprofile
.
Function summary:
update_env
, update environment variables after changes to Carto user name and API keylocal_import
, upload local fileurl_import
, import a remote file by urlconvert_dropbox_link
, convert dropbox shared link into direct file link. Can take windows clipboard if no parameter provided.url_sync
, let Carto to sync with a remote file by urlcheck_sync_status
, check status of a sync fileforce_sync
, force sync a file instead of by schedulelist_sync_tables
, list all files in sync in accountlist_sync_tables_df
, return sync file information in data frameremove_sync
, remove sync relationship but keep the data filesql_inquiry
, run sql inquirytest_connection
, simple read only call just to test connection is working. Both this and list_sync_tables
can be used to test if the Carto user name and API key is working at intendedsql_inquiry_df
, run sql inquiry and return result in data framesql_inquiry_save
, run sql inquiry and return result in filesql_inquiry_save_geojson
, run sql inquiry and return result in GEOJson format in filesql_batch_inquiry
, submit Batch sql inquirysql_batch_inquiry_id
, submit Batch sql inquiry and return job idsql_batch_check
, check Batch sql inquiry status by job idsql_batch_cancel
, cancel Batch sql inquirySee help on individual function for details. Check Carto API document for more information about the API call and parameters.
I wrote about some tips in my blog post, which include: