RETURN-project / makeDataCube

Data management
Apache License 2.0
0 stars 0 forks source link

Consider using C-style string formatting #28

Closed PabRod closed 3 years ago

PabRod commented 3 years ago

Problem

This project often passes strings to the function system. These strings represent shell commands. The commands are built by pasting together it's parts, for instance:

system(
   paste0("tar -xvzf ", wvpCompressed, " -C ", wvpfolder), # Command
   intern = TRUE, ignore.stderr = TRUE # Other parameters
)

These commands can (arguably) be a bit difficult to read.

Improvement

A neater way of building string commands is to use sprintf instead of paste0.

system(
   sprintf("tar -xvzf %s -C %s", wvpCompressed, wvpfolder), # Command
   intern = TRUE, ignore.stderr = TRUE # Other parameters
)

References

PabRod commented 3 years ago

Enhancement

@wandadk, in future edits, please consider replacing:

system(paste0('command1', object1, 'command2', object2, ..., intern = TRUE, ignore.stderr = TRUE))

by the more readable:

systemf('command1 %s command2 %s', object1, object2)

The idea is that each %s is a placeholder for each of the strings that are passed immediately afterwards.

Examples

# With paste0
system(paste0("tar -xvzf ", wvpCompressed, " -C ", wvpfolder), intern = TRUE, ignore.stderr = TRUE)

# With systemf (basic usage)
systemf("tar -xvzf %s -C %s", wvpCompressed, wvpfolder)

# It (optionally) accepts variable names
systemf("tar -xvzf %s -C %s", orig = wvpCompressed, dest = wvpfolder)

# intern and ignore.stderr are TRUE by default, but can be overridden
systemf("tar -xvzf %s -C %s", wvpCompressed, wvpfolder, intern = FALSE, ignore.stderr = FALSE)

Sorry for not having noticed this potential enhancement before. I will refactor the paste0's we have already written in the next days.

PabRod commented 3 years ago

Implemented in https://github.com/RETURN-project/makeDataCube/commit/178cb6e1fafbda2eb7e0d8fc24642eff01ec399e and tagged as v0.2.

@wandadk: this version includes some major changes that I could test only partially. In particular, I managed to correctly reproduce the full workflow from scratch and as a re-run.

In case something fails, I've tagged the non C-style version of this code as v0.1.