```{r, echo = FALSE}
library(knitr)
library(readr)
opts_chunk$set(comment = "")
```

<style type="text/css">
pre { /* Code block - slightly smaller in this lecture */
    font-size: 20px;
}
</style>


## Data Output

While its nice to be able to read in a variety of data formats, it's equally important to be able to output data somewhere.

The `readr` package provides data exporting functions which have the pattern `write_*`: 

- `write_csv()`, 
- `write_delim()`, others. 

From `write_csv()` documentation:  

```
write_csv(x, file, 
  na = "NA", append = FALSE, 
  col_names = !append, quote_escape = "double",
  eol = "\n", path = deprecated()
)
```

```{r, echo = FALSE}
dat <- read_csv("http://jhudatascience.org/intro_to_r/data/Youth_Tobacco_Survey_YTS_Data.csv")
```

## Data Output

`x`: data frame you want to write

`file`:  file path where you want to R object written; it can be: 

* an absolute path, 
* a relative path (relative to your working directory), 
* a file name only (which writes the file to your working directory)
* remember to include the file extension (`.csv `, `.txt`, or `.tsv`)

```{r, eval = FALSE}
# Examples

write_csv(dat, file = "YouthTobacco_newNames.csv")

write_delim(dat, file = "YouthTobacco_newNames.csv", delim = ",")
```


## R binary file

`.rds` is an extension for R native file format.

`write_rds()` and `read_rds()` from `readr` package can be used to write/read a single R object to/from file.

Saving datasets in `.rds` format can save time if you have to read it back in later.

```{r}
# write an object: a data frame "dat"
write_rds(dat, file = "yts_dataset.rds")

# write an object: vector "x"
x <- c(1, 3, 3)
write_rds(x, file = "my_vector.rds")

# read an object from file and assign to a new object named "y"
x2 <- read_rds(file = "my_vector.rds")
x2
```


## Saving multiple objects

You may want to export a set of objects from R for later use, either to save time or to use in another R script. You can output these to an `.RData` file individually, or save your entire Environment with `save.image()`.

```{r}
save(x, x2, file = "x_x2_output.RData")
save.image(file = "my_environment.RData")
```


## Using RStudio for importing/exporting data

If there is an `.rds` or `.RData` file that you want to work with, you can open it into your environment using the file icon.

```{r echo = FALSE, out.width="50%"}
knitr::include_graphics(here::here("images/open_R_data_files.png"))
```

Can also save your entire environment or a subset of objects in your environment to a new `.RData` file with the save icon. Click the "List" button and switch to "Grid" to select which objects to delete or keep before saving the Environment.

```{r echo = FALSE, fig.show="hold", out.width="50%"}
knitr::include_graphics(here::here("images/subset_objects_in_environment.png"))
knitr::include_graphics(here::here("images/save_environment.png"))
```

## Summary {.small}

- Use `write_csv()` and `write_delim()` from the `readr` package to write your (modified) data
- `.rds` files can be handy for saving intermediate work
- Can save environment (or subset) using `save()` and `save.image()`

🏠 [Class Website](https://jhudatascience.org/intro_to_r/)

💻 [Data Output Lab](https://jhudatascience.org/intro_to_r/modules/Data_Output/lab/Data_Output_Lab.Rmd)

```{r, fig.alt="The End", out.width = "50%", echo = FALSE, fig.align='center'}
knitr::include_graphics(here::here("images/the-end-g23b994289_1280.jpg"))
```

Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=812226">Gerd Altmann</a> from <a href="https://pixabay.com//?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=812226">Pixabay</a>

