mchangun
mchangun

Reputation: 10342

Sys.glob expansion

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.

Upvotes: 5

Views: 4867

Answers (1)

mnel
mnel

Reputation: 115435

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*"

Upvotes: 6

Related Questions