Now that we have created a project, let us take a look at the R Console window. Click on the window marked Console
. All console commands are issued by typing your command following the command prompt >
, and then pressing Enter. I will just illustrate three commands that will help you answer the questions "Which project am I on?", and "What files do I have in my folders?"
getwd()
: Thegetwd()
command is very important since it will always tell you which directory you are in. Since we just created a new project, we expect that we will be pointing to the directory we just created, right? To double check, switch over to the console, issue thegetwd()
command, and then press Enter. That should echo back the current working directory:
dir()
: Thedir()
command will give you a list of all files in the current working directory. In our case, it is simply the names of the three directories you have just created. However, typically may see many files, usually corresponding to the type of directory you are in (.R
for source files,.dat
,.csv
for data files, and so on):
setwd()
: Sometimes you will need to switch directories within the same project or even to another project. The command you will use issetwd()
. You will supply the directory that you want to switch to, all contained within the parentheses. Here is an example which will switch to the sub-directory which will house the R code. This particular example supplies the entire path as the directory destination. Since you are already in thePracticalPredictiveAnalytics
directory, you can also usesetwd("R")
which accomplishes the same thing:
> setwd("C:/PracticalPredictiveAnalytics/R")
To verify that it has changed, issue the getwd()
command again:
> getwd() [1] "C:/PracticalPredictiveAnalytics/R"
I suggest using getwd()
and setwd()
liberally, especially if you are working on multiple projects, and want to avoid reading or writing the wrong files.