This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from Edouard-Legoupil/koboloadeR
/
kobo_get_dataframes_levels.R
116 lines (112 loc) · 3.59 KB
/
kobo_get_dataframes_levels.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#' @name kobo_get_dataframes_levels
#' @rdname kobo_get_dataframes_levels
#' @title Dataframes Levels
#'
#' @description Produce a dataframe that represents levels and parents for the main dataframe and all sub datasets.
#'
#' @param form The full filename of the form to be accessed (xls or xlsx file).
#' It is assumed that the form is stored in the data folder.
#'
#'
#' @return A "data.frame" contains levels and parents for the main dataframe and all sub datasets.
#'
#' @author Maher Daoud
#'
#'
#' @examples
#' \dontrun{
#' kobo_get_dataframes_levels("myform.xlsx")
#' }
#'
#' @export kobo_get_dataframes_levels
#'
kobo_get_dataframes_levels <- function(form="form.xlsx") {
tryCatch({
mainDir <- kobo_getMainDirectory()
#form_tmp <- paste(mainDir, "data", form, sep = "/", collapse = "/")
form_tmp <- paste(mainDir, "data-raw", form, sep = "/", collapse = "/")
survey <- tryCatch({
as.data.frame(readxl::read_excel(form_tmp, sheet = "survey"),
stringsAsFactors = FALSE) #read survey sheet from the form
}, error = function(err) {
data.frame( #if it doesn't exist, we need to create empty dataframe with those fields
type = character(),
name = character(),
label = character(),
variable = character(),
disaggregation = character(),
chapter = character(),
structuralequation.risk = character(),
structuralequation.coping = character(),
structuralequation.resilience = character(),
anonymise = character(),
correlate = character(),
clean = character(),
cluster = character(),
predict = character(),
mappoint = character(),
mappoly = character(),
stringsAsFactors = FALSE
)
})
survey <- survey[c("name","type")]
survey$type <- tolower(survey$type)
survey$type <- stringr::str_replace(survey$type,"_"," ")
survey$type <- stringr::str_replace(survey$type,"-"," ")
survey <- survey[!is.na(survey$type),]
survey <- survey[survey$type=="begin repeat" | survey$type=="end repeat", ]
if(nrow(survey)==0){
return(data.frame(
name = "MainDataFrame",
level = 1,
parent = "root"
,stringsAsFactors = F
))
}
result <- data.frame(
name = "MainDataFrame",
level = 1,
parent = "root"
,stringsAsFactors = F
)
opcl <- data.frame(
name = character(),
open = logical(),
stringsAsFactors = F
)
for(i in 1:nrow(survey)){
st <- survey$type[i]
sn <- ifelse(st=="begin repeat",survey$name[i],NA)
if(st=="begin repeat"){
temp <- opcl[opcl$open==T,]
if(nrow(temp)==0){
result <- rbind(result,
c(sn, 2, "MainDataFrame" )
)
}else{
tempName <- temp[nrow(temp),"name"]
result <- rbind(result,
c(sn, as.integer(result[result$name==tempName,"level"]) + 1, tempName)
)
}
opcl <- rbind(opcl,
data.frame(
name = sn,
open = T,
stringsAsFactors = F
)
)
}else if(st=="end repeat"){
temp <- opcl[opcl$open==T,]
tempName <- temp[nrow(temp),"name"]
opcl[opcl$name == tempName,"open"] = F
}
}
result$level <- as.integer(result$level)
result <- result[order(result$level, result$parent),]
return(result)
}, error = function(err) {
print("kobo_get_dataframes_levels_ERROR")
return(structure(err, class = "try-error"))
})
}