File: ess-rlib.R

package info (click to toggle)
ess 16.10-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 7,260 kB
  • ctags: 2,341
  • sloc: lisp: 27,692; sh: 1,579; asm: 848; makefile: 368
file content (202 lines) | stat: -rw-r--r-- 8,032 bytes parent folder | download | duplicates (10)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
## My modification of R 1.7.1 functions to help with iESS... Search for
## iESS to see the changes I made.
## source("ess-rlib.R") before running (ess-rpackage "")

print.libraryIQR <- function (x, ...) 
{
  sQuote <- function(s) paste("'", s, "'", sep = "")
  db <- x$results
    out <- if (nrow(db) == 0) 
        NULL
    else lapply(split(1:nrow(db), db[, "LibPath"]), function(ind) db[ind, 
        c("Package", "Title"), drop = FALSE])
    outFile <- tempfile("RlibraryIQR")
    outConn <- file(outFile, open = "w")
    first <- TRUE
    for (lib in names(out)) {
        writeLines(paste(ifelse(first, "", "\n"), "Packages in library ", 
            sQuote(lib), ":\n", sep = ""), outConn)
        if (!is.null(getOption("STERM")) &&
            grep("^iESS", getOption("STERM")) == 1)
          writeLines(formatDL(paste("\\package{", out[[lib]][, "Package"], "}",
                                    sep=''),
                              out[[lib]][, "Title"]), outConn)
        else
          writeLines(formatDL(out[[lib]][, "Package"],
                              out[[lib]][, "Title"]), outConn)
        first <- FALSE
    }
    if (first) {
        close(outConn)
        unlink(outFile)
        writeLines("no packages found")
    }
    else {
        if (!is.null(x$footer)) 
            writeLines(c("\n", x$footer), outConn)
        close(outConn)
        file.show(outFile, delete.file = TRUE, title = "R packages available")
    }
    invisible(x)
}


