Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sys.glob expansion

Tags:

r

filepath

I am trying to use Sys.glob to open a file called "apcp_sfc_latlon_subset_19940101_20071231.nc". The following command works:

> Sys.glob(file.path("data/train", "apcp*"))
[1] "data/train/apcp_sfc_latlon_subset_19940101_20071231.nc"

But this command doesn't return anything. I'm don't know why it doesn't work.

> Sys.glob(file.path("data/train", "apcp", "*"))
character(0)

I want the "apcp" bit as it's own argument because I will be passing a variable instead of a hard coded string.

Thank you.

like image 258
mchangun Avatar asked Jul 30 '13 05:07

mchangun


1 Answers

file.path("data/train", "apcp", "*") returns "data/train/apcp/*" whereas file.path("data/train", "apcp*") returns "data/train/apcp*".

Thus in the first case you are looking for files in the subdirectoy apcp, and in the (working) case you are looking for files which begin apcp within the data\train directory.

If you want to be able to pass the apcp component as a argument, using paste0 will work

starting <- "apcp"

file.path("data/train", paste0(starting, '*', collapse =''))

# "data/train/apcp*"
like image 197
mnel Avatar answered Sep 19 '22 06:09

mnel