Close external application launched from R

2020-04-23 08:35发布

问题:

Is it possible to close an application that was launched from within R?

Assume that I have opened a CSV file my_file.csv with its associated application via the shell.exec function. I then want to close this application.

回答1:

Since R has no control over other programs you cannot directly close files opended without R reliably. You do not even know which program to close. E.g. one one computer a csv file may be opened with notepad, on another computer it may be opened with Excel.

If you know the program you can use system2() or similar commands to execute a command to kill the other program. E.g. if you want to close Excel execute system2("taskkill", args = "/im excel.exe"). Note that this will close all open instances of the program/Excel, not jut a specific one.