Description Usage Arguments Value Implementation Unit tests Author(s) See Also Examples
Retrieves the first or last few records from a dbframe. This function mimics the corresponding data frame methods.
1 2 3 4 5 |
x |
A |
n |
An integer. If positive, the number of records to retrieve. If negative, these functions will retrieve all but n records. |
... |
Other arguments to pass to select |
Returns a data frame with the records.
head and tail are really basic functions; the implementation is pretty straightforward. The only complication is that the “...” arguments can be passed multiple times, so I pull them out and store them in a list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <<*>>=
head.dbframe <- function(x, n = 6L,...) {
if (n >= 0) {
<<Return the first |n| records>>
} else {
<<Return all but the last |n| records>>
}
}
tail.dbframe <- function(x, n = 6L,...) {
if (n >= 0) {
<<Return the last |n| records>>
} else {
<<Return all but the first |n| records>>
}
}
|
We use select statements with “limit” and “offset” to get the records.
1 2 | <<Return the first |n| records>>=
return(select(x, limit = n, as.data.frame = TRUE,...))
|
1 2 |
1 2 3 |
1 2 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <<test-head.R>>=
library(testthat)
filename <- tempfile(fileext = ".db")
data(morley)
test_that("head and tail return the right number of records", {
dbf <- dbframe("tab1", dbname = filename, data = morley)
expect_that(nrow(head(dbf)), equals(6))
expect_that(nrow(tail(dbf)), equals(6))
nrec <- sample(1:nrow(morley), 1)
expect_that(nrow(head(dbf, nrec)), equals(nrec))
expect_that(nrow(tail(dbf, nrec)), equals(nrec))
expect_that(nrow(head(dbf, -nrec)), equals(nrow(morley) - nrec))
expect_that(nrow(tail(dbf, -nrec)), equals(nrow(morley) - nrec))
})
|
Gray Calhoun gcalhoun@iastate.edu
1 2 3 4 5 6 7 8 | |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.