Skip to content

Commit

Permalink
Add return statements to all named functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeramsenthi committed Oct 21, 2019
1 parent a77eb6d commit 4943ecc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 68 deletions.
8 changes: 4 additions & 4 deletions R/data_analysis_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ analyze_workloop <- function(x,
attr(x,"net_power")<-net_p
if(!all(is.na(attr(x,"units"))))
attr(x,"units")<-c(attr(x,"units"),"m/s","m/s","W")
x
return(x)
},
x_by_cycle,
velocity,
Expand All @@ -298,7 +298,7 @@ analyze_workloop <- function(x,
attr(result,"summary")<-summary_table
class(result)<-c("analyzed_workloop","list")

stats::setNames(result,paste0("cycle_",cycle_names))
return(stats::setNames(result,paste0("cycle_",cycle_names)))
}


Expand Down Expand Up @@ -390,7 +390,7 @@ time_correct <- function(x){
utils::tail(x$mtime,1)-utils::head(x$mtime,1)
attr(x,"time_correction_rate") <-
attr(x,"power_difference") / attr(x,"time_difference")
x
return(x)
}


Expand Down Expand Up @@ -520,6 +520,6 @@ isometric_timing <- function(x,
set_point_results<-data.frame(as.list(set_point_results))

# return both result
cbind(main_results,set_point_results)
return(cbind(main_results,set_point_results))
}

120 changes: 57 additions & 63 deletions R/data_import_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ muscle_stim <-
attr(data, "file_id") <- file_id
attr(data, "mtime") <- mtime
class(data) <- c(class(data), "muscle_stim", "data.frame")
data
return(data)
}

# Classes for specific trial type
Expand All @@ -61,10 +61,10 @@ workloop <-

attr(data,"position_inverted")<-FALSE
class(data)<-c("workloop")
muscle_stim(data=data,
stim_table=stim_table,
sample_frequency=sample_frequency,
...)
return(muscle_stim(data=data,
stim_table=stim_table,
sample_frequency=sample_frequency,
...))
}

#' @noRd
Expand All @@ -73,16 +73,16 @@ tetanus<-
attr(data,"stimulus_frequency")<-stim_table$frequency[1]
attr(data,"stimulus_length")<-stim_table$length[1]
class(data)<-c("tetanus","isometric")
muscle_stim(data=data,
stim_table=stim_table,
...)
return(muscle_stim(data=data,
stim_table=stim_table,
...))
}

#' @noRd
twitch<-
function(data,...){
class(data)<-c("twitch","isometric")
muscle_stim(data=data,...)
return(muscle_stim(data=data,...))
}

# Generate type-specific output header for workloop objects
Expand Down Expand Up @@ -327,7 +327,7 @@ as_muscle_stim <- function(x,
"workloop"=class(x)<-c("workloop",class(x)),
"tetanus"=class(x)<-c("tetanus","isometric",class(x)),
"twitch"=class(x)<-c("twitch","isometric",class(x)))
x
return(x)
}

########################## read_ddf files - work loops #########################
Expand Down Expand Up @@ -420,8 +420,7 @@ read_ddf <-
rename_cols = list(c(2, 3), c("Position", "Force")),
skip_cols = 4:11,
phase_from_peak = FALSE,
...)
{
...){
# Import and checks
if(missing(filename)) stop("A filename is required")
if(!file.exists(filename)) stop(paste0("File ",filename," not found!"))
Expand Down Expand Up @@ -493,17 +492,17 @@ read_ddf <-
"Stimulus-Tetanus"=read_filetype.ddf<-read_tetanus.ddf,
stop("Could not parse experiment type (workloop, twitch, or tetanus)! Please ensure that the protocol section of the ddf header includes a label with one of the following: Stimulus-Train, Stimulus-Twitch, or Stimulus-Tetanus.")
)
read_filetype.ddf(file_id=file_id,
mtime=mtime,
header=header,
units_table=units_table,
units=units,
protocol_table=protocol_table,
raw_data=dataz,
sample_frequency=sample_frequency,
rename_cols=rename_cols,
skip_cols=skip_cols,
phase_from_peak=phase_from_peak)
return(read_filetype.ddf(file_id=file_id,
mtime=mtime,
header=header,
units_table=units_table,
units=units,
protocol_table=protocol_table,
raw_data=dataz,
sample_frequency=sample_frequency,
rename_cols=rename_cols,
skip_cols=skip_cols,
phase_from_peak=phase_from_peak))
}

#' Import a batch of work loop or isometric data files from a directory
Expand Down Expand Up @@ -581,7 +580,7 @@ read_ddf_dir <- function(filepath,
}
ms_list<-ms_list[order(sapply(ms_list,function(i)attr(i,sort_by)))]

ms_list
return(ms_list)
}

############################# rescale data matrix ##############################
Expand Down Expand Up @@ -610,7 +609,7 @@ rescale_data<-
if(!is.null(rename_cols))
names(rescaled)[rename_cols[[1]]]<-rename_cols[[2]]

rescaled[,-skip_cols]
return(rescaled[,-skip_cols])
}

########################## read_ddf files - workloop ###########################
Expand All @@ -622,8 +621,7 @@ read_wl.ddf<-
sample_frequency,
rename_cols,
skip_cols,
...)
{
...) {
#get info on experimental parameters
stim_table<-
utils::read.table(
Expand All @@ -646,13 +644,13 @@ read_wl.ddf<-
skip_cols)

#construct and return workloop object
workloop(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
cycle_table=cycle_table,
...)
return(workloop(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
cycle_table=cycle_table,
...))
}


Expand All @@ -665,8 +663,7 @@ read_twitch.ddf<-
sample_frequency,
rename_cols=list(c(2,3),c("Position","Force")),
skip_cols=4:11,
...)
{
...) {
#get info on experimental parameters
stim_table<-
utils::read.table(
Expand All @@ -684,12 +681,12 @@ read_twitch.ddf<-
skip_cols)

#construct and return workloop object
twitch(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
...)
return(twitch(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
...))
}


Expand All @@ -702,8 +699,7 @@ read_tetanus.ddf<-
sample_frequency,
rename_cols=list(c(2,3),c("Position","Force")),
skip_cols=4:11,
...)
{
...) {
#get info on experimental parameters
stim_table <-
utils::read.table(
Expand All @@ -722,12 +718,12 @@ read_tetanus.ddf<-
skip_cols)

#construct and return workloop object
tetanus(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
...)
return(tetanus(data=rescaled_data,
sample_frequency=sample_frequency,
units_table=units_table,
protocol_table=protocol_table,
stim_table=stim_table,
...))
}


Expand Down Expand Up @@ -810,7 +806,7 @@ read_analyze_wl <- function(filename,
fulldata<-read_ddf(filename,...)
if(!("workloop" %in% class(fulldata)))
stop(paste0("The provided file ",filename," does not appear to contain data from a workloop experiment!"))
analyze_workloop(select_cycles(fulldata,...),...)
return(analyze_workloop(select_cycles(fulldata,...),...))
}


Expand Down Expand Up @@ -947,7 +943,7 @@ read_analyze_wl_dir <- function(filepath,
warning("The provided sort_by argument is not a valid attribute. Defaulting to `mtime`.")
sort_by<-"mtime"
}
wl_list<-wl_list[order(sapply(wl_list,function(i)attr(i,sort_by)))]
return(wl_list<-wl_list[order(sapply(wl_list,function(i)attr(i,sort_by)))])
}

######################### summarize sequence of work loops #####################
Expand Down Expand Up @@ -1020,16 +1016,14 @@ summarize_wl_trials <- function(wl_list){
if(!all(sapply(wl_list,function(x) 'analyzed_workloop' %in% class(x))))
stop("The provided list includes elements that are not analyzed workloop objects")

data.frame(
File_ID = sapply(wl_list,function(i)attr(i,"file_id")),
Cycle_Frequency = sapply(wl_list,function(i)attr(i,"cycle_frequency")),
Amplitude = sapply(wl_list,function(i)attr(i,"amplitude")),
Phase = sapply(wl_list,function(i)attr(i,"phase")),
Stimulus_Pulses = sapply(wl_list,function(i)attr(i,"stimulus_pulses")),
Stimulus_Frequency = sapply(wl_list,function(i)attr(i,"stimulus_frequency")),
mtime = sapply(wl_list,function(i)attr(i,"mtime")),
Mean_Work = sapply(wl_list,function(i)mean(attr(i,"summary")$Work)),
Mean_Power = sapply(wl_list,function(i)mean(attr(i,"summary")$Net_Power))
)
return(data.frame(File_ID = sapply(wl_list,function(i)attr(i,"file_id")),
Cycle_Frequency = sapply(wl_list,function(i)attr(i,"cycle_frequency")),
Amplitude = sapply(wl_list,function(i)attr(i,"amplitude")),
Phase = sapply(wl_list,function(i)attr(i,"phase")),
Stimulus_Pulses = sapply(wl_list,function(i)attr(i,"stimulus_pulses")),
Stimulus_Frequency = sapply(wl_list,function(i)attr(i,"stimulus_frequency")),
mtime = sapply(wl_list,function(i)attr(i,"mtime")),
Mean_Work = sapply(wl_list,function(i)mean(attr(i,"summary")$Work)),
Mean_Power = sapply(wl_list,function(i)mean(attr(i,"summary")$Net_Power))))
}

2 changes: 1 addition & 1 deletion R/data_transformation_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ select_cycles <- function(x,
x$Cycle<-letters[as.factor(x$Cycle)]
if(!all(is.na(attr(x,"units")))) attr(x,"units")<-c(attr(x,"units"),"letters")
attr(x,"retained_cycles")<-keep_cycles
x
return(x)
}

############################## position inversion ##############################
Expand Down

0 comments on commit 4943ecc

Please sign in to comment.