Merge branch 'master' of git.curoverse.com:arvados into 13076-r-autogen-api
[arvados.git] / sdk / R / R / util.R
1 trimFromStart <- function(sample, trimCharacters)
2 {
3     if(startsWith(sample, trimCharacters))
4         sample <- substr(sample, nchar(trimCharacters) + 1, nchar(sample))
5
6     sample
7 }
8
9 trimFromEnd <- function(sample, trimCharacters)
10 {
11     if(endsWith(sample, trimCharacters))
12         sample <- substr(sample, 0, nchar(sample) - nchar(trimCharacters))
13
14     sample
15 }
16
17 RListToPythonList <- function(RList, separator = ", ")
18 {
19     pythonArrayContent <- sapply(RList, function(elementInList)
20     {
21         if((is.vector(elementInList) || is.list(elementInList)) &&
22             length(elementInList) > 1)
23         {
24             return(RListToPythonList(elementInList, separator))
25         }
26         else
27         {
28             return(paste0("\"", elementInList, "\""))
29         }
30     })
31
32     pythonArray <- paste0("[", paste0(pythonArrayContent, collapse = separator), "]")
33     pythonArray
34 }
35
36 appendToStartIfNotExist <- function(sample, characters)
37 {
38     if(!startsWith(sample, characters))
39         sample <- paste0(characters, sample)
40
41     sample
42 }
43
44 splitToPathAndName = function(path)
45 {
46     path <- appendToStartIfNotExist(path, "/")
47     components <- unlist(stringr::str_split(path, "/"))
48     nameAndPath <- list()
49     nameAndPath$name <- components[length(components)]
50     nameAndPath$path <- trimFromStart(paste0(components[-length(components)], collapse = "/"),
51                                       "/")
52
53     nameAndPath
54 }