新人求助setwd的cannot changer working directoryy处理

新人求助setwd的cannot change working directory处理-百谷歌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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.&&Loading required package: utils&&R.oo v1.8.3 () 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 () 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 haltedWhat 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
Search Discussions
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 lookreproducible were it legible.Do you have read/write access to the directories in question?MichaelOn Sat, Feb 25, 2012 at 11:25 PM, ying chen 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 bysean at I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI 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 () successfully loaded. See ?R.methodsS3 for help.&&Loading required package: utils&&R.oo v1.8.3 () 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 () 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 haltedWhat 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 listPLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
try setwd(choose.dir())--View this message in context: Sent from the R help mailing list archive .
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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3 () successfully loaded. See ?R.oo for help.Attaching package: �R.oo�The following object(s) are masked from �package:R.methodsS3�:throw.defaultThe following object(s) are masked from �package:methods�:getClass, getClasses, getMethodsThe following object(s) are masked from �package:base�:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: �R.utils�The following object(s) are masked from �package:utils�:timestampThe 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 haltedWhat 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!Yingsean@I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean -25 20:39 Data01drwx------ 2 sean sean -25 20:28 Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2 sean sean -25 21:18 Data04drwx------ 2 sean sean -25 20:27 Data05-rw-r--r-- 1 sean sean
15:28 scriptA.R.......From: michael.Date: Sun, 26 Feb :57 -0500Subject: Re: [R] Help needed! Error in setwd(newdir) : cannot change working directoryTo: ying_CC: r-help@r-project.orgThis 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 lookreproducible were it legible.Do you have read/write access to the directories in question?MichaelOn Sat, Feb 25, 2012 at 11:25 PM, ying chen 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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.&&Loading required package: utils&&R.oo v1.8.3 () 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 () 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 haltedWhat 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@r-project.org mailing listPLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
Hi,I am sorry about the format it showed up. I do not know what happened as now it looks worse even on my own machine. I do not know what to do, I just add &\n& to the end of each line and hope it will come out OK this time. 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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3 () successfully loaded. See ?R.oo for help.Attaching package: �R.oo�The following object(s) are masked from �package:R.methodsS3�:throw.defaultThe following object(s) are masked from �package:methods�:getClass, getClasses, getMethodsThe following object(s) are masked from �package:base�:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: �R.utils�The following object(s) are masked from �package:utils�:timestampThe 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 haltedWhat 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!Yingsean@I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean -25 20:39 Data01drwx------ 2 sean sean -25 20:28 Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2 sean sean -25 21:18 Data04drwx------ 2 sean sean -25 20:27 Data05-rw-r--r-- 1 sean sean
15:28 scriptA.R.......From: ying_To: michael.Date: Sun, 26 Feb :20 -0500CC: r-help@r-project.orgSubject: Re: [R] Help needed! Error in setwd(newdir) : cannot change working directoryHi,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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3 () successfully loaded. See ?R.oo for help.Attaching package: �R.oo�The following object(s) are masked from �package:R.methodsS3�:throw.defaultThe following object(s) are masked from �package:methods�:getClass, getClasses, getMethodsThe following object(s) are masked from �package:base�:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: �R.utils�The following object(s) are masked from �package:utils�:timestampThe 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 haltedWhat 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!Yingsean@I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean -25 20:39 Data01drwx------ 2 sean sean -25 20:28 Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2 sean sean -25 21:18 Data04drwx------ 2 sean sean -25 20:27 Data05-rw-r--r-- 1 sean sean
15:28 scriptA.R.......From: michael.Date: Sun, 26 Feb :57 -0500Subject: Re: [R] Help needed! Error in setwd(newdir) : cannot change working directoryTo: ying_CC: r-help@r-project.orgThis 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 lookreproducible were it legible.Do you have read/write access to the directories in question?MichaelOn Sat, Feb 25, 2012 at 11:25 PM, ying chen 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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 for help.&&Loading required package: utils&&R.oo v1.8.3 () 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 () 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 haltedWhat 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@r-project.org mailing listPLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]______________________________________________r-help@r-project.org mailing listPLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
At this point you need to learn how to do some basic debugging of yourscript.
Open a GUI window that you can copy/paste parts of your script andexecute it piece-meal.
Get to the point where you want to change yourworking directory and do a 'getwd()' to see where you are.
It is obviousfrom your error message that whatever directory you are giving as anargument is not valid at the point you are executing the 'setwd' function.At least put a 'print(getwd())' right before your setwd function call.This is debugging 101 that you will have to learn so that you can developmore complex scripts.On Sun, Feb 26, 2012 at 4:12 PM, ying chen wrote:Hi,I am sorry about the format it showed up. I do not know what happened asnow it looks worse even on my own machine. I do not know what to do, I justadd &\n& to the end of each line and hope it will come out OK this time.The packages A, B, C are not the real package names.
Basically I havescriptA.R that works from inside a directory with my data(/home/sean/Rtest/Data01). Now I want to modify the script to make it runfrom the upper directory (/home/sean/Rtest) because I have many datadirectories under /Rtest, such as /home/sean/Rtest/Data01,/home/sean/Rtest/Data02, /home/sean/Rtest/Data03,..... I want to modify myscript so that it will run from /Rtest, and with the data directory name asarg passed to my R script. My point is that when I put the scriptA.R indata 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 thedirectory 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 bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean@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 () successfully loaded. See ?R.methodsS3 forhelp.Loading required package: utilsR.oo v1.8.3 () successfully loaded. See ?R.oo for help.Attaching package: �R.oo�The following object(s) are masked from �package:R.methodsS3�:throw.defaultThe following object(s) are masked from �package:methods�:getClass, getClasses, getMethodsThe following object(s) are masked from �package:base�:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: �R.utils�The following object(s) are masked from �package:utils�:timestampThe 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 haltedWhat did I do wrong? I think I have the right to read and write to thedirectories.One more question, is there anyway to modify this script to make it workon all data directories under /home/sean/Rtest by itself?Thanks a lot for the help!Yingsean@I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean -25 20:39 Data01drwx------ 2 sean sean -25 20:28 Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2 sean sean -25 21:18 Data04drwx------ 2 sean sean -25 20:27 Data05-rw-r--r-- 1 sean sean
15:28 scriptA.R.......From: ying_To: michael.Date: Sun, 26 Feb :20 -0500CC: r-help@r-project.orgSubject: Re: [R] Help needed! Error in setwd(newdir) : cannot changeworking directoryHi,I am sorry about the format it showed up. I do not know what happened asit looks fine when I read it using chrome on ubuntu 11.10 64bit. Thepackages A, B, C are not the real package names.
Basically I havescriptA.R that works from inside a directory with my data(/home/sean/Rtest/Data01). Now I want to modify the script to make it runfrom the upper directory (/home/sean/Rtest) because I have many datadirectories under /Rtest, such as /home/sean/Rtest/Data01,/home/sean/Rtest/Data02, /home/sean/Rtest/Data03,..... I want to modify myscript so that it will run from /Rtest, and with the data directory name asarg passed to my R script. My point is that when I put the scriptA.R indata directory such as Data01, it works. Here are my scriptB.R script:######################scriptA.R########################################## load the necessary librarieslibrary(&A&);
# A fake packagenamelibrary(&B&);
# B fake package namelibrary(&C&);
# C fake packagename........
#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 thedirectory just above Data directories, I got the error message.###########################scriptB.R######################################## retrieve argsargs &- commandArgs(TRUE);# store the currentdirectoryinitial.dir &- getwd();newdir &-paste(initial.dir,args,sep=&/&);outfilename &- paste(args,&out&,sep=&.&);#change to the new directorysetwd(newdir);# load the necessarylibrarieslibrary(&A&);
# A fake package namelibrary(&B&);
# B fakepackage namelibrary(&C&);
# C fake package name# set the outputfilesink(outfilename);........
#data processing........
#dataprocessing........
#data processing# close the output filesink();#unload the librariesdetach(&package:A&);#detach(&package:B&);detach(&package:C&);# change back to the originaldirectorysetwd(initial.dir);##################################################################I run the scriptB.R bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean@I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RAttempting toload the environment �package:R.utils�Loading required package:R.methodsS3R.methodsS3 v1.2.1 () successfully loaded. See?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3() successfully loaded. See ?R.oo for help.Attaching package: �R.oo�The following object(s) are masked from �package:R.methodsS3�:throw.defaultThe following object(s) are masked from �package:methods�:getClass, getClasses, getMethodsThe following object(s) are masked from �package:base�:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: �R.utils�The following object(s) are masked from �package:utils�:timestampThe 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 workingdirectoryExecution haltedWhat did I do wrong? I think I have the right to read and write to thedirectories.One more question, is there anyway to modify this script to make it workon all data directories under /home/sean/Rtest by itself?Thanks a lot for the help!Yingsean@I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean 4096 20:39 Data01drwx------ 2 sean sean -25 20:28Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2 seansean -25 21:18 Data04drwx------ 2 sean sean -2520:27 Data05-rw-r--r-- 1 sean sean
15:28 scriptA.R.......From: michael.Date: Sun, 26 Feb :57 -0500Subject: Re: [R] Help needed! Error in setwd(newdir) : cannot changeworking directoryTo: ying_CC: r-help@r-project.orgThis 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 lookreproducible were it legible.Do you have read/write access to the directories in question?MichaelOn Sat, Feb 25, 2012 at 11:25 PM, ying chen wrote:Hi Guys,I am new to R and just trying to write a small script to automate acouple commands. But I run into the setwd(): cannot change workingdirectory.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 mydata (/home/sean/Rtest/Data01). Now I want to modify the script to make itrun from the upper directory (/home/sean/Rtest) because I have many datadirectories 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 withthe data directory name as arg passed to my R script. Here is my mytest02.Rscript:# retrieve args&&args &- commandArgs(TRUE);&&# store the currentdirectory&&initial.dir &- getwd();&&newdir &-paste(initial.dir,args,sep=&/&);&&outfilename &-paste(args,&out&,sep=&.&);&&# change to the newdirectory&&setwd(newdir);&&# load the necessary libraries&&library(&A&);#&&library(&B&);&&library(&C&);&&# set the outputfile&&sink(outfilename);&&........
#data processing&&........
#dataprocessing&&........
#data processing&&# close the outputfile&&sink();&&# unload the libraries&&detach(&package:A&);#&&detach(&package:B&);&&detach(&package:C&);&&# change back to theoriginal directory&&setwd(initial.dir);Then I run the script bysean@I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI got the error message:sean@I7-3930K:~/Rtest$ R --slave --args Data01 &mytest02.R&&Attempting to load the environment �package:R.utils�&&Loadingrequired package: R.methodsS3&&R.methodsS3 v1.2.1 () successfullyloaded. See ?R.methodsS3 for help.&&Loading required package: utils&&R.oov1.8.3 () successfully loaded. See ?R.oo for help.&&&&Attachingpackage: �R.oo�&&&&The following object(s) are masked from�package:R.methodsS3�:&& &&
throw.default&&&&The following object(s) aremasked 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() successfully loaded. See ?R.utils for help.&&&&Attachingpackage: �R.utils�&&&&The following object(s) are masked from�package:utils�:&&&&
timestamp&&&&The following object(s) are maskedfrom �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 workingdirectory&&Execution haltedWhat did I do wrong?One more question, is there anyway to modify this script to make itwork on all data directories under /home/sean/Rtest by itself?Thanks a lot for the help!Ying[[alternative HTML version deleted]]______________________________________________r-help@r-project.org mailing listPLEASE do read the posting guidehttp://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]______________________________________________r-help@r-project.org mailing listPLEASE do read the posting guidehttp://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]______________________________________________r-help@r-project.org mailing listPLEASE do read the posting guidehttp://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.--Jim HoltmanData Munger GuruWhat is the problem that you are trying to solve?Tell me what you want to do, not how you want to do it.
On Sun, Feb 26, 2012 at 04:12:03PM -0500, ying chen wrote:Hi,I am sorry about the format it showed up. I do not know what happened as now it looks worse even on my own machine. I do not know what to do, I just add &\n& to the end of each line and hope it will come out OK this time. 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);Print the variable &newdir& to see, whether it contains, what you expect.# load the necessary librarieslibrary(&A&);
# A fake package namelibrary(&B&);
# B fake package namelibrary(&C&);
# C fake package nameLoading libraries in this way does not depend on the working directory.# 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 bysean at I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI 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 () successfully loaded. See ?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3 () successfully loaded. See ?R.oo for help.Attaching package: ?R.oo?The following object(s) are masked from ?package:R.methodsS3?:throw.defaultThe following object(s) are masked from ?package:methods?:getClass, getClasses, getMethodsThe following object(s) are masked from ?package:base?:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils for help.Attaching package: ?R.utils?The following object(s) are masked from ?package:utils?:timestampThe 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 haltedTry print(newdir) before setwd(newdir).Petr Savicky.
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
Go Live...DCN:&jdnewmil@dcn.davis.ca.us&
Basics: ##.#.
Live Go...Live:
OO#.. Dead: OO#..
PlayingResearch Engineer (Solar/Batteries
with/Software/Embedded Controllers)
rocks...1k---------------------------------------------------------------------------Sent from my phone. Please excuse my brevity.ying chen wrote:Hi,I am sorry about the format it showed up. I do not know what happenedas it looks fine when I read it using chrome on ubuntu 11.10 64bit. Thepackages A, B, C are not the real package names.
Basically I havescriptA.R that works from inside a directory with my data(/home/sean/Rtest/Data01). Now I want to modify the script to make itrun from the upper directory (/home/sean/Rtest) because I have manydata directories under /Rtest, such as /home/sean/Rtest/Data01,/home/sean/Rtest/Data02, /home/sean/Rtest/Data03,..... I want to modifymy script so that it will run from /Rtest, and with the data directoryname as arg passed to my R script. My point is that when I put thescriptA.R in data directory such as Data01, it works. Here are myscriptB.R script:######################scriptA.R########################################## load the necessary librarieslibrary(&A&);
# A fake packagenamelibrary(&B&);
# B fake package namelibrary(&C&);
# C fake packagename........
#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 thedirectory just above Data directories, I got the error message.###########################scriptB.R######################################## retrieve argsargs &- commandArgs(TRUE);# store the currentdirectoryinitial.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 packagenamelibrary(&B&);
# B fake package namelibrary(&C&);
# C fake packagename# set the output filesink(outfilename);........
#dataprocessing........
#data processing........
#data processing#close the output filesink();# unload the librariesdetach(&package:A&);#detach(&package:B&);detach(&package:C&);# change back to the originaldirectorysetwd(initial.dir);##################################################################I run the scriptB.R bysean at I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RI got the error message:sean at I7-3930K:~/Rtest$ R --slave --args Data01 & scriptB.RAttempting toload the environment ?package:R.utils?Loading required package:R.methodsS3R.methodsS3 v1.2.1 () successfully loaded. See?R.methodsS3 for help.Loading required package: utilsR.oo v1.8.3() successfully loaded. See ?R.oo for help.Attaching package: ?R.oo?The following object(s) are masked from ?package:R.methodsS3?:throw.defaultThe following object(s) are masked from ?package:methods?:getClass, getClasses, getMethodsThe following object(s) are masked from ?package:base?:attach, detach, environment, gc, load, saveR.utils v1.9.11 () successfully loaded. See ?R.utils forhelp.Attaching package: ?R.utils?The following object(s) are masked from ?package:utils?:timestampThe 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 changeworking directoryExecution haltedWhat did I do wrong? I think I have the right to read and write to thedirectories.One more question, is there anyway to modify this script to make itwork on all data directories under /home/sean/Rtest by itself?Thanks a lot for the help!Yingsean at I7-3930K:~/Rtest$ ls -ltotal 56drwx------ 3 sean sean 4096 20:39 Data01drwx------ 2 sean sean -25 20:28Data02drwx------ 2 sean sean -26 07:41 Data03drwx------ 2sean sean -25 21:18 Data04drwx------ 2 sean sean 4096 20:27 Data05-rw-r--r-- 1 sean sean
15:28scriptA.R.......From: michael.weylandt Date: Sun, 26 Feb :57 -0500Subject: Re: [R] Help needed! Error in setwd(newdir) : cannot changeworking directoryTo: ying_chen CC: r-help at r-project.orgThis 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 lookreproducible were it legible.Do you have read/write access to the directories in question?MichaelOn Sat, Feb 25, 2012 at 11:25 PM, ying chen wrote:Hi Guys,I am new to R and just trying to write a small script to automate acouple commands. But I run into the setwd(): cannot change workingdirectory.I googled a little bit and tried all fixes/suggestions with nosuccess.Basically I have a script that works from inside a directory withmy data (/home/sean/Rtest/Data01). Now I want to modify the script tomake it run from the upper directory (/home/sean/Rtest) because I havemany 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, andwith the data directory name as arg passed to my R script. Here is mymytest02.R script:# retrieve args&&args &- commandArgs(TRUE);&&# store the currentdirectory&&initial.dir &- getwd();&&newdir &-paste(initial.dir,args,sep=&/&);&&outfilename &-paste(args,&out&,sep=&.&);&&# change to the newdirectory&&setwd(newdir);&&# load the necessarylibraries&&library(&A&);
#&&library(&B&);&&library(&C&);&&# set theoutput file&&sink(outfilename);&&........
#data processing&&........#data processing&&........
#data processing&&# close the outputfile&&sink();&&# unload the libraries&&detach(&package:A&);#&&detach(&package:B&);&&detach(&package:C&);&&# change back to theoriginal directory&&setwd(initial.dir);Then I run the script bysean at I7-3930K:~/Rtest$ R --slave --args Data01 & mytest02.RI 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.methodsS3v1.2.1 () successfully loaded. See ?R.methodsS3 forhelp.&&Loading required package: utils&&R.oo v1.8.3 ()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() successfully loaded. See ?R.utils for help.&&&&Attachingpackage: ?R.utils?&&&&The following object(s) are masked from?package:utils?:&&&&
timestamp&&&&The following object(s) are maskedfrom ?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 haltedWhat did I do wrong?One more question, is there anyway to modify this script to make itwork 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 listPLEASE do read the posting guideand provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]______________________________________________R-help at r-project.org mailing listPLEASE do read the posting guideand provide commented, minimal, self-contained, reproducible code.
Related Discussions
viewthread |
categories
user style
6 users in discussion
site design / logo & 2017 Grokbase

我要回帖

更多关于 r working directory 的文章

 

随机推荐