Unable to change directory in R Studio - problem with oneDrive - r

I am facing a problem while trying to change directory to read a csv in Rstudio.
When I type getwd() I get this as my directory:
C:/Users/giorg/OneDrive/Υπολογιστής/MSc_Data_Science_&_CI/Introduction to Statistical methods for Data Science/Coursework
So when I try to read my csv I get that error:
C:/Users/giorg/OneDrive/ÕðïëïãéóôÞò/MSc_Data_Science_&_CI/Introduction to Statistical methods for Data Science/Coursework/x.csv': No such file or directory
I tried to change through Global options > General but I still have oneDrive in my path
And when I try with setwd() I get :
setwd("C:/Users/giorg/OneDrive/Υπολογιστής/Giorgos")
Error in setwd("C:/Users/giorg/OneDrive/<U+03A5>p<U+03BF><U+03BB><U+03BF><U+03B3><U+03B9>st<U+03AE><U+03C2>/Giorgos")
cannot change working directory
Any suggestions about what should I do?

I had the same problem at some point. When I tried to change the directory I got the same error. In my case, the problem started when I changed the language of my computer. The file which I wanted to use as directory named in Greek language and after I turned my computer to English, R couldn't recognize it.
The problem solved when I renamed the file with English characters.

Related

Why do I keep getting an error when trying to import my file?

I'm trying to read csv file using R notebook and keep getting this error:
Error: 'Examples/data/starbucks.csv' does not exist in current
working directory ('C:/Users/c227466/Desktop')
I'm not sure what's going on!
This is the code I used:
starbucks <- read_csv("Examples/data/starbucks.csv")
starbucks
Your working directory is your desktop (which is not recommended). On your desktop, you should add a folder "Examples" and, in it, another folder "data" and your file there.

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.

R studio will not remember my file path for a code chunk

When I run the following code I get an error.
library(tidyverse)
day <- read.csv("day.csv")[, -1]
glimpse(day)
Result:
Show in New Window
Error in file(file, "rt") : cannot open the connection
However when I specify the file path it works:
library(tidyverse)
setwd("~/UofUProjects/IS6489")
day <- read.csv("day.csv")[, -1]
glimpse(day)
I don't want to have to specify the file path for every single code chunk I run. Is this a setting I may have accidentally disabled? I tried deleting an re adding the file. I also tried clicking more and choosing set working directory and I still get the same error. I also tried updating Rstudio and R to the newest versions.
here is my working directory
The R markdown file I was using was saved in my download folder instead of my project folder. I was trying to specify the working directory folder with the R studio settings, and thought it would recognize however it did not work unless I actually saved the R markdown file in the project folder or used setwd(). I ran the code without the setwd() after I saved in the correct location and it worked just the way I wanted it to.

Error in gzfile(file, "rb") - what should I do?

I'm going to write shortly, because my english is not perfect.
My code is:
explanatory=readRDS("explanatory_complete.Rds")
And I want knit pdf or word document, I receive this message:
Error in gzfile(file, "rb")- can't oppen connection
Where is problem? I set the right working directory.
Try with explanatory<-readRDS('filename.rds') this works for me.
Not sure if this is your case but I ran into
Error in gzfile(file, "rb")- can't open connection
when I tried to read a large file after it was downloaded using rsync. This was due to different file access permissions set by rsync than my Windows 10 system. The error was fixed in a local MobaXterm session using
chmod u+rwx filename
(change filename to your files)
The file likely doesn't exist in your working directory. This often happens to me when I have changed my working directory and forgotten to change it back.
Have you tried saving the new R/RMD file first?
I faced the same issue and problem was that, I was not saving the new file first therefore the R session was unable to understand the path for RDS file.
After I saved the file (in the same folder, but not a necessity) and ran the readRDS, it worked.
I had this issue. For me, it was being caused by an update.packages() call in a script my RMD document was sourcing. I'm sure there is a more elegant solution, but I just put a # in front of this line of code for knitting this RMD and then removed it again after
Check your exact file name and the working directly. it might be .rds missing in your saved file name.

How to change .Rprofile location in RStudio

I am working with a "factory fresh" version of RStudio on Windows 7. R is installed under C:/Program Files which means the default libraries are stored here, and the two locations contained in .libPaths() on startup are both within this folder.
I want to work with another R library (igraph). Since the C:\Program Files folder is write-protected, I have set up another area to work in: C:\Users\nick\R and installed the igraph library in C:\Users\nick\R\library. I can manually add this location to the .libPaths() variable and use the library with no problems.
However, my problem is getting RStudio to automatically add this location to the .libPaths() variable on startup. I read that I could add the relevant command to my .Rprofile file - but I couldn't find any such file (presumably they are not automatically created when RStudio is installed). I then created a file called .Rprofile containing only this command. This only seemed to work when the .Rprofile file was saved in C:\Users\nick\Documents (which is the path stored in both the R_USER and HOME environmental variables). What I would like is to have the .Rprofile file stored in C:\Users\nick\R.
I have read all the information in ?Startup and it talks about where to store commands that run on startup. But I just can't make this work. For example there seems to be no way to change the location of the home directory without reading a file stored in the home directory. I don't seem to have any .Renviron files and creating these myself doesn't seem to work either.
I would really appreciate an answer in simple terms that explains how I could go about changing where the .Rprofile file is read from.
In Windows, you set the R_USER profile by opening up a command line and running:
SETX R_PROFILE_USER "C:/.../.Rprofile"
Where (obviously) the path is the path to your desired .Rpofile. In R, you can check that it worked:
Sys.getenv("R_PROFILE_USER")
Should return the path you specified. Note that you likely need to have all R sessions closed before setting the R_USER variable.

Resources