3

I want to use R's System function in my code and so I had written simple one-liner to check whether it's properly running or not. So this was my one liner -

system("dir", intern = TRUE)

I am using Windows so it should give me the same output as it would give on Command Prompt (show all the files present in current directory). But instead it resulted in an error:

Error in system("dir", intern = TRUE) : 'dir' not found

1
  • On my machine your code works fine. But I do have trouble with some other DOS commands like del with the same not found error. I think this is supposed to mean that it can't find the executable, but in this case it seems to be a red herring Commented Sep 19, 2023 at 13:16

1 Answer 1

2

From the description of ?system:

command must be an executable (extensions ‘.exe’, ‘.com’) or a batch file (extensions ‘.cmd’ and ‘.bat’): these extensions are tried in turn if none is supplied. This means that redirection, pipes, DOS internal commands, ... cannot be used: see shell if you want to pass a shell command-line.

So, DOS internal commands cannot be used. It actually doesn't return an error for me, but it does not return anything.

However, shell works as expected:

 shell('dir', intern = TRUE)
 #[1] " Volume in drive C is "                                                  
 #[2] " Volume Serial Number is "                                            
 #[3] ""                                                                              
 #[4] " Directory of C:\\Users\\TB\\Documents"                             
 #[5] ""                                                                              
 #[6] "07/06/2019  11:10    <DIR>          ."     
 #.....                                    
Sign up to request clarification or add additional context in comments.

1 Comment

At least in R 4.2.3, system and system2 can execute internal DOS commands - I ran the OP's code without issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.