data <- function (..., list = character(0), package = .packages(),
                  lib.loc = NULL, 
                  verbose = getOption("verbose")) 
{
    sQuote <- function(s) paste("'", s, "'", sep = "")
    names <- c(as.character(substitute(list(...))[-1]), list)
    if (!missing(package)) 
        if (is.name(y <- substitute(package))) 
            package <- as.character(y)
    found <- FALSE
    fsep <- .Platform$file.sep
    paths <- .find.package(package, lib.loc, verbose = verbose)
    if (is.null(lib.loc)) 
        paths <- c(.path.package(package, TRUE), getwd(), paths)
    paths <- unique(paths[file.exists(paths)])
    nodata <- !(file.exists(file.path(paths, "data")) & file.info(file.path(paths, 
        "data"))$isdir)
    if (any(nodata)) {
        if (!missing(package) && (length(package) > 0)) {
            packagesWithNoData <- package[package %in% sapply(paths[nodata], 
                basename)]
            if (length(packagesWithNoData) > 1) {
                warning(paste("packages", paste(sQuote(packagesWithNoData), 
                  collapse = ", "), "contain no datasets"))
            }
            else if (length(packagesWithNoData) == 1) {
                warning(paste("package", sQuote(packagesWithNoData), 
                  "contains no datasets"))
            }
        }
        paths <- paths[!nodata]
    }
    if (length(names) == 0) {
        db <- matrix(character(0), nr = 0, nc = 4)
        noindex <- character(0)
        for (path in paths) {
            entries <- NULL
            if (file.exists(INDEX <- file.path(path, "Meta", 
                "data.rds"))) {
                entries <- .readRDS(INDEX)
            }
            else if (file.exists(INDEX <- file.path(path, "data", 
                "00Index.rds"))) {
                entries <- .readRDS(INDEX)
            }
            else if (file.exists(INDEX <- file.path(path, "data", 
                "00Index.dcf"))) {
                entries <- read.dcf(INDEX)
                entries <- cbind(colnames(entries), c(entries))
            }
            else if (file.exists(INDEX <- file.path(path, "data", 
                "00Index"))) 
                entries <- read.00Index(INDEX)
            else {
                if (length(list.files(file.path(path, "data"))) > 
                  0) 
                  noindex <- c(noindex, basename(path))
            }
            
            if (NROW(entries) > 0) {
              ## SJE
              if (!is.null(getOption("STERM")) &&
                  grep("^iESS", getOption("STERM")) == 1)
                entries[,1] <- paste("\\data{",entries[,1], "}", sep='' )
              
              db <- rbind(db, cbind(basename(path), dirname(path), 
                                    entries))
            }
        }
        colnames(db) <- c("Package", "LibPath", "Item", "Title")
        if (length(noindex) > 0) {
            if (!missing(package) && (length(package) > 0)) {
                packagesWithNoIndex <- package[package %in% noindex]
                if (length(packagesWithNoIndex) > 1) {
                  warning(paste("packages", paste(sQuote(packagesWithNoIndex), 
                    collapse = ", "), "contain data sets but no index"))
                }
                else if (length(packagesWithNoIndex) == 1) 
                  warning(paste("package", sQuote(packagesWithNoIndex), 
                    "contains data sets but no index"))
            }
        }
        footer <- if (missing(package)) 
            paste("Use ", sQuote(paste("data(package =", ".packages(all.available = TRUE))")), 
                "\n", "to list the data sets in all *available* packages.", 
                sep = "")
        else NULL
        y <- list(type = "data", title = "Data sets", header = NULL, 
            results = db, footer = footer)
        class(y) <- "packageIQR"
        return(y)
    }
    paths <- file.path(paths, "data")
    for (name in names) {
        files <- NULL
        for (p in paths) {
            if (file.exists(file.path(p, "Rdata.zip"))) {
                if (file.exists(fp <- file.path(p, "filelist"))) 
                  files <- c(files, file.path(p, scan(fp, what = "", 
                    quiet = TRUE)))
                else warning(paste(sQuote("filelist"), "is missing for dir", 
                  sQuote(p)))
            }
            else {
                files <- c(files, list.files(p, full = TRUE))
            }
        }
        files <- files[grep(name, files)]
        found <- FALSE
        if (length(files) > 1) {
            good <- c("R", "r", "RData", "rdata", "rda", "tab", 
                "txt", "TXT", "csv", "CSV")
            exts <- sub(".*\\.", "", files)
            o <- match(exts, good, nomatch = 100)
            paths <- dirname(files)
            paths <- factor(paths, levels = paths)
            files <- files[order(paths, o)]
        }
        if (length(files) > 0) {
            subpre <- paste(".*", fsep, sep = "")
            for (file in files) {
                if (verbose) 
                  cat("name=", name, ":\t file= ...", fsep, sub(subpre, 
                    "", file), "::\t", sep = "")
                if (found) 
                  break
                found <- TRUE
                ext <- sub(".*\\.", "", file)
                if (sub(subpre, "", file) != paste(name, ".", 
                  ext, sep = "")) 
                  found <- FALSE
                else {
                  zfile <- zip.file.extract(file, "Rdata.zip")
                  switch(ext, R = , r = source(zfile, chdir = TRUE), 
                    RData = , rdata = , rda = load(zfile, envir = .GlobalEnv), 
                    TXT = , txt = , tab = assign(name, read.table(zfile, 
                      header = TRUE), env = .GlobalEnv), CSV = , 
                    csv = assign(name, read.table(zfile, header = TRUE, 
                      sep = ";"), env = .GlobalEnv), found <- FALSE)
                  if (zfile != file) 
                    unlink(zfile)
                }
                if (verbose) 
                  cat(if (!found) 
                    "*NOT* ", "found\n")
            }
        }
        if (!found) 
            warning(paste("Data set", sQuote(name), "not found"))
    }
    invisible(names)
}

## This function needed for ess-rpackage, Sun 14 Sep 2003
funs.for.package <- function(package) {
  x <- .readRDS(file = system.file("Meta", "Rd.rds", package = package))
  writeLines(formatDL(unlist(x[,5]),
                    rep(x[,4], sapply(x[,5], length))))
}