Issue: Working Directory in R Studio "stuck" on directory containing open R Markdown file - r

I am using the most recent version of R (3.3.2), running in the most recent version of RStudio (1.0.136) on MacOS Sierra (10.12.3). I am running into an issue in which my working directory corresponds, and is stuck on, the directory that contains the .RMD file I currently have open in RStudio. Upon opening the file, the working directory is correctly set to the directory holding the .Rproj file. When I go to load in a file with a path relative to that directory, however, I get an error that there is no such file in the current working directory, and the error returns the location of the .RMD file as that working directory.
The working directory, however (using getwd()) still reads where the working directory is supposed to be, and no matter where I try to set it, I still get the same error message when I try to read in a file. Notably, I do NOT get an error message that the working directory cannot be changed--R tells me that the working directory has been changed, and that directory is allegedly the current working directory...but it's not.
I have tried fully (as far as I am aware) uninstalling R and R studio and reinstalling them, to no avail. Does anyone have a solution? This is frustrating the heck out of me right now, since I have to revise all the relative paths in the notebooks that I have defined to do my work in the interim.
Extra information in case it's relevant: I restored from a Time Machine backup that I suspect may have been corrupted somehow; some contents of my Applications folder were missing that I had to move over manually. Could this be causing the issue? Are there other system files that R depends on when interacting with the filesystem that I might look to? I'm trying to avoid doing a clean OS install or a piecemeal rebuilding of my files, since I don't know if that's actually the issue.
Thanks in advance!

This is a known feature/bug of RStudio notebooks (Working Directory about halfway down). Notebooks are executed in the same directory as the file. As #Simon Jackson noted, you can change this using knitr::opts_knit$set(root.dir = normalizePath()).

Related

What's the reason that readRDS doesn't seem to work in Windows?

I'm trying to make the my code portable to Windows and realized that even though I use file.path to create paths still the readRDS function won't work, for example:
file.exists('C:/temp/HarvardX-Skillability/data/rds/Users.rds')
> TRUE
readRDS('C:/temp/HarvardX-Skillability/data/rds/Users.rds')
> Error in readRDS("C:/temp/HarvardX-Skillability/data/rds/Users.rds") :
error reading from connection
I also tried:
file.exists('data/rds/Users.rds')
> TRUE
readRDS('data/rds/Users.rds')
> Error in readRDS("data/rds/Users.rds") : error reading from connection
Why is that? and how can I fix it? In Ubuntu 18.04 works perfectly ...
The problem seems to be with downloading/cloning the files from GitHub. After running git clone on your repository, when I open my local copy of Tags.rds in a text editor I see this:
version https://git-lfs.github.com/spec/v1
oid sha256:b4a2cb3775126a3895e9533ef9ef4ad786b2021cfd1660b07028fbef85b025bb
size 641098
(this is the entire contents of the file). Furthermore, running file Tags.rds (in a Terminal on MacOS) reports Tags.rds: ASCII text. (All of the .rds files are like this.)
The GitHub web interface confirms that your files are OK on the repo:
This question looks related. After installing Git LFS and running git lfs pull, I get the full file downloaded (and readRDS() seems to work fine).
The culprit to the OP was something really unexpected, I also didn't provide the information for it because I couldn't suspect this was the issue.
The problem was those files were being downloaded automatically using download.file(url, filePath, extra="L") and in Windows this is known to cause issues with binary files that are not the expected ones. This is why the rds files were unrecognizable.
I found out while building exception handling recovery code that was looking to download the same files from a Dropbox folder and then came to the same issue, therefore it wasn't because of Git LFS.
The OP solution was to add the argument download.file(..., mode="wb").
See the question R trouble unzipping file under Windows

RStudio states file does not exist

Attempting to use the mread function to open a cpp file through R. However, when I run the script I get the following:
setwd("C:/Users/Gustavo/Documents/R/page-2018-mrgsolve-master/model")
getwd()
#> [1] "C:/Users/Gustavo/Documents/R/page-2018-mrgsolve-master/model"
library(mrgsolve)
mod <- mread("simple", "model")
#> Error: project directory 'model' must exist and be readable.
Obviously I am setting the directory to "model" itself. So why isn't R able to read it? Any help would be appreciated as I am still learning R and want to learn the mrgsolve package as well.
Additional info: R version 3.4.4. Rtools version 3.4.0. Rstudio version 1.1.463.
An adaptation to the email I sent my colleagues that were assisting me with a similar issue:
To review, I was unable to open any files through RStudio because RStudio returned error messages indicating either that the file itself or the work directory did not exist. I've done multiple installations of different versions of R, RStudio, and Rtools in an attempt to resolve the issue. I also moved the locations of files and programs of interest and changed the work directory to see if that made a difference. Unfortunately, when RStudio is first initiated on a computer, it establishes a "hidden directory" folder that retains the settings of the program when it was first initiated. However, by deleting this folder, RStudio was wiped and I was able to regain control of where files would be stored and read as desired (more on this in the following link: https://support.rstudio.com/hc/en-us/articles/200534577-Resetting-RStudio-Desktop-s-State). A combination of this and forcing Rtools to the front of the 'path' also allowed me to resolve 'status 127' errors that I was receiving as well.
Unfortunately, this is the result of a more personal issue between the initial settings that RStudio took to my computer and my attempt to manipulate where RStudio should read files which I guess were discordant of one another? Regardless, it seems that I would need to be more cognizant of how RStudio establishes a folder which retains its initial settings.

Cannot change working directory to folder in windows through R in Ubuntu command line

I have had to install Ubuntu command line on windows10 in order to install R and download a package that is not supported on R for windows. This has worked fine, however, I need to set the working directory in R to the folder containing the files the package needs to work with. I have looked at lots of forums about changing directory and I have no luck!
My default working directory is a hidden folder created by windows when I installed ubuntu command line. I can access this, but I dont need this folder...I need to change the working directory in R to a folder on the D drive on my computer. I have tried to no avail!!
I have tried the normal setwd() command with the following combinations, all of which return the error 'cannot change working directory'
setwd("D:/RNA-seq data/")
setwd("d:/RNA-seq)
setwd("~D:/RNA-seq)
I am really frustrated that I cannot carry out this simple command (maybeI am doing something fundamentally wrong) or maybe this is a limitation because of the hidden default home folder set by windows? Please not I cannot transfer the necessary files in to the default folder created by windows for 'home/R' as it is on the C drive and there isnt enough space on disk. I need to navigate to the folder on d drive in order to carry out my code!!
Any help would be really really appreciated! Many thanks!

Cannot permanently Change R Working Directory - Windows 10

I am running R 3.5.0 64 bit on my Windows 10 laptop.
I can set the Working directory fine for my work space but I am unable to make a permanent change. Each time I reload it, it does not stick.
I changed my Start In file path to the new directory under properties when right clicking the icon.
I have also added setwd() with my file path in the Rprofile.site file
No matter what I do, it continues to revert back to documents when I run getwd() in R when I open it new.
Anyone have any ideas on what I can do to make it stick?
Thanks in advance
You could go the route of using an .Rprofile which is a way to run scripts / commands on startup.
Read about .Rprofile files here in Efficient R Programming
Also, has has been mentioned by #r2evans, if you're using RStudio, there is an option for setting the default working directory. It's under the General section of the Tools -> Global Options menu.

R Markdown: openBinaryFile: does not exist (No such file or directory)

I've developed a shiny app that allows user to download a HTML report via R Markdown. I'm trying to include custom css and images into my rmarkdown file. However, I keep getting this error message:
pandoc: Could not fetch (either css or image file)
openBinaryFile: does not exist (No such file or directory)
When I knit the .rmd file on R Studio, it is able to reference the image file or css that I want. However, when I run the Shiny app and download the html file, I get the above error message. I've even tried to put the images and css files in the same working directory as the .rmd file, but to no avail...
output:
html_document:
css: pandoc.css
(same error message as above)
Been trying to find a solution for this but can't seem to...can anyone help here?
I just had this issue as well, but for me the reason was that the RStudio project was on a shared drive, and I had opened it through the network location. The problem was resolved when I closed out of the project, and opened it back up through a mapped network drive. (If when you run getwd() your location starts with \\, this is probably what is happening to you.)
I had a similar problem. I was not using the full file path. I was using ~/path/to/file. I changed it to the full path (i.e. removed the ~/) and it worked.
I recently ran into the issue on my Windows work-computer where I simply set the .libPaths() in the Rprofile.site file. This in line with previous answers but a little more detailed.
Step 1
Check your current paths:
> .libPaths()
[1] "\\\\my_work_server.se/some_subdir$/username/Dokument/R/win-library/3.6"
[2] "C:/R/R-3.6.3/library"
Step 2
Look for the \\\, in this case it is the path "\\\\my_work_server.se/some_subdir$/username/Dokument/R/win-library/3.6". This path is most likely some already mounted home directory, in my case it is H: = "\\\\my_work_server.se/some_subdir$/username/. If you don't have a mounted directory you may want to fix this first or change the library path to another.
Step 3
So if you've installed R under C:/R/R-3.6.3/ you edit the file C:/R/R-3.6.3/etc/Rprofile.site and add:
.First <- function(){
.libPaths(c("H:/Dokument/R/win-library/3.6", "C:/R/R-3.6.3/library"))
}
Remember to change H: to where you have your mounted network directory.
Step 4
That's it, restart R and you should be able to knit your document.
The issue that I had with RStudio & the pandoc error (openBinaryFile error) was due to the file path in which the project was created and loaded.
Project File Path
When I created the project, I created it using the Universal path, which is the 2nd option in the image above. However, when I changed this to the mapped drive letter, the option above it, my pandoc error was gone.
I'm running RStudio 1.2.1335 and R version 3.4.4
I have been having a similar issue, with RStudio, rmarkdown and pandoc on a windows machine with network filestore. I followed various advice, mapped the drive to a letter and it still didn't help.
Eventually, I discovered that one of the paths in my libPaths contained the network location/Universal path. I updated that libpath to the mapped letter drive and everything seems to be going well!
I had a similar error message, but in my case the problem was that I used a # symbol in one of the chunk titles:
```{r show distribution of # of commits per month}
Which is no problem if you just run the notebook in Rstudio, but apparently confuses knitr/pandoc: when knitting, I got an error message like
pandoc: <path>/figure-html/show distribution of : openBinaryFile: does not exist (No such file or directory)`)
Removing the # from the chunk title solved the problem.
I just ran into the same message when running:
pandoc -s foo.html foo.md
Where I totally missed the -o flag as it should have been
pandoc -s -o foo.html foo.md
With the latter, everything is working like a charm.
I believe that I had the same issue. I first had tried Changing the Default Directory but every time I went to knit the RMarkdown file, I would get the same set of errors indicating that the process was still trying to access files on my Network's H drive rather that my local C drive, specifically it was looking in the rmarkdown library file on the network drive. I thought I was following the advice above, but after that did not work I tried deleting the rmarkdown folder in the network drive
eg: \\fwnew12\Home\My Documents\R\win-library\3.6\rmarkdown.
This seemed to force R to only use my local C drive (C:/Program Files/R/R-3.6.3/library) and finally successfully knit a PDF. Maybe this is not a recommended approach, but I just need something that works.

Resources