Showcasing import_gtfs() #6
Replies: 4 comments 20 replies
-
Great work, @dhersz . Thanks for the function overview here! As I mentioned on issue #4 , I believe it is more appropriate if ps. I do like how the function is flexible to read additional files/fields not specivied in the GTFS format though. |
Beta Was this translation helpful? Give feedback.
-
Worked on your suggestions and feedback. As of f4adeb5 I also created the Using the same example provided above: library(gtfsio)
data_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(data_path, files = c("levels"))
gtfs$levels
#> level_id level_index level_name elevation
#> 1: L0 0 Street 0
#> 2: L1 -1 Mezzanine -6
#> 3: L2 -2 Southbound -18
#> 4: L3 -3 Northbound -24
class(gtfs$levels$elevation)
#> [1] "character"
gtfs <- import_gtfs(
data_path,
files = "levels",
extra_spec = list(levels = c(elevation = "integer"))
)
gtfs$levels
#> level_id level_index level_name elevation
#> 1: L0 0 Street 0
#> 2: L1 -1 Mezzanine -6
#> 3: L2 -2 Southbound -18
#> 4: L3 -3 Northbound -24
class(gtfs$levels$elevation)
#> [1] "integer" Note that you cannot use this argument to overwrite the standards: gtfs <- import_gtfs(
data_path,
files = "levels",
extra_spec = list(levels = c(level_index = "character"))
)
#> Error in FUN(X[[i]], ...): The following field(s) from the 'levels' file were specified in 'extra_spec' but are already documented in the official GTFS reference: 'level_index' It also complains if a field is specified in gtfs <- import_gtfs(
data_path,
files = "levels",
fields = list(levels = c("level_index", "level_id")),
extra_spec = list(levels = c(elevation = "integer"))
)
#> Error in FUN(X[[i]], ...): The following files were specified in 'extra_spec' but either were not specified in 'fields' or do no exist: 'elevation' How do you like it? |
Beta Was this translation helpful? Give feedback.
-
Hi all. Have we all agreed on this function? I wondering what else needs to be done before we make our first CRAN submission. Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
Just a thought: Could a parameter that lists files that should not be read (like import_gtfs("feed.zip", ignore_files = c("shapes", "stop_times")) than import_gtfs("feed.zip", files = c("agency", "stops", "routes", "trips", "calendar", "calendar_dates", "transfers", "feed_info")) |
Beta Was this translation helpful? Give feedback.
-
Hello @tbuckl, @mpadge, @rafapereirabr, @polettif.
I'm creating this discussion to showcase
import_gtfs()
, and to ask for some feedback from you. @rafapereirabr has already opened an issue on some of the things I'll briefly show you.First things first, since
{gtfsio}
is aimed at package authors who maintain GTFS-related packages, I'm trying to maintain the dependencies to a minimum. Currently it only imports{data.table}
, to read and manipulate files,{utils}
(download.file()
) and{zip}
(I had some problems usingutils::unzip()
before, and{zip}
is a dependency-free alternative that doesn't rely on any installed files-compactor software).The package includes a small sample GTFS file built based on and example feed provided by Google.
Basic usage
Just input the path to a GTFS text file. By default, it reads all text files and fields:
path
also accepts an URL:You can specify which files to read with
files
(must be a character vector):And, similarly, which fields to read with
fields
(must be a named list, each element being a character vector):fields
argument only impacts specified fields, as expected:Adjust message output with
quiet
:The function returns a
gtfs
object. I'm still working on the methods, constructors and validators for the class, but you can have a look on the direction I'm going to at R/gtfs.R.Undocumented fields and files
Undocumented fields and files (i.e. not specified in the official GTFS reference) are always read as characters (i.e. data types are never guessed by
data.table::fread()
).Funny enough, the Google example includes an undocumented field in
levels.txt
(elevation
):Raising exceptions
Basic input checking aside, the function might also throw some errors and warnings. To be honest, I refer to them as "exceptions" in the code because I'm not sure if they should be errors or warnings. I wanted to hear from you what you think, since the code is meant to be used by you :). I thought about creating a
warnings_as_errors
argument, or something along those lines, to convert these warnings to errors, and ultimately leaving the choice of which exception to raise to package maintainers, but that wouldn't be necessary if we all agree on something from the beginning.If you specify a file that does not exist in
files
, it will throw a warning.Unless none of the files you specified exist. In such case, it will raise the warning and an error (this error is actually not that optional, because
zip::unzip()
also throws an error when neither file exist - I just created this error to be more explicit on why this error was thrown, becausezip::unzip()
error message is less clear):Also, if you specify a file in
fields
, but not infiles
(or, alternatively, you meant to read all files, but the one specified infields
doesn't exist), it raises a warning:If you specify some fields in
fields
that do not exist, it also raises a warning. If none of the specified fields exist, the function returns a NULL data.table:So, what do you think about these exceptions? In
{gtfstools}
I treat all of them as errors, if I recall correctly. Not sure what you prefer.Final remarks
I haven't covered every detail of the function here. I suggest you taking a quick look at the code if you are interested in knowing every small corner of it :P
I like this feature showcasing discussion format. I think it's a good way to communicate and discuss function/classes/etc behaviour, both expected and observed. What do you think? I plan on creating a similar one to showcase the
gtfs
class soon.Cheers!
Beta Was this translation helpful? Give feedback.
All reactions