Skip to content

Commit

Permalink
updated progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
rosieluain committed Jun 28, 2023
1 parent f8657de commit cde9c4a
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 144 deletions.
115 changes: 80 additions & 35 deletions R/mort.R

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
#' @examples
#' morts<-morts(data=events,type="mort",ID="ID",station="Station.Name",
#' method="any")
#' head(morts)
#'
#' # If station change not identified yet:
#' morts_bw<-backwards(data=events,morts=morts,ID="ID",
#' station="Station.Name",res.start="ResidenceStart")
#' head(morts_bw)
#'
#' # Identify station change first:
#' station.change<-stationchange(data=events,type="mort",
Expand All @@ -42,6 +44,7 @@
#' morts_bw<-backwards(data=events,morts=morts,ID="ID",
#' station="Station.Name",res.start="ResidenceStart",
#' stnchange=station.change)
#' head(morts_bw)
backwards<-function(data,morts,ID,station,res.start,stnchange=NULL){
if (any(data[[station]]=="Break")){
warning("Either a station name was 'Break' or data included seasonal
Expand Down Expand Up @@ -137,13 +140,19 @@ backwards<-function(data,morts,ID,station,res.start,stnchange=NULL){
#' @export
#'
#' @examples
#' drift.events<-drift(data=events,type="mort",ID="ID",
#' station="Station.Name",ddd=ddd,from.station="From",to.station="To")
#' # With no drift:
#' head(events)
#'
#' drift.events<-drift(data=events[events$ID=="A",],type="mort",ID="ID",
#' station="Station.Name",ddd=ddd,from.station="From",to.station="To",
#' progress.bar=FALSE)
#' head(drift.events)
#'
#' # With cutoff:
#' drift.events<-drift(data=events,type="mort",ID="ID",
#' drift.events<-drift(data=events[events$ID=="A",],type="mort",ID="ID",
#' station="Station.Name",ddd=ddd,from.station="From",to.station="To",
#' cutoff=1,cutoff.units="days")
#' cutoff=1,cutoff.units="days",progress.bar=FALSE)
#' head(drift.events)
drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
residences="auto",units="auto",ddd,from.station,to.station,
cutoff=NULL,cutoff.units=NULL,progress.bar=TRUE){
Expand Down Expand Up @@ -301,7 +310,7 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
#' residence events that is within the period of interest will be retained,
#' and `residences` will be recalculated, using specified `units`.
#' Default is `TRUE`.
#' @param progressbar option to display progress bar as function is run. Default
#' @param progress.bar option to display progress bar as function is run. Default
#' is TRUE.
#'
#' @return a dataframe in the same format as the input data, with residence
Expand All @@ -311,16 +320,19 @@ drift<-function(data,type,ID,station,res.start="auto",res.end="auto",
#' @examples
#' # Seasons in format dd-mm
#' season.events<-season(data=events,type="mort",ID="ID",
#' station="Station.Name",season.start="01-06",season.end="31-10")
#' station="Station.Name",season.start="01-06",season.end="31-10",
#' progress.bar=FALSE)
#' head(season.events)
#'
#' # Seasons in format YYYY-mm-dd HH:MM:SS
#' season.start<-c("2003-06-15","2004-06-21")
#' season.end<-c("2003-10-15","2004-10-30")
#' season.events<-season(data=events,type="mort",ID="ID",
#' station="Station.Name",season.start=season.start,season.end=season.end)
#' station="Station.Name",season.start=season.start,season.end=season.end,progress.bar=FALSE)
#' head(season.events)
season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
residences="auto",units="auto",season.start,
season.end,overlap=TRUE,progressbar=TRUE){
season.end,overlap=TRUE,progress.bar=TRUE){

if (type %in% c("actel","vtrack")){
data<-extractres(data=data,type=type)
Expand Down Expand Up @@ -423,11 +435,11 @@ season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
data.season<-data[0,]

for (i in 1:length(season.start)){
if (progressbar==TRUE){
if (progress.bar==TRUE){
print(paste("season/period",i,"of",length(season.start)))
}
if (length(tag)>1){
if (progressbar==TRUE){
if (progress.bar==TRUE){
pb<-txtProgressBar(1,length(tag),style=3)
}
}
Expand Down Expand Up @@ -471,7 +483,7 @@ season<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
}
data.season<-rbind(data.season,data.temp)
if (length(tag)>1){
if (progressbar==TRUE){
if (progress.bar==TRUE){
setTxtProgressBar(pb,j)
}
}
Expand Down
15 changes: 8 additions & 7 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#' mortalities. Plotting uses ggplot2. Interactive option also uses plotly.
#'
#' @param data a dataframe of residence events. Residence events must include
#' tag ID, location name, start time, and end time. Residence events must also
#' include duration if making an interactive plot.
#' tag ID, location name, start time, and end time.
#' @param type the method used to generate the residence events. Options are
#' "mort", "actel", "glatos", "vtrack", or "manual".
#' @param ID a string of the name of the column in `data` that holds the tag or
Expand Down Expand Up @@ -45,7 +44,7 @@
#' `facet.by="year"`.
#' @param facet.by option to facet by "season" (as defined with `season.start`
#' and `season.end`) or "year". Default is "season".
#' @param progressbar option to display progress bar as function is run. Default
#' @param progress.bar option to display progress bar as function is run. Default
#' is TRUE.
#'
#' @return a ggplot2 plot. Additional arguments (e.g., formatting axes,
Expand All @@ -57,19 +56,21 @@
#'
#' @examples
#' plot<-mortsplot(data=events,type="mort",ID="ID",station="Station.Name")
#' plot
#'
#' # With mortalities plotted over residences:
#' morts<-morts(data=events,type="mort",ID="ID",station="Station.Name",
#' method="any")
#' method="any",progress.bar=FALSE)
#'
#' plot<-mortsplot(data=events,type="mort",ID="ID",station="Station.Name",
#' morts=morts)
#' plot
mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
morts=NULL,singles=TRUE,interactive=FALSE,residences=NULL,
units=NULL,
season.start=NULL,season.end=NULL,facet=FALSE,
facet.axis="x",facet.by="season",
progressbar=TRUE){
progress.bar=TRUE){

if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("Package \"ggplot2\" must be installed to use this function.",
Expand Down Expand Up @@ -175,7 +176,7 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
if (units=="auto"){
units<-autofield(type=type,field="units",data=data)
}
if (progressbar==TRUE){
if (progress.bar==TRUE){
print("Extracting data from the period/season(s) of interest")
}
if (is.null(season.start)&is.null(season.end)&facet.by=="year"){
Expand All @@ -184,7 +185,7 @@ mortsplot<-function(data,type,ID,station,res.start="auto",res.end="auto",
}
data<-season(data=data,type=type,ID=ID,station=station,res.start=res.start,res.end=res.end,
residences=residences,units=units,season.start=season.start,
season.end=season.end,overlap=FALSE,progressbar=FALSE)
season.end=season.end,overlap=FALSE,progress.bar=progress.bar)
data<-data[data[[station]]!="Break",]
if (!is(season.start,"POSIXt")){
try(season.start<-as.POSIXct(season.start,tz="UTC"),silent=TRUE)
Expand Down
18 changes: 13 additions & 5 deletions R/residences.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' @param ID a string of the name of the column in `data` that holds the tag or sample IDs.
#' @param datetime a string of the name of the column in `data` that holds the date and time.
#' @param station a string of the name of the column in `data` that holds the station name or receiver location.
#' @param progress.bar option to display progress bar as residences are generated.
#' Default is TRUE.
#'
#' @return A data frame with one row for each residence event, including date/time of
#' residence start, date/time of residence end, and duration of residence event. All
Expand All @@ -23,10 +25,12 @@
#' @importFrom utils setTxtProgressBar txtProgressBar
#'
#' @examples
#' res.events<-residences(data=detections,ID="ID",station="Station.Name",
#' datetime="DateTimeUTC",cutoff=1,units="days")
#' head(detections)
#' res.events<-residences(data=detections[1:500,],ID="ID",station="Station.Name",
#' datetime="DateTimeUTC",cutoff=1,units="days",progress.bar=FALSE)
#' head(res.events)

residences<-function(data,ID,station,datetime,cutoff,units){
residences<-function(data,ID,station,datetime,cutoff,units,progress.bar=TRUE){
# Create list of unique IDs
tag<-unique(na.omit(data[[ID]]))

Expand All @@ -51,7 +55,9 @@ residences<-function(data,ID,station,datetime,cutoff,units){
# Set up res
res<-cbind(data[0,],ResidenceEnd=as.POSIXct(as.character()))

pb<-txtProgressBar(1,length(tag),style=3)
if (progress.bar==TRUE){
pb<-txtProgressBar(1,length(tag),style=3)
}
for (i in 1:length(tag)){
# Subset by ID
res.sub<-data[data[[ID]]==tag[i],]
Expand Down Expand Up @@ -109,7 +115,9 @@ residences<-function(data,ID,station,datetime,cutoff,units){
break
}
}
setTxtProgressBar(pb,i)
if (progress.bar==TRUE){
setTxtProgressBar(pb,i)
}
}
colnames(res)[colnames(res)==datetime]<-"ResidenceStart"
attributes(res$ResidenceStart)$tzone<-attributes(data[[datetime]])$tzone
Expand Down
13 changes: 8 additions & 5 deletions R/review.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' alive.
#'
#' @param morts a dataframe with previously flagged mortalities. Format does
#' not need to match `new.data`, but the names and formats of `ID`, `station`,
#' not need to match `new.data` exactly, but the names and formats of `ID`, `station`,
#' and `res.start` fields must match in all input dataframes.
#' @param new.data a dataframe of new residence events (i.e., generated from
#' detection data that were not included in earlier `mort` analyses).
Expand Down Expand Up @@ -37,6 +37,8 @@
#' @param residences an optional character string with the name of the column
#' in `morts` and `new.data` that holds the duration of the residence events.
#' Only needed if drift is applied.
#' @param progress.bar option to display progress bar as function and called
#' functions are run. Default is TRUE.
#'
#' @return A dataframe with one row for each tag ID from `morts`
#' with a station/location change that was identified
Expand All @@ -49,13 +51,14 @@
#'
#' @examples
#' morts<-morts(data=events,type="mort",ID="ID",station="Station.Name",
#' method="any")
#' method="any",progress.bar=FALSE)
#'
#' undead<-review(morts=morts,new.data=new.data,
#' type="mort",ID="ID",station="Station.Name")
#' type="mort",ID="ID",station="Station.Name",progress.bar=FALSE)
review<-function(morts,new.data,old.data=NULL,type,ID,station,res.start="auto",
res.end=NULL,residences=NULL,units=NULL,
ddd=NULL,from.station=NULL,to.station=NULL){
ddd=NULL,from.station=NULL,to.station=NULL,
progress.bar=TRUE){

if (type %in% c("actel","vtrack")){
new.data<-extractres(data=new.data,type=type)
Expand Down Expand Up @@ -199,7 +202,7 @@ review<-function(morts,new.data,old.data=NULL,type,ID,station,res.start="auto",
res.start=res.start,res.end=res.end,
ddd=ddd,from.station=from.station,to.station=to.station,
residences=residences,
units=units)
units=units,progress.bar=progress.bar)
for (i in 1:nrow(morts)){
res.temp<-data[data[[ID]]==morts[[ID]][i],]
j<-1
Expand Down
Loading

0 comments on commit cde9c4a

Please sign in to comment.