Skip to content

Commit 5fe3dda

Browse files
committed
changed print to warning
1 parent a34b462 commit 5fe3dda

10 files changed

Lines changed: 181 additions & 36 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Description: Converting date ranges into dating 'steps' eases the visualization
88
License: CC BY-SA 4.0
99
Encoding: UTF-8
1010
LazyData: true
11-
RoxygenNote: 6.0.1
11+
RoxygenNote: 7.0.2

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(create.sub.objects)
34
export(datsteps)
5+
export(get.weights)
46
export(scaleweight)
7+
export(switch.dating)

R/datplot_funs.R

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,76 @@
55
#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems
66
#' in any case.
77
#'
8-
#' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row.
8+
#' @param df a dataframe with 4 variables: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row.
99
#' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps.
1010
#'
1111
#' @return a larger dataframe with a number of steps for each object as well as a 'weight' value, that is a quantification of how well the object is dated (lesser value means object is dated to larger timespans, i.e. with less confidence)
1212
#'
1313
#' @export datsteps
1414

15-
datsteps <- function(df, stepsize = 5) {
15+
datsteps <- function(df, stepsize = 25) {
1616
result <- as.data.frame(NULL)
17-
if (any(df[,3] > df[,4]) == TRUE) {
18-
print(paste("Error: Dating seems to be in wrong order at ",
19-
df[which(df[,3] > df[,4]),1],
20-
" (Index: ", which(df[,3] > df[,4]), ")",
21-
". Please supply minimum date in 3rd Column, maximum date in 4th.", sep = ""))
22-
} else {
23-
weights <- get.weights(df[,3], df[,4])
17+
if (stepsize == "auto") {
18+
timespans <- abs(df[,3] - df[,4])
19+
stepsize <- generate.stepsize(timespans)
20+
} else if (!is.numeric(stepsize)) {
21+
stop(error("stepsize has to be either 'auto' or numeric."))
22+
}
2423

24+
if (any(df[,3] > df[,4])) {
25+
warning(paste("Warning: Dating seems to be in wrong order at ID ", paste(df[which(df[,3] > df[,4]),1], collapse = ", "), " (Index: ",
26+
paste(which(df[,3] > df[,4]), collapse = ", "), ")",
27+
". Dates have been switched, but be sure to check your original data for possible mistakes.", sep = ""))
28+
DAT_err <- which(df[,3] > df[,4])
29+
df <- switch.dating(df, DAT_err)
30+
}
2531

26-
if (any(weights[,2] == FALSE)) {
27-
print(paste("Warning: DAT_min and DAT_max in ",
28-
df[which(weights == FALSE),1],
29-
" (Index: ", which(weights == FALSE), ")",
30-
" have the same value! Is this correct? Please check the table for possible errors.", sep = ""))
31-
}
32+
weights <- get.weights(df[,3], df[,4])
3233

33-
df$weight <- weights[,1]
34-
result <- create.sub.objects(df, stepsize)
35-
}
34+
df$weight <- weights[,1]
35+
result <- create.sub.objects(df, stepsize)
3636
return(result)
3737
}
3838

3939

40+
#' Generate stepsize
41+
#'
42+
#' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
43+
#' minimum date (int/numeric) and maximum date (int/numeric).
44+
#'
45+
#' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
46+
#'
47+
#' @return stepsize
48+
#'
49+
#' @export switch.dating
50+
51+
generate.stepsize <- function(timespans) {
52+
stepsize <- min(abs(df[,4] - df[,3]))
53+
if(stepsize < 1) {
54+
stepsize <- 1
55+
}
56+
print(paste("Using stepsize = ", stepsize, " (auto).", sep = ""))
57+
return(stepsize)
58+
}
59+
60+
#' Switch values where dating is in wrong order
61+
#'
62+
#' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
63+
#' minimum date (int/numeric) and maximum date (int/numeric).
64+
#'
65+
#' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
66+
#' @param DAT_err a vector containing the dates in wrong order
67+
#'
68+
#' @return corrected df
69+
#'
70+
#' @export switch.dating
71+
72+
switch.dating <- function(df, DAT_err) {
73+
df[DAT_err,3:4] <- df[DAT_err,4:3]
74+
return(df)
75+
}
76+
77+
4078
#' Calculate the weights for each dated object
4179
#'
4280
#' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
@@ -61,9 +99,18 @@ get.weights <- function(DAT_min, DAT_max) {
6199
weights[which(weights[,1] == 0),1] <- 1
62100
}
63101
weights[,1] <- 1/weights[,1]
102+
if (any(weights[,2] == FALSE)) {
103+
warning(paste("Warning: DAT_min and DAT_max in ID ",
104+
paste(df[which(weights[,2] == FALSE),1], collapse = ", "),
105+
" (Index: ", paste(which(weights[,2] == FALSE), collapse = ", "), ")",
106+
" have the same value! Is this correct? Please check the table for possible errors.", sep = ""))
107+
}
64108
return(weights)
65109
}
66110

111+
112+
113+
67114
#' Create sub-objects for each object in a dataframe
68115
#'
69116
#' Requires a dataframe with 5 variables: ID (ideally factor), group (ideally factor),
@@ -83,17 +130,29 @@ create.sub.objects <- function(df, stepsize) {
83130

84131
mean_year_index <- which(df[,4]-df[,3] < stepsize)
85132

86-
outputnr <- ceiling(sum(((abs(df[-mean_year_index,3]-df[-mean_year_index,4]))/stepsize)+1))
87-
outputnr <- outputnr+length(mean_year_index)
133+
if (length(mean_year_index) == 0) {
134+
outputnr <- ceiling(sum(((abs(df[,3]-df[,4]))/stepsize)+1))
135+
} else {
136+
outputnr <- ceiling(sum(((abs(df[-mean_year_index,3]-df[-mean_year_index,4]))/stepsize)+1))
137+
outputnr <- outputnr+length(mean_year_index)
138+
}
139+
140+
result <- as.data.frame(matrix(ncol = ncol(df)+1, nrow = outputnr+100))
88141

89-
result <- as.data.frame(matrix(ncol = ncol(df)+1, nrow = outputnr))
90142

91143
colnames(result) <- c(colnames(df), "DAT_step")
144+
diffs <- df[,4]-df[,3]
145+
146+
if (any(diffs < stepsize)) {
147+
diffs <- diffs[diffs < stepsize]
148+
warning(paste("stepsize is larger than the range of the closest dated object: ",
149+
paste(df[which(diffs < stepsize),1], collapse = ", "), " (Index = ",
150+
paste(which(diffs < stepsize), collapse = ", "), "). Using mean as year.", sep = ""))
151+
}
152+
92153
for (i in 1:nrow(df)) {
93154
sequence <- NULL
94155
if ((df[i,4]-df[i,3]) < stepsize) {
95-
print(paste("stepsize is larger than the range of the closest dated object: ",
96-
df[i,1], " (Index = ", i, "). Using mean as year.", sep = ""))
97156
sequence <- (df[i,3]+df[i,4])/2
98157
} else {
99158
sequence <- seq(df[i,3], df[i,4], by = stepsize)

datplot.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
1818
BuildType: Package
1919
PackageUseDevtools: Yes
2020
PackageInstallArgs: --no-multiarch --with-keep.source
21+
PackageRoxygenize: rd,collate,namespace

man/create.sub.objects.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/datsteps.Rd

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/generate.stepsize.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get.weights.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scaleweight.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/switch.dating.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)