![]() file.path("folder1", "folder2") # "folder1/folder2" Pass ‘folder1’ and ‘folder2’ as arguments to file.path() to make a platform-independent pathname. R then handles how these names are put together, accounting for the differences between Mac, Windows, Linux, etc. It means that in this function (file.path) you give R the names of the directories in the order they are found (i.e., the directory hierarchy). You can use file.path() to construct file and directory paths that are independent of the operating system your R code is running on. Provide the relative path to the file “mytest3.R” by using file.path(). But what if you were working with dozens, or millions, of individual files? In that case, being able to programatically act on many files would be absolutely necessary. You now have two files in the current directory. The name of the file you want to copy(“from =”), and the name of the new copied file (“to =”). If you check the help page or arguments, you will see that this function requires two pieces of information. Make a copy of “mytest2.R” called “mytest3.R” using py(). You might now try to delete mytest.R using file.remove(‘mytest.R’), but that won’t work since mytest.R no longer exists. Your operating system will provide simpler tools for these sorts of tasks, but having the ability to manipulate files programatically is useful. ![]() ![]() file.rename("mytest.R", "mytest2.R") # TRUE We will use “$” a lot more as we progress through the semester.Ĭhange the name of the file “mytest.R” to “mytest2.R” by using file.rename(). You can use the $ operator - e.g., (“mytest.R”)$mode - to grab specific items. These might be important in various programming contexts. This function provides information on the size, various time stamps, and users of the file. ("mytest.R") # size isdir mode mtime ctime But, if you are running a program that loops through a series of files and does some processing on each one, you will want to check to see that each exists before you try to process it.Īccess information about the file “mytest.R” by using (). These sorts of functions are excessive for interactive use. We can also specifically check to see if “mytest.R” exists in the working directory using the file.exists() function. Let’s check this by listing all the files in the current directory. OK, so “mytest.R” should be the only file in this newly created directory. These options will become more useful when you start writing and saving code. Similar to the previous lesson, let’s assign 9 to x using x Global Options, then the Appearance tab. If you are continuing from the previous lesson (Basic_Building_Blocks) without closing R or RStudio, you will likely notice that the variables you created are still in R’s memory, or local workspace. # "initpath" "install_course" "keep_rmd" "les" ls() # "course_dir" "dest_dir" "destrmd" "initcode" “Local workspace” refers to what is loaded or created in R’s memory that can be accessed in this session. ![]() Now, you can list all the objects in your local workspace using ls(). We will learn how to move about inside the computer and change the working directory later in this lesson. If you open RStudio by using the GUI to open a file, that will be the working directory. Let’s jump right in so you can get a feel for how these special functions work!ĭetermine which directory your R session is using as its current working directory using getwd(). However it’s important to note that R provides a common API (a common set of commands) for interacting with files, that way your code will work across different kinds of computers. In this lesson, you’ll learn how to examine your local workspace in R and begin to explore the relationship between your workspace and the file system of your machine.īecause different operating systems have different conventions with regards to things like file paths, the outputs of these commands may vary across machines.
0 Comments
Leave a Reply. |