Skip to content

Commit

Permalink
Add has_nycflights13 function.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 9, 2016
1 parent ef923b6 commit 0499a1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export(group_size)
export(grouped_df)
export(groups)
export(has_lahman)
export(has_nycflights13)
export(id)
export(ident)
export(inner_join)
Expand Down
14 changes: 14 additions & 0 deletions R/data-nycflights13.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ nycflights13_postgres <- function(dbname = "nycflights13", ...) {
})
}

#' @rdname nycflights13
#' @export
has_nycflights13 <- function(type = c("sqlite", "postgresql"), ...) {
if (!requireNamespace("nycflights13", quietly = TRUE)) return(FALSE)

type <- match.arg(type)

succeeds(switch(type,
sqlite = nycflights13_sqlite(...), quiet = TRUE,
postgres = nycflights13_postgres(...), quiet = TRUE
))
}


#' @export
#' @rdname nycflights13
copy_nycflights13 <- function(src, ...) {
Expand Down
3 changes: 3 additions & 0 deletions man/nycflights13.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vignettes/databases.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Aside from SQLite, the overall workflow is essentially the same regardless of th
For example, the following code allows me to connect to a local PostgreSQL database that contains a copy of the `flights` data:

```{r}
if (has_lahman("postgres")) {
if (has_nycflights13("postgres")) {
flights_postgres <- tbl(src_postgres("nycflights13"), "flights")
}
```
Expand All @@ -227,7 +227,7 @@ PostgreSQL is a considerably more powerful database than SQLite. It has:
The following examples show how we can perform grouped filter and mutate operations with PostgreSQL. Because you can't filter on window functions directly, the SQL generated from the grouped filter is quite complex; so they instead have to go in a subquery.

```{r}
if (has_lahman("postgres")) {
if (has_nycflights13("postgres")) {
daily <- group_by(flights_postgres, year, month, day)
# Find the most and least delayed flight each day
Expand Down

0 comments on commit 0499a1a

Please sign in to comment.