forked from lebebr01/SPSStoR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetToR.r
93 lines (78 loc) · 3.25 KB
/
getToR.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
#' Get to R
#'
#' Converts SPSS get syntax to R syntax
#'
#' @param x SPSS syntax - read in by SPSStoR function
#' @export
get_to_r <- function(x){
fileLoc <- grep("file\\s?=", x, ignore.case = TRUE)
if(any(grepl('\"', x)) == TRUE){
path <- substr(x[fileLoc], (which(strsplit(x[fileLoc], '')[[1]]=='\"')[1]),
(which(strsplit(x[fileLoc], '')[[1]]=='\"')[2]))
} else {
path <- substr(x[fileLoc], (which(strsplit(x[fileLoc], '')[[1]]=='\'')[1]),
(which(strsplit(x[fileLoc], '')[[1]]=='\'')[2]))
}
rx <- paste0("x <- read.spss(", path, ", to.data.frame = TRUE)")
finMat <- paste("library(foreign)", rx, sep = "\n")
finMat
}
#' Get Data to R
#'
#' Converts SPSS Get Data command to R syntax. Available for delimited or
#' excel data files.
#'
#' @param x SPSS syntax - read in by SPSStoR function
#' @export
getdata_to_r <- function(x) {
fileLoc <- grep("file\\s?=", x, ignore.case = TRUE)
if(any(grepl('\"', x)) == TRUE){
path <- substr(x[fileLoc], (which(strsplit(x[fileLoc], '')[[1]]=='\"')[1]),
(which(strsplit(x[fileLoc], '')[[1]]=='\"')[2]))
} else {
path <- substr(x[fileLoc], (which(strsplit(x[fileLoc], '')[[1]]=='\'')[1]),
(which(strsplit(x[fileLoc], '')[[1]]=='\'')[2]))
}
typeLoc <- grep("type\\s?=", x, ignore.case = TRUE)
typeVal <- substr(x[typeLoc], (which(strsplit(x[typeLoc], '')[[1]]=='=')+1),
nchar(x[typeLoc]))
if(grepl("TXT|txt", typeVal) == TRUE){
firstcase <- grep("firstcase\\s?=", x, ignore.case = TRUE)
num <- as.numeric(substr(x[firstcase], (which(strsplit(x[firstcase], '')[[1]]=='=')+1),
nchar(x[firstcase])))
header <- paste0("skip = ", num)
delimLoc <- grep("delimiters\\s?=", x, ignore.case = TRUE)
if(any(grepl('\"', x[delimLoc])) == TRUE){
delim <- substr(x[delimLoc], (which(strsplit(x[delimLoc], '')[[1]]=='\"')[1]),
(which(strsplit(x[delimLoc], '')[[1]]=='\"')[2]))
} else {
delim <- substr(x[delimLoc], (which(strsplit(x[delimLoc], '')[[1]]=='\'')[1]),
(which(strsplit(x[delimLoc], '')[[1]]=='\'')[2]))
}
finMat <- paste0("x <- read.table(", path, ", sep = ", delim, ", ", header, ")")
finMat
} else {
if(grepl("XLS|xls|XLSX|xlsx", typeVal) == TRUE) {
sheetLoc <- grep("sheet\\s?=", x, ignore.case = TRUE)
if(grepl("name", x[sheetLoc], ignore.case = TRUE) == TRUE){
if(any(grepl('\"', x[sheetLoc])) == TRUE){
sheetT <- substr(x[sheetLoc], (which(strsplit(x[sheetLoc], '')[[1]]=='\"')[1]),
(which(strsplit(x[sheetLoc], '')[[1]]=='\"')[2]))
sheetN <- NULL
} else {
sheetT <- substr(x[sheetLoc], (which(strsplit(x[sheetLoc], '')[[1]]=='\'')[1]),
(which(strsplit(x[sheetLoc], '')[[1]]=='\'')[2]))
sheetN <- NULL
}
} else {
sheetN <- gsub('[^0-9]', '', x[sheetLoc])
sheetT <- NULL
}
rx <- paste0("x <- read.xlsx(", path, ", ", sheetT, sheetN, ")")
finMat <- paste("library(xlsx)", rx, sep = "\n")
finMat
} else {
message("Data for type ODBC or OLEDB are not supported")
}
}
}