[R] Terminating a program using R

S Ellison S.Ellison at LGCGroup.com
Thu Feb 12 12:35:19 CET 2015


> I would like to query that if it is possible for R to terminate a program such as
> notepad, word, etc.?

pskill from the tools package says it supports the Windows system call
'TerminateProcess'.

You can get a process's ID using the windows command line 'tasklist'.

I got this to work on my text editor process (TextPad) using

pname <- "TextPad.exe"      #Amend to your process name
tl <- system('tasklist', intern=TRUE)       #Get the task list 
pid.line <- tl[grep(pname, tl)]                #Find the row that matches [WARNING - I assumed only one]
pid.char <- gsub(paste("^",pname," *([0-9]+).*", sep=""), "\\1", pid.line) 
                                                               #Extract the PID 
pid <- as.numeric(pid.char)                   #Convert to integer
pskill(pid)                                               #Kill the process


You'll have to tweak that if you have more than one process with the same name, and also to check for nonexistence of an expected process.


S Ellison



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list