r-transit / gtfsio

Read and Write General Transit Feed Specification (GTFS)
https://r-transit.github.io/gtfsio/
Other
13 stars 3 forks source link

add summary.gtfs method #20

Closed mpadge closed 3 years ago

mpadge commented 3 years ago

@dhersz Would it be okay if this summary method gets moved from gtfsrouter to here? I think a summary method is really useful here, especially to quickly check how big the tables are.

dhersz commented 3 years ago

Yup, a summary() method is definitely useful. Many thanks.

What do you think of using cat() instead of message() there? I think message() is perhaps better suited for handling conditions and making diagnostics, an cat() for printing things.

This is the behaviour when using capture.output() right now:

a <- capture.output(summary(gtfs))
#> A gtfs object with the following tables and respective numbers of entries in each:
a
#> [1] " calendar_dates fare_attributes      fare_rules       feed_info     frequencies          levels "
#> [2] "              4               5              10               1               3               4 "
#> [3] "       pathways          routes          shapes      stop_times           stops       transfers "
#> [4] "             19               1               3              11              16               3 "
#> [5] "   translations           trips          agency    attributions        calendar "                
#> [6] "              3               2               1               2               2 " 

When using cat() it feels more natural (oops I forgot to add an \n there):

summary.gtfs <- function (object, ...) {

    cat ("A gtfs object with the following tables ",
         "and respective numbers of entries in each:")

    print (vapply (object, nrow, numeric (1)))
}

gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)

a <- capture.output(summary(gtfs))
a
#> [1] "A gtfs object with the following tables  and respective numbers of entries in each: calendar_dates fare_attributes      fare_rules       feed_info     frequencies "
#> [2] "              4               5              10               1               3 "                                                                                   
#> [3] "         levels        pathways          routes          shapes      stop_times "                                                                                   
#> [4] "              4              19               1               3              11 "                                                                                   
#> [5] "          stops       transfers    translations           trips          agency "                                                                                   
#> [6] "             16               3               3               2               1 "                                                                                   
#> [7] "   attributions        calendar "                                                                                                                                   
#> [8] "              2               2 "
mpadge commented 3 years ago

What do you think of using cat() instead of message() there?

Yeah, sure. I generally just follow the rOpenSci guide which discourages cat, but then summary is effectively a print method, and relies on print for the rest anyway, so i'll modify now. Thanks!

dhersz commented 3 years ago

Thanks!