Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ps_read_vrl family? #8

Open
sebdalgarno opened this issue Feb 3, 2018 · 5 comments
Open

add ps_read_vrl family? #8

sebdalgarno opened this issue Feb 3, 2018 · 5 comments
Assignees

Comments

@sebdalgarno
Copy link
Member

sebdalgarno commented Feb 3, 2018

Vrl output csv come in a standard (messy) format and are therefore easy to get into a format that we want.
similar to other read functions, ps_read_vrl takes a single file. and ps_read_vrls takes a directory. A tz argument allows automatic conversion of datetime column into desired tz.

ps_read_vrl <- function(path = '~/Dropbox/Data/quesnel-exploitation/20170208/test.csv', tz = getOption("ps.tz", "UTC"),
                        datetime_column = "DateTimeDetection", receiver_column = "Receiver", transmitter_column = "Transmitter") {
  # need to check that the selected csv fits standard format of vrl output
  data <- readr::read_csv(path) %>% 
    select(1:3)
  colnames(data) <- c(datetime_column, receiver_column, transmitter_column)
  data[[datetime_column]] %<>% with_tz(tz = tz)
  data
}

ps_read_vrls <- function(dir = ".", tz = getOption("ps.tz", "UTC")) {
  files <- list.files(path = dir, pattern = "[.]csv$", full.names = TRUE, recursive = TRUE)
  data <- purrr::map_dfr(files, ~ ps_read_vrl(., tz = tz))
  data
}
@joethorley
Copy link
Member

Good idea although I'm thinking we only need a ps_read_vrl and it can be combined with the general function ps_load_files - thoughts?

@sebdalgarno
Copy link
Member Author

sebdalgarno commented Feb 3, 2018

I'm not sure ps_load_files is what we want for this. I want to take a directory and combine all files within that directory into a single data.frame which can be named...more like what ps_read_tracks_gpxs does.

@sebdalgarno sebdalgarno self-assigned this Feb 3, 2018
@joethorley
Copy link
Member

if we were to use ps_load_files to pull into a clean environment and then ps_bind_datas this would get us there without having to write a specific multi file function for each file type.

an alternative option might be to use a function factory to generate them http://adv-r.had.co.nz/Functional-programming.html

My issue with manually writing functions with identical code save the bit to read in an individual file is that its at odds with the do not repeat yourself (DRY) principle

@sebdalgarno
Copy link
Member Author

sebdalgarno commented Feb 5, 2018

good points.
Perhaps a good option is a simple wrapper of list.files with useful default args, in conjunction with map_dfr:

vrl_df <- map_dfr(ps_list_files("~/Poisson/..."), ~ ps_read_vrl(.))

where defaults args are:
pattern = "[.]csv$", full.names = TRUE, recursive = TRUE

I might not be seeing the full utility of the ps_load_files/ps_bind_datas combo...

@joethorley
Copy link
Member

let's chat about more in person when we meet to discuss the ssdca shiny app tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants