---
title: "Data Archiving"
date: "2017-06-15"
output:
  html_document:
    fig_caption: false
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
    toc_depth: 2
vignette: >
  %\VignetteIndexEntry{4. Data Archiving}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

This vignette describes how to archive data into Dataverse directly from R.


```r
library("dataverse")
Sys.setenv("DATAVERSE_SERVER" = "dataverse.harvard.edu")
```

## SWORD-based Data Archiving

The main data archiving (or "deposit") workflow for Dataverse is built on [SWORD v2.0](http://swordapp.org/sword-v2/). This means that to create a new dataset listing, you will have first initialize a dataset entry with some metadata, add one or more files to the dataset, and then publish it. This looks something like the following:

```R
# retrieve your service document
d <- service_document()

# list current datasets in a dataverse
list_datasets("mydataverse")

# create a new dataset
## create a list of metadata
metadat <- list(title = "My Study",
                creator = "Doe, John",
                description = "An example study")
## initiate the dataset
dat <- initiate_sword_dataset("mydataverse", body = metadat)
```

Once the dataset is initiated, it is possible to add and delete files:

```R
tmp <- tempfile()
write.csv(iris, file = tmp)
f <- add_file(dat, file = tmp)
```

The `add_file()` function accepts, as its first argument, a character vector of file names, a data.frame, or a list of R objects. Files can be deleted using `delete_file()`. Once the dataset is finalized, it can be published using `publish_dataset()`:

```R
publish_dataset(dat)
```

And it will then show up in the list of published datasets returned by `list_datasets(dat)`.

## Native API

Dataverse also implements a second way to release datasets, called the "native" API. It is similar to to the SWORD API:

```R
# create the dataset
ds <- create_dataset("mydataverse")

# add files
tmp <- tempfile()
write.csv(iris, file = tmp)
f <- add_dataset_file(file = tmp, dataset = ds)

# publish dataset
publish_dataset(ds)

# dataset will now be published
get_dataverse("mydataverse")
```


