--- title: "Importing data from non .ddf sources" author: "Vikram B. Baliga" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Importing data from non .ddf sources} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` `workloopR`'s data import functions, such as `read_ddf()`, are generally geared towards importing data from .ddf files (e.g. those generated by Aurora Scientific's Dynamic Muscle Control and Analysis Software). Should your data be stored in another file format, you can use the `as_muscle_stim()` function to generate your own `muscle_stim` objects. These `muscle_stim` objects are used by nearly all other `workloopR` functions and are formatted in a very specific way. This helps ensure that other functions can interpret data & metadata correctly and also perform internal checks. ## Load packages Before running through anything, we'll ensure we have the packages we need. ```{r package_loading, message=FALSE, warning=FALSE} library(workloopR) library(magrittr) library(ggplot2) ``` ## Data Because it is somewhat difficult to simulate muscle physiology data, we'll use one of our workloop files, deconstruct it, and then re-assemble the data via `as_muscle_stim()`. ```{r get_data} ## Load in the work loop example data from workloopR workloop_dat <- system.file( "extdata", "workloop.ddf", package = 'workloopR') %>% read_ddf(phase_from_peak = TRUE) %>% fix_GR(GR = 2) ## First we'll extract Time Time <- workloop_dat$Time ## Now Position Position <- workloop_dat$Position ## Force Force <- workloop_dat$Force ## Stimulation Stim <- workloop_dat$Stim ## Put it all together as a data.frame my_data <- data.frame(Time = Time, Position = Position, Force = Force, Stim = Stim) head(my_data) ``` ## Assemble via `as_muscle_stim()` It is absolutely crucial that the columns be named "Time", "Position", "Force", and "Stim" (all case-sensitive). Otherwise, `as_muscle_stim()` will not interpret data correctly. At minimum, this `data.frame`, the type of experiment, and the frequency at which data were recorded (`sample_frequency`, as a numeric) are necessary for `as_muscle_stim()`. ```{r as_mus_basic} ## Put it together my_muscle_stim <- as_muscle_stim(x = my_data, type = "workloop", sample_frequency = 10000) ## Data are stored in columns and basically behave as data.frames head(my_muscle_stim) ggplot(my_muscle_stim, aes(x = Time, y = Position)) + geom_line() + labs(y = "Position (mm)", x = "Time (secs)") + ggtitle("Time course of length change") + theme_bw() ``` ### Attributes By default, a couple attributes are auto-filled based on the available information, but it's pretty bare-bones ```{r attributes} str(attributes(my_muscle_stim)) ``` We highly encourage you to add in as many of these details as possible by passing them in via the `...` argument. For example: ```{r add_file_id} ## This time, add the file's name via "file_id" my_muscle_stim <- as_muscle_stim(x = my_data, type = "workloop", sample_frequency = 10000, file_id = "workloop123") ## For simplicity, we'll just target the file_id attribute directly instead of ## printing all attributes again attr(my_muscle_stim, "file_id") ``` ### Possible attributes Here is a list of all possible attributes that can be filled. ```{r} names(attributes(workloop_dat)) ``` To see how each should be formatted, (e.g. which ones take numeric values vs. character vectors...etc) ```{r} str(attributes(workloop_dat)) ``` ## Thanks for reading! Please feel free to contact either Vikram or Shree with suggestions or code development requests. We are especially interested in expanding our data import functions to accommodate file types other than .ddf in future versions of `workloopR`.