forked from PYannick/HighFrequencyChecks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassessmentproductivity.R
85 lines (74 loc) · 3.87 KB
/
assessmentproductivity.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# WARNING - Generated by {fusen} from dev/function_documentation.Rmd: do not edit by hand
#' @name assessmentProductivity
#' @rdname assessmentProductivity
#' @title Summary of daily average productivity
#' @description This function display the number of interview conducted per day.
#'
#' @param ds dataset containing the survey (from kobo): labelled data.frame
#' @param checkperiod if not null number of day before today when the check should be made
#' @param surveyConsent name of the field in the dataset where the survey consent is stored: string
#' @param consentForValidSurvey value defined in the kobo form to acknowledge the surveyed person gave his consent: string
#' @param surveyDate name of the field in the dataset where the date of the survey is stored: string
#' @param dateFormat format used for the date: string ('\%m/\%d/\%Y')
#'
#' @return result a list that includes:
#' * dst same dataset as the inputed one but with survey marked for deletion if errors are found and delete=TRUE (or NULL)
#' * ret_log list of the errors found (or NULL)
#' * var a list of value (or NULL)
#' * graph graphical representation of the results (or NULL)
#'
#' @export assessmentProductivity
#' @examples
#' load(system.file("sample_dataset.RData", package = "HighFrequencyChecks"))
#' ds <- sample_dataset
#' surveyDate <- "survey_date"
#' dateFormat <- "%m/%d/%Y"
#' surveyConsent <- "survey_consent"
#' result <- assessmentProductivity(ds = ds,
#' surveyDate = surveyDate,
#' dateFormat = dateFormat,
#' surveyConsent = surveyConsent)
#' knitr::kable(head(result[["ret_log"]], 10))
#' print(result[["graph"]])
#'
assessmentProductivity <- function(ds=NULL,
surveyDate=NULL,
dateFormat=NULL,
surveyConsent=NULL){
if(is.null(ds) | nrow(ds)==0 | !is.data.frame(ds)){
stop("Please provide the dataset")
}
if(is.null(surveyDate) | !is.character(surveyDate)){
stop("Please provide the field where the survey date is stored")
}
if(is.null(dateFormat) | !is.character(dateFormat)){
stop("Please provide the format used for the date ('%m/%d/%Y')")
}
if(is.null(surveyConsent) | !is.character(surveyConsent)){
stop("Please provide the field where the survey consent is stored")
}
tmp <-ds %>%
#dplyr::group_by(surveydate=surveydate) %>%
dplyr::filter(! (is.na(.data[[ surveyDate ]]))) %>%
dplyr::group_by(surveydate=.data[[ surveyDate ]]) %>%
dplyr::summarize(NbSurvey= dplyr::n()) %>%
dplyr::mutate(surveydate = as.factor(format(as.Date(surveydate, dateFormat),
"%d-%m")))
# tmp$surveydate<-as.Date(tmp$surveydate, dateFormat)
ret_log <-tmp[with(tmp, order(surveydate)), ]
graph <- ggplot2::ggplot(tmp) +
ggplot2::geom_col(ggplot2::aes(x = surveydate,
y = NbSurvey)) +
unhcrthemes::theme_unhcr(font_size = 14) +
ggplot2::theme(panel.grid.major.x = ggplot2::element_blank(),
panel.grid.minor.x = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(angle = 45, vjust = 1, hjust = 1)) +
ggplot2::labs(title = "Number of Completed Interviews per day",
x = "Dates",
y = "Number of Interview")
result <- list( dst = NULL, # same dataset as the inputed one but with survey marked for deletion if errors are found and delete = TRUE (or NULL)
ret_log = ret_log , # list of the errors found (or NULL)
var = NULL, # a list of value (or NULL)
graph = graph) # graphical representation of the results (or NULL)
return(result)
}