2

I am using R on a Unix system. I wrote an R script in windows, and uploaded it to Unix (via putty), such that I could run the script in R (in unix) with the command source('CODE.R').

Now, here is the problem. In my R script, I wrote write.table(myDataOutput, file = "/myfolder/wantedData.txt") as my output, but after running the script, the text file is nowhere to be found (not in any folder).

I did some research on google, but it seemed that my question is kind of too specific, and I did not manage to find a similar problem.

Please help me. Thank you!

ps: now the slash is updated. Sorry for the confusion.

NeverBe
  • 107
  • 1
  • 1
  • 7

2 Answers2

0

I have had this problem myself, not being able to figure out the default directory which R uses for a file with a relative path. Since you used an absolute path, the file should be found at \myfolder\wantedData.txt. If it isn't there, you can try searching for it:

sudo find . -name 'wantedData.txt`

If this search comes up empty, then R did not write the file. If you find it, but in a location which you did not expect, then most likely R interpreted your path as a relative, rather than absolute, path. In this case, remember the location so you can find your file next time.

Update

Based on the warning message you got from Unix, I would guess that your R script does not have permissions to write the output file in the location you specified. You could try running the R script with sudo, which might get around this problem.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 2
    I don't think the file can be written to a non-existent directory. If this is a relative path (and I think it is) I'm not sure `myfolder` exists. (And of course, a backslash doesn't even work on windows without escaping it and it certainly doesn't work on a *nix system.) – Roland Aug 24 '16 at 09:06
  • @Roland He mentioned that the script was run on Unix. `\myfolder` does _not_ mean something relative in the UNIX file system, it means a folder called `myfolder` located directly under the root. Hence, it is a mystery why R would interpret a directory path in this way. – Tim Biegeleisen Aug 24 '16 at 09:08
  • 2
    @Downvoter: Please reveal yourself. I won't downvote you, I promise. – Tim Biegeleisen Aug 24 '16 at 09:43
  • I actually get a warning from the system. How so? – NeverBe Aug 24 '16 at 10:12
  • What is the "warning?" If you can't find the file anywhere, it probably never got written. – Tim Biegeleisen Aug 24 '16 at 10:16
  • I did not recall the whole message, but it did mention "the incident will be reported". Anyway, I have run the script (with simple dataset) in windows, and it is fine with the output files produced properly... – NeverBe Aug 24 '16 at 10:40
  • Hi, I realize the plausible problem of the warning. I am running unix on the server, and sudo is usually for administrator, so that is where the warning comes in. Regarding the permission, I did a test with a very simple data set (the built-in dataset, "cars"), and `write.table(cars, file="junk.txt")`, and it worked... – NeverBe Aug 24 '16 at 18:30
0

Welcome on SO. This SuperUser question explains why Windows uses backslashes and Unix forward slashes in file paths. This SNAP TECH blog post introduces a choose.OS function that is useful if you work with different OS. This SO question explains how to efficiently convert backslash to forward slash.

Community
  • 1
  • 1
000andy8484
  • 563
  • 3
  • 16