Saves an R object to a designated location in the project's Google Drive folder. If no custom writing function is provided, the function will infer the appropriate writing method based on the file's extension.
Arguments
- x
An R object to be written to Google Drive.
- file
Path to a file relative to project folder root. Can contain only letters, digits, '-', '_', '.', spaces and '/' symbols.
- fun
A custom writing function. If
NULL
(default), the appropriate writing function will be inferred based on the file's extension.- ...
Additional arguments to pass to the writing function
fun
.- local
Logical, defaulting to
FALSE
. IfTRUE
, the function will also create a local copy of the file at the specified path. Note that some writing functions might not overwrite existing files unless explicitly allowed. Typically, such functions have a parameter (often namedoverwrite
) to control this behavior. Check the documentation of the writing function used to determine the exact parameter name and pass it through the...
argument if necessary. Alternatively, you can define an anonymous function forfun
that calls a writing function with the overwriting option enabled.- root
Google Drive ID or URL of the project root. This serves as the reference point for all relative paths. When left as
NULL
, the root is automatically derived from thecloudfs.drive
field of the project's DESCRIPTION file.
Value
Invisibly returns a googledrive::dribble object representing the written file on Google Drive.
Default writing functions
Here's how we identify a writing function based on file extension
.csv
: readr::write_csv.json
: jsonlite::write_json.rds
: base::saveRDS.xls
: writexl::write_xlsx.xlsx
: writexl::write_xlsx.sav
: haven::write_sav.xml
: xml2::write_xml
Examples
if (FALSE) { # interactive()
# write mtcars dataframe to mtcars.csv in data folder
cloud_drive_write(mtcars, "data/mtcars.csv")
cloud_drive_write(random_forest, "models/random_forest.rds")
# provide custom writing function with parameters
cloud_drive_write(c("one", "two"), "text/count.txt", writeLines, sep = "\n\n")
}