loading downloaded data to Rstudio - r

mission on online course:
Download this RData file to your working directory. Then load the data into R with the following command:
load("skew.RData")
so I have downloaded it to my computer but where is my working directory? or how do I load the downloaded file to Rstudio

You can see your working directory by executing getwd().
In order to load the data you need to change it to the folder path where the data is stored.
setwd("C:/your/path")
load("skew.RData")

You can set you working directory in RStudio on the right side manually and then save the files there and open them like :
load("skew.RData")
If your file is saved somewhere else, you should define the path to it:
load("path/to/your/file/skew.RData")

Related

How to use relative paths to load .RData files from working directory?

I am trying to use the load() function to load an .RData file into my current R session. I open my code base by double clicking on the R project file in my directory and then attempt to load the .RData file by running load('./Data/mydata.rdata'). However, this returns a 'No such file or directory' error. I verified that the working directory is correct by using getwd().
I figure I must be using incorrect syntax because I have no issues loading the file when I type in the full file path.
Working directory for R Project file: "/Users/Me/Library/OneDrive/RStuff"
Directory containing .RData file: "/Users/Me/Library/OneDrive/RStuff/Data"
Code that works: load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")
Code that fails: load('./Data/mydata.rdata')
Do relative paths not work with load() or is my syntax wrong?
Do relative paths not work with load() or is my syntax wrong?
It looks like there is an error with your paths. You said the working directory is "/Users/Me/Library/OneDrive/RStuff"
But according to the code that works:
load("/Users/Me/Library/OneDrive/RStuff/Data/mydata.rdata")
...the data file is in "/Users/Me/Library/OneDrive/RStuff/Data"
Therefore to use relative paths, you would use:
load("./Data/mydata.rdata")

R project WD changes "on its own"

I'm working on a project involving several people. We are using GitHub and a Rproject to be able to work on this project on different computers.
But I'm facing a really weird issue when I try to load files and/or get my working directory:
When I type getwd() in R console, I indeed get the path where the project is saved on my computer
> getwd()
[1] "/media/Data/Documents/my_R_project"
When I save getwd() in an object, the WD is not the same and now moves to the path were the script are saved:
folderpath <- getwd()
> folderpath
[1] "/media/Data/Documents/my_R_project/R/Script"
I get the same issue when I try to load a file which is located in /media/Data/Documents/my_R_project/R/Data: when I use read_csv (or any other function like it) and write the file path as
read_csv("R/Data/file.csv") I get and error stating me that there is no such file in the directory /media/Data/Documents/my_R_project/R/Script/R/Data/.
How could I resolve this and make the WD used in read_csv() be the right one ("/media/Data/Documents/my_R_project") so that I don't have to specify the full path every time and that people on another computer can run my script?
I'm working on Ubuntu LTS 20.04
In the end, the error was due to a conflict between the WD of the project and the WD used when I knitted the .Rmd file. The problem was solved by changing the knit options and set its directory as the project directory.

What R command to use to force download files from iCloud

On a mac using iCloud file optimization, large files that are seldom used are uploaded to iCloud and only a small pointer file is left. When I look for the file in Finder, I see the file name and to the left is an icon that indicates that the file is in the cloud. To access the file, I click on the icon and the file is downloaded. With the file.exist command, R returns FALSE for the existence of the file. But after some research I found that the file link is stored in a directory below ~/Library/Mobile\ Documents/com~apple~CloudDocsand the file name is changed to xxx.icloud where xxx is the original file name.
Here's an example of the path to a a directory that holds a .icloud file from a shell in my mac
/Users/gcn/Library/Mobile Documents/com~apple~CloudDocs/Documents/workspace/nutmod/data-raw/NutrientData
I can query for the existence of the file with exists(xxx.icloud). But how do I tell my mac to download the iCloud file and then read it in? Using something like read.table or read.csv doesn't work because the pointer file is not csv.
You can read a csv file directly from a iCloud folder on the Mac by using the path to that folder. Get the path by finding it in the finder. Then right click on the filename at the bottom of the finder window where it shows all the folders leading to the file and choose: Copy "YourFile" as Pathname.
That path will look something like this:
"/Users/NAME/Library/Mobile
Documents/com~apple~CloudDoc/Docs/YourFile.csv"
Use that in your read code:
iCloudDat <- read_csv("/Users/NAME/Library/Mobile Documents/com~apple~CloudDocs/Documents/YourFileName.csv")
That should work.
If the extension isn't .txt or .csv read.table and read.csv won't work.
you have to download the file and extract the tables to a readable format.
you can download the file using download.file() which is is the utils basic library.

R set working directory to current folder

I am currently working on a joint project, with all the files stored in a Dropbox. To avoid changing the working directory every time we change something, we use the following code:
this.dir = dirname(parent.frame(2)$ofile)
setwd(this.dir)
This is working fine when sourcing the code in Rstudio, but we are looking for a solution that is working outside RStudio.
The team is working on Mac and Windows.
For Windows, locate the file "Rprofile.site". In there, there will be set the following command setwd("PATH_TO_WD")
The same goes for Mac, the file is usually located at /etc/R/ path.
Just change the setwd in that file and everytime you launch R, the working directory will be set to the directory you want.
EDIT: In my Windows computer, the file is located at "C:\Program Files\R\R-3.2.3\etc

R workspaces i.e. .R files

How do I start a new .R file default in a new session for new objects in that session?
Workspaces are .RData files, not .R files. .R files are source files, i.e. text files containing code.
It's a bit tricky. If you saved the workspace, then R saves two files in the current working directory : an .RData file with the objects and a .RHistory file with the history of commands. In earlier versions of R, this was saved in the R directory itself. With my version 2.11.1, it uses the desktop.
If you start up your R and it says : "[Previously saved workspace restored]", then it loaded the file ".RData" and ".RHistory" from the default working directory. You find that one by the command
getwd()
If it's not a desktop or so, then you can use
dir()
to see what's inside. For me that doesn't work, as I only have the file "desktop.ini" there (thank you, bloody Windoze).
Now there are 2 options : you manually rename the workspace, or use the command:
save.image(file="filename.RData")
to save the workspaces before you exit. Alternatively, you can set those options in the file Rprofile.site. This is a text file containing the code R has to run at startup. The file resides in the subdirectory /etc of your R directory. You can add to the bottom of the file something like :
fn <- paste("Wspace",Sys.Date(),sep="")
nfiles <- length(grep(paste(fn,".*.RData",sep=""),dir()))
fn <- paste(fn,"_",nfiles+1,".RData",sep="")
options(save.image.defaults=list(file=fn))
Beware: this doesn't do a thing if you save the workspace by clicking "yes" on the message box. You have to use the command
save.image()
right before you close your R-session. If you click "yes", it will still save the workspace as ".RData", so you'll have to rename it again.
I believe that you can save your current workspace using save.image(), which will default to the name ".RData". You can load a workspace simply using load().
If you're loading a pre-existing workspace and you don't want that to happen, rename or delete the .RData file in the current working directory.
If you want to have different projects with different workspaces, the easiest thing to do is create multiple directories.
There is no connection between sessions, objects and controlling files .R. In short: no need to.
You may enjoy walking through the worked example at the end of the Introduction to R - A Sample Session.
Fire up R in your preferred environment and execute the commands one-by-one.

Resources