R purrr system2 map

94 Views Asked by At

I have a vector of filenames A and I have an equivalent vector of new names for those files -- B.

I was hoping to do:

test <-map2(A, B, ~system2('mv', args=c(.x, .y)))

or perhaps

test <-map2(A, B, ~system2('mv', args=paste(.x, .y)))

but .x and .y don't get interpreted nicely and the command fails.

mv: cannot stat '/home/rob/KRBD_Data/Client_ID/000/raw/monthzips/2015-01/Data_2015-01-07.zip'

If I use rename as the command I get

test <-map2(A, B, ~system2('rename', args=paste(.x, .y)))

Unknown regexp modifier "/r" at (user-supplied code), at end of line

Unknown regexp modifier "/b" at (user-supplied code), at end of line

1

There are 1 best solutions below

1
David Robinson On

If you paste together the arguments, the system2 function will escape that space that separates them, as though it's one long filename that contains a space.

Instead, pass both the arguments as a vector:

test <-map2(A, B, ~system2('mv', args=c(.x, .y)))