This function saves an R object to a file in one of several supported formats. It supports saving in `.txt`, `.csv`, `.rds`, `.xlsx`, `.json`, `.xml`, and `.zip` formats.
Arguments
- object
The R object to be saved. This can be a data frame, list, or other compatible data structures depending on the file format.
- file
A character string specifying the name of the file, including the file extension.
- sep
A character string specifying the field separator string. Defaults to `" "`. This applies to `.txt` and `.csv` formats.
- na_strings
A logical value indicating whether to treat missing values as `"NA"`. Defaults to `TRUE`.
Details
The function automatically detects the file format based on the file extension and applies the appropriate saving method. Supported formats include:
- **.txt**: Saves as a text file. - **.csv**: Saves as a comma-separated values file. - **.rds**: Saves as an RDS file. - **.xlsx**: Saves as an Excel file (requires the `openxlsx` package). - **.json**: Saves as a JSON file (requires the `jsonlite` package). - **.zip**: Compresses and saves as a zip archive, with the object saved as a temporary `.txt` file inside. - **.sav**: Saves as a SPSS .sav file - **.sas7bdat**: Saves as SAS file - **.dta**: Saves as a Stata .dta file
If the necessary packages for certain formats are not installed, the function will stop and prompt the user to install the required package.
Examples
if (FALSE) { # \dontrun{
# Save a data frame as a CSV file
guides::save_as(mtcars, "mtcars.csv")
# Save an object as an RDS file
guides::save_as(mtcars, "mtcars.rds")
# Save a data frame as an Excel file
guides::save_as(mtcars, "mtcars.xlsx")
# Save a data frame as a JSON file
guides::save_as(mtcars, "mtcars.json")
} # }