https://github.com/favstats/appendornot
The goal of appendornot is to provide easy access to writing and appending csv or text files.
https://github.com/favstats/appendornot
Last synced: 10 months ago
JSON representation
The goal of appendornot is to provide easy access to writing and appending csv or text files.
- Host: GitHub
- URL: https://github.com/favstats/appendornot
- Owner: favstats
- License: mit
- Created: 2022-11-25T13:49:57.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-06T21:49:22.000Z (over 2 years ago)
- Last Synced: 2024-01-06T22:31:06.862Z (over 2 years ago)
- Language: R
- Homepage:
- Size: 33.2 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# appendornot
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=appendornot)
The goal of `appendornot` is to provide easy access to writing and appending csv or text files.
## Installation
You can install the development version of `appendornot` like so:
``` r
## install.packages("remotes")
remotes::install_github("favstats/appendornot")
```
## Load Library
```{r example}
library(appendornot)
```
## Motivation for `appendornot`
I often find myself in the situation where I am scraping data and I want to keep appending some data to a file. However, I also want to make it so that it creates the file in the first place if it doesn't exist yet, then appends to it in the next iteration.
## Example `save_csv()`
Write csv file with `save_csv`. The function will automatically create a new csv or append the file if it already does exist.
``` r
save_csv(cars, "cars.csv")
```
You can also create a folder (if it doesn't exist yet) before it saves the csv. Just specify: `create_subfolders = TRUE`.
This example creates the "`data`" folder first, then saves the file:
``` r
save_csv(cars, "data/cars.csv", create_subfolders = TRUE)
```
### Different order and new variables to be appended (or are missing)
Note: `save_csv` will automatically handle column order and column names that may not be present/are being added to the (appended) data.
``` r
## this data to be appended has a new column
new_column_dat <- cars %>% mutate(thisisnownew = "hello")
## this is a different order than the original dataset
different_order <- cars %>% select(dist, speed)
## this is a new column with a different order and missing a variable that is already there
different_order_newcolumn <- new_column_dat %>% select(dist, thisisnownew)
```
All of these cases will be automatically handled by the `save_csv` function:
``` r
save_csv(new_column_dat, "cars.csv")
```
``` r
save_csv(different_order, "cars.csv")
```
``` r
save_csv(different_order_newcolumn, "cars.csv")
```
## Example `save_lines()`
Write text file with `save_lines`. The function will automatically create a new text file or append the file if it already does exist.
``` r
save_lines(names(cars), "lines.txt")
```
You can also create a folder (if it doesn't exist yet) before it saves the text file.
This example creates the "`txt`" folder first, then saves the file:
``` r
save_lines(names(cars), "txt/lines.txt")
```