how to delete a file with R? [duplicate]
Asked Answered
B

2

82

Possible Duplicate:
Automatically Delete Files/Folders in R

I would like to know if there is a way in R to check up if a file is in my current directory, and if it is there then the program deletes it?

I know that other languages have direct access to OS functions to do this task, but I am a little bit dubious if R has that capability.

Buehler answered 8/1, 2013 at 16:44 Comment(1)
Type ?system at the console prompt.Brutal
W
187

How about:

#Define the file name that will be deleted
fn <- "foo.txt"
#Check its existence
if (file.exists(fn)) {
  #Delete file if it exists
  file.remove(fn)
}

As far as I know, this is a permanent, non-recoverable (i.e. not "move to recycle bin") on all platforms ...

Warmblooded answered 8/1, 2013 at 16:54 Comment(5)
Any alternative that moves files to recycle bin/trash?Corrida
@duncanrager, I don't know of one. You could ask a new question (after checking for other answers on StackOverflow, of course ...)Warmblooded
Stupid question: how are you able to execute the if() condition without the requisite {}?Blues
curly brackets are not required if the code to be executed consists of a single statement (although arguably it would be best practice to include them anyway)Warmblooded
@derp92 Just to confirm. file.remove(fullfilepath) permanently deletes the file.Harrus
B
24

One of the reasons R cannot be safely exposed to outside users is that it offers complete access to system facilities. In addition to the list.files, list.dirs and the file.remove functions, the system function allows access to pretty much any exploit imaginable.

Brutal answered 8/1, 2013 at 16:50 Comment(2)
I think the downvotes (not necessarily from @DWin) are because it shouldn't have been too hard to find these answers by googling/searching Stack Overflow. I don't actually think the link pointed to (which you would have found by searching SO for [r] delete file) is an exact duplicate, but it probably would have answered your question.Warmblooded
@BenBolker. A question gets closed as a duplicate i it cover the same material. The functions in your answer all appear immediately if one types ?files at the command prompt. No googling needed. I suspect the downvotes are due both to the lack of any statement that a search of any sort was done and to the implied slur at R being limited in some way.Brutal

© 2022 - 2024 — McMap. All rights reserved.