How to create a new output file in R if a file with that name already exists?

Usman Khan

I am trying to run an R-script file using windows task scheduler that runs it every two hours. What I am trying to do is gather some tweets through Twitter API and run a sentiment analysis that produces two graphs and saves it in a directory. The problem is, when the script is run again it replaces the already existing files with that name in the directory.

As an example, when I used the pdf("file") function, it ran fine for the first time as no file with that name already existED in the directory. Problem is I want the R-script to be running every other hour. So, I need some solution that creates a new file in the directory instead of replacing that file. Just like what happens when a file is downloaded multiple times from Google Chrome.

Spacedman

I'd just time-stamp the file name.

> filename = paste("output-",now(),sep="")
> filename
[1] "output-2014-08-21 16:02:45"

Use any of the standard date formatting functions to customise to taste - maybe you don't want spaces and colons in your file names:

> filename = paste("output-",format(Sys.time(), "%a-%b-%d-%H-%M-%S-%Y"),sep="")
> filename
[1] "output-Thu-Aug-21-16-03-30-2014"

If you want the behaviour of adding a number to the file name, then something like this:

serialNext = function(prefix){
    if(!file.exists(prefix)){return(prefix)}
    i=1
    repeat {
       f = paste(prefix,i,sep=".")
       if(!file.exists(f)){return(f)}
       i=i+1
     }
  }

Usage. First, "foo" doesn't exist, so it returns "foo":

> serialNext("foo")
[1] "foo"

Write a file called "foo":

> cat("fnord",file="foo")

Now it returns "foo.1":

> serialNext("foo")
[1] "foo.1"

Create that, then it returns "foo.2" and so on...

> cat("fnord",file="foo.1")
> serialNext("foo")
[1] "foo.2"

This kind of thing can break if more than one process might be writing a new file though - if both processes check at the same time there's a window of opportunity where both processes don't see "foo.2" and think they can both create it. The same thing will happen with timestamps if you have two processes trying to write new files at the same time.

Both these issues can be resolved by generating a random UUID and pasting that on the filename, otherwise you need something that's atomic at the operating system level.

But for a twice-hourly job I reckon a timestamp down to minutes is probably enough.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a new output file in R if a file with that name already exists?

From Dev

How to check if file name already exists?

From Dev

Python copy files to a new directory and rename if file name already exists

From Dev

Command line: create file with new filename if the filename already exists

From Dev

Command line: create file with new filename if the filename already exists

From Dev

Create a new file, but add a number if the filename already exists

From Dev

File(String name) - Creates also a new file if already a "name"-named file exists?

From Dev

How to create an empty folder when a file with the folder's name already exists?

From Dev

How to create an empty folder when a file with the folder's name already exists?

From Dev

Renaming a file to name of file that already exists

From Dev

MapReduce on Hadoop says 'Output file already exists'

From Dev

Save with a different name if the file already exists in directory

From Dev

Move and replace if same file name already exists?

From Dev

The file name you submitted already exists on the server

From Dev

In Powershell, how do I copy/paste a file, if file already exists, to roll the new copy of the file?

From Dev

Java - renaming output file if name already exists with an increment, taking into account existing increments

From Dev

How to create a batch file that output file name in txt?

From Dev

Warning message if file name already exists when creating a pdf file

From Dev

Cannot create a file when it already exists using File.Move

From Dev

mklink error: Cannot create a file when that file already exists

From Dev

How to copy-paste a file that already exists?

From Dev

How to check if the file already exists in the LinkItemCollection?

From Dev

How to check if file already exists, if not, download on Python?

From Dev

How to copy-paste a file that already exists?

From Dev

how to check if value already exists in txt file

From Dev

If File Already Exists Rename it

From Dev

Error in file already exists

From Dev

How to check if a file with date name exists from inside a batch file and create file

From Dev

R download.file() rename the downloaded file, if the filename already exists

Related Related

  1. 1

    How to create a new output file in R if a file with that name already exists?

  2. 2

    How to check if file name already exists?

  3. 3

    Python copy files to a new directory and rename if file name already exists

  4. 4

    Command line: create file with new filename if the filename already exists

  5. 5

    Command line: create file with new filename if the filename already exists

  6. 6

    Create a new file, but add a number if the filename already exists

  7. 7

    File(String name) - Creates also a new file if already a "name"-named file exists?

  8. 8

    How to create an empty folder when a file with the folder's name already exists?

  9. 9

    How to create an empty folder when a file with the folder's name already exists?

  10. 10

    Renaming a file to name of file that already exists

  11. 11

    MapReduce on Hadoop says 'Output file already exists'

  12. 12

    Save with a different name if the file already exists in directory

  13. 13

    Move and replace if same file name already exists?

  14. 14

    The file name you submitted already exists on the server

  15. 15

    In Powershell, how do I copy/paste a file, if file already exists, to roll the new copy of the file?

  16. 16

    Java - renaming output file if name already exists with an increment, taking into account existing increments

  17. 17

    How to create a batch file that output file name in txt?

  18. 18

    Warning message if file name already exists when creating a pdf file

  19. 19

    Cannot create a file when it already exists using File.Move

  20. 20

    mklink error: Cannot create a file when that file already exists

  21. 21

    How to copy-paste a file that already exists?

  22. 22

    How to check if the file already exists in the LinkItemCollection?

  23. 23

    How to check if file already exists, if not, download on Python?

  24. 24

    How to copy-paste a file that already exists?

  25. 25

    how to check if value already exists in txt file

  26. 26

    If File Already Exists Rename it

  27. 27

    Error in file already exists

  28. 28

    How to check if a file with date name exists from inside a batch file and create file

  29. 29

    R download.file() rename the downloaded file, if the filename already exists

HotTag

Archive