stRoke/vignettes/ds2dd.Rmd

88 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2023-04-12 13:59:26 +02:00
---
2023-04-12 14:09:16 +02:00
title: "ds2dd"
date: "`r Sys.Date()`"
2023-04-12 13:59:26 +02:00
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{ds2dd}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(stRoke)
```
# Easy data set to data base workflow
This function can be used as a simple tool for creating at data base metadata file for REDCap (called a DataDictionary) based on a given data set file.
## Step 1 - Load your data set
Here we'll use the sample TALOS dataset included with the package.
```{r}
data("talos")
2023-04-12 14:25:19 +02:00
ds <- talos
2023-04-12 13:59:26 +02:00
# As the data set lacks an ID column, one is added
2023-04-12 14:25:19 +02:00
ds$id <- seq_len(nrow(ds))
2023-04-12 13:59:26 +02:00
```
## Step 2 - Create the DataDictionary
```{r}
2023-04-12 14:25:19 +02:00
datadictionary <- ds2dd(ds,record.id = "id",include.column.names = TRUE)
2023-04-12 13:59:26 +02:00
```
Now additional specifications to the DataDictionary can be made manually, or it can be uploaded and modified manually in the graphical user interface on the web page.
2023-04-12 14:25:19 +02:00
The function will transform column names to lower case and substitute spaces for underscores. The output is a list with the DataDictionary and a vector of new column names for the dataset to fit the meta data.
## Step 3 - Meta data upload
2023-04-12 13:59:26 +02:00
Now the DataDictionary can be exported as a spreadsheet and uploaded or it can be uploaded using the `REDCapR` package (only projects with "Development" status).
Use one of the two approaches below:
### Manual upload
```{r eval=FALSE}
2023-04-12 14:25:19 +02:00
write.csv(datadictionary$DataDictionary,"datadictionary.csv")
2023-04-12 13:59:26 +02:00
```
### Upload with `REDCapR`
```{r eval=FALSE}
REDCapR::redcap_metadata_write(
2023-04-12 14:25:19 +02:00
datadictionary$DataDictionary,
2023-04-12 13:59:26 +02:00
redcap_uri = keyring::key_get("DB_URI"),
token = keyring::key_get("DB_TOKEN")
)
```
In the ["REDCap R Handbook"](https://agdamsbo.github.io/redcap-r-handbook/) more is written on interfacing with REDCap in R using the `library(keyring)`to store credentials in [chapter 1.1](https://agdamsbo.github.io/redcap-r-handbook/access.html#sec-getting-access).
2023-04-12 14:25:19 +02:00
## Step 4 - Data upload
2023-04-13 13:55:10 +02:00
The same two options are available for data upload as meta data upload: manual or through `REDCapR`.
2023-04-12 14:25:19 +02:00
Only the latter is shown here.
```{r eval=FALSE}
2023-04-13 13:55:10 +02:00
# new column names are applied
2023-04-12 14:25:19 +02:00
colnames(ds) <- datadictionary$`Column names`
REDCapR::redcap_write(
ds,
redcap_uri = keyring::key_get("DB_URI"),
token = keyring::key_get("DB_TOKEN")
)
```