Search code examples
rsystem

Invoke a system command from R with dynamic variable name


I want to call python script using the system command. I am passing a parameter with it, but the variable is dynamic. I am not sure how to paste the filename dynamically. I would like the Filename to be changed depending on the user input.

system('python testMethod.py Filename', wait = TRUE)

Solution

  • Use paste0 to create the command string:

    filenname <- "Filename"
    system(paste0("python testMethod.py ", filename), wait = TRUE)
    

    Edit: M--'s suggestion does make it cleaner:

    filenname <- "Filename"
    system(paste("python testMethod.py", filename), wait = TRUE)