[R] Help needed! Error in setwd(newdir) : cannot change working directory

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Feb 26 22:23:01 CET 2012


Stop it with the "fake" code if you want help. In keeping with the posting guide, make one reproducible example that exhibits a real problem. For what it is worth, I don't have difficulty with setwd().
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

ying chen <ying_chen at live.com> wrote:

>
>
>
>
>
>Hi,
>I am sorry about the format it showed up. I do not know what happened
>as it looks fine when I read it using chrome on ubuntu 11.10 64bit. The
>packages A, B, C are not the real package names.  Basically I have 
>scriptA.R that works from inside a directory with my data
>(/home/sean/Rtest/Data01). Now I want to modify the script to make it
>run from the upper directory (/home/sean/Rtest) because I have many
>data directories under /Rtest, such as /home/sean/Rtest/Data01,
>/home/sean/Rtest/Data02, /home/sean/Rtest/Data03,..... I want to modify
>my script so that it will run from /Rtest, and with the data directory
>name as arg passed to my R script. My point is that when I put the
>scriptA.R in data directory such as Data01, it works. Here are my
>scriptB.R script:
>######################scriptA.R#########################################
># load the necessary librarieslibrary("A");  # A fake package
>namelibrary("B");  # B fake package namelibrary("C");  # C fake package
>name........    #data processing........    #data processing........   
>#data processing# unload the librariesdetach("package:A"); 
>detach("package:B");detach("package:C");q();#######################################################################
>But, when I tried to modify scriptA.R to scriptB.R to run it from the
>directory just above Data directories, I got the error message.
>###########################scriptB.R#######################################
># retrieve argsargs <- commandArgs(TRUE);# store the current
>directoryinitial.dir <- getwd();newdir <-
>paste(initial.dir,args,sep="/");outfilename <-
>paste(args,"out",sep=".");# change to the new directorysetwd(newdir);#
>load the necessary librarieslibrary("A");  # A fake package
>namelibrary("B");  # B fake package namelibrary("C");  # C fake package
>name# set the output filesink(outfilename);........    #data
>processing........    #data processing........    #data processing#
>close the output filesink();# unload the librariesdetach("package:A"); 
>#detach("package:B");detach("package:C");# change back to the original
>directorysetwd(initial.dir);
>##################################################################
>I run the scriptB.R by
>sean at I7-3930K:~/Rtest$ R --slave --args Data01 < scriptB.R
>I got the error message:
>sean at I7-3930K:~/Rtest$ R --slave --args Data01 < scriptB.RAttempting to
>load the environment �package:R.utils�Loading required package:
>R.methodsS3R.methodsS3 v1.2.1 (2010-09-18) successfully loaded. See
>?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3
>(2011-11-01) successfully loaded. See ?R.oo for help.
>Attaching package: �R.oo�
>The following object(s) are masked from �package:R.methodsS3�:
>   throw.default
>The following object(s) are masked from �package:methods�:
>   getClass, getClasses, getMethods
>The following object(s) are masked from �package:base�:
>  attach, detach, environment, gc, load, save
>R.utils v1.9.11 (2012-01-17) successfully loaded. See ?R.utils for
>help.
>Attaching package: �R.utils�
>The following object(s) are masked from �package:utils�:
>    timestamp
>The following object(s) are masked from �package:base�:
>cat, commandArgs, getOption, inherits, isOpen, lapply, parse,   
>remove, warnings
>[1] "/home/sean/Rtest"[1] "/home/sean/Rtest/NA"      
>"/home/sean/Rtest/TRUE"    [3] "/home/sean/Rtest/Data01"[1] "NA.out"   
>"TRUE.out"     "Data01.out"Error in setwd(newdir) : cannot change
>working directoryExecution halted
>What did I do wrong? I think I have the right to read and write to the
>directories.
>One more question, is there anyway to modify this script to make it
>work on all data directories under /home/sean/Rtest by itself?
>Thanks a lot for the help!
>Ying
>sean at I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean 4096
>2012-02-25 20:39 Data01drwx------ 2 sean sean 4096 2012-02-25 20:28
>Data02drwx------ 2 sean sean 4096 2012-02-26 07:41 Data03drwx------ 2
>sean sean 4096 2012-02-25 21:18 Data04drwx------ 2 sean sean 4096
>2012-02-25 20:27 Data05-rw-r--r-- 1 sean sean  437 2012-02-26 15:28
>scriptA.R.......
>> From: michael.weylandt at gmail.com
>> Date: Sun, 26 Feb 2012 12:48:57 -0500
>> Subject: Re: [R] Help needed! Error in setwd(newdir) : cannot change
>working directory
>> To: ying_chen at live.com
>> CC: r-help at r-project.org
>> 
>> This is a mess -- please resend in plain text.
>> 
>> Also, there are not, to my knowledge, packages (not libraries) called
>> "A", "B", or "C" so your script doesn't even begin to look
>> reproducible were it legible.
>> 
>> Do you have read/write access to the directories in question?
>> 
>> Michael
>> 
>> On Sat, Feb 25, 2012 at 11:25 PM, ying chen <ying_chen at live.com>
>wrote:
>> >
>> > Hi Guys,
>> >
>> > I am new to R and just trying to write a small script to automate a
>couple commands. But I run into the setwd(): cannot change working
>directory.
>> > I googled a little bit and tried all fixes/suggestions with no
>success.
>> > Basically I have a script that works from inside a directory with
>my data (/home/sean/Rtest/Data01). Now I want to modify the script to
>make it run from the upper directory (/home/sean/Rtest) because I have
>many data directories under /Rtest, such as /home/sean/Rtest/Data01,
>/home/sean/Rtest/Data02, /home/sean/Rtest/Data03,.....
>> > I want to modify my script so that it will run from /Rtest, and
>with the data directory name as arg passed to my R script. Here is my
>mytest02.R script:
>> >>># retrieve args>>args <- commandArgs(TRUE);>># store the current
>directory>>initial.dir <- getwd();>>newdir <-
>paste(initial.dir,args,sep="/");>>outfilename <-
>paste(args,"out",sep=".");>># change to the new
>directory>>setwd(newdir);>># load the necessary
>libraries>>library("A");  #>>library("B");>>library("C");>># set the
>output file>>sink(outfilename);>>........    #data processing>>........
>#data processing>>........    #data processing>># close the output
>file>>sink();>># unload the libraries>>detach("package:A"); 
>#>>detach("package:B");>>detach("package:C");>># change back to the
>original directory>>setwd(initial.dir);
>> > Then I run the script by
>> >>>sean at I7-3930K:~/Rtest$ R --slave --args Data01 < mytest02.R
>> > I got the error message:
>> >>>sean at I7-3930K:~/Rtest$ R --slave --args Data01 <
>mytest02.R>>Attempting to load the environment
>�package:R.utils�>>Loading required package: R.methodsS3>>R.methodsS3
>v1.2.1 (2010-09-18) successfully loaded. See ?R.methodsS3 for
>help.>>Loading required package: utils>>R.oo v1.8.3 (2011-11-01)
>successfully loaded. See ?R.oo for help.>>>>Attaching package:
>�R.oo�>>>>The following object(s) are masked from
>�package:R.methodsS3�:>> >>   throw.default>>>>The following object(s)
>are masked from �package:methods�:>> >>   getClass, getClasses,
>getMethods>>>>The following object(s) are masked from �package:base�:>>
>>>   attach, detach, environment, gc, load, save>>>>R.utils v1.9.11
>(2012-01-17) successfully loaded. See ?R.utils for help.>>>>Attaching
>package: �R.utils�>>>>The following object(s) are masked from
>�package:utils�:>>>>    timestamp>>>>The following object(s) are masked
>from �package:base�:>>>>    cat, commandArgs, getOption, inherits,
>isOpen, lapply, parse,>>    remove, warnings>>>>[1]
>"/home/sean/Rtest">>[1] "/home/sean/Rtest/NA"      
>"/home/sean/Rtest/TRUE"    >>[3] "/home/sean/Rtest/Data01">>[1]
>"NA.out"       "TRUE.out"     "Data01.out">>Error in setwd(newdir) :
>cannot change working directory>>Execution halted
>> > What did I do wrong?
>> > One more question, is there anyway to modify this script to make it
>work on all data directories under /home/sean/Rtest by itself?
>> > Thanks a lot for the help!
>> > Ying
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>
> 		 	   		  
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list