Render rmarkdown from RStudio server to external drive - r

When working locally, I use the following to render rmarkdown documents:
rmarkdown::render(input = rmd.temp , output_dir = out.dir, output_file = out.name)
This gets my template doc rmd.temp, then renders the document to out.dir and names the file out.name.
I am trying to replicate on a RStudio server. When out.dir is a local directory the code works fine. However, whenout.dir is a mapped directory to an external drive, I get the following error:
Error in replayPlot(x) :
could not open file'/...path..../figure-html/unnamed-chunk-2-1.png'
The link to the mapped drive works because I can use:
dir.create(out.dir)
and see the new folder appear. Any ideas why the rendering gets stuck?
Update
This is perhaps just a permission problem because I can't write a csv file either:
df <- data.frame(a=1:10)
setwd(out.dir)
write.csv(df, file = "df.csv")
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
RStudio server is running on Ubuntu. I will open a new question on how to give RStudio folder permissions.

For anyone who has this problem, it can be fixed by adding this to the start of your script:
Sys.umask(mode="0000")
It was a permission problem. From the shell / command line, global permission was "0000" but from the RStudio console it was "0002", which produces -rw-r-- permissions for new files. The global permission can probably be changed for RStudio but this works.

Related

cannot open file 'test.csv': No such file or directory Error in file(file, "rt") : cannot open the connection

I'm having issues reading the data on the desktop version of Rstudio on mac. When I do the usual read.csv it shows the error shown in the title. This is the first time this has happened. I have tried to change my working directory in 'Sessions' at the top and also to read the file with its granularity
You could try rstudioapi library to set an unique wd for your script path and then use read.csv() where path= "test.csv"
library(rstudioapi)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
Test <- read.csv("test.csv")

R - Why am I getting an error message in a shiny server?

I'm currently usinga shiny server using DigitalOcean with Ubuntu as the operating system. I created a survey with the hopes of collecting user inputs and writing them in a csv file.
Locally, it works (i.e. responses get saved in a local folder). However, when I developed a shiny app within my new R Studio server to test it, there is an error which reads:
"Error: cannot open the connection"
This occurs when I press the submit button. I think the error occurs because my shiny server doesn't have rights to save files in my desired folder, i.e.
/home/my_user/test
The folder I'd save the responses would be located in that shiny folder in my new R Studio directory.
I've checked extensively on the website and I found:
Write permission from shiny app
Permission denied while using write.csv on ubuntu server
This is my saveData function:
saveData <- function(data) {
fileName <- sprintf("%s_%s.csv", humanTime(), digest::digest(data))
write.csv(x = data, file = file.path(responsesDir, fileName), row.names = FALSE, quote = TRUE)
}
The response directory (responsesDir) is
/home/my_user/test/csv_files
I've tried the following:
sudo chown shiny:shiny /var/shiny-server/test/
It says that there is no such file or directory.
Am I missing something very obvious? Thank you so much!

How to remove a 'permission denied' file from folder within R

I am downloading a large xlsx file as a part of a function. This file is removed with file.remove() in linux and mac but I have permission denied in windows machines. Below is the code for my function.
download.file(
'http://mirtarbase.mbc.nctu.edu.tw/cache/download/7.0/miRTarBase_MTI.xlsx',
'miRTarBase.xlsx', mode = "wb")
readxl::read_excel('miRTarBase.xlsx') -> miRTarBase
write.csv(miRTarBase, 'miRTarBase.csv')
read.csv('miRTarBase.csv', row.names = 1) -> miRTarBase
file.remove("miRTarBase.xlsx")
I get the following error message in my console
Warning message:
In file.remove("miRTarBase.xlsx") :
cannot remove file 'miRTarBase.xlsx', reason 'Permission denied'.
Again this warning only appears in windows.
Furthermore, after checking the properties of the file itself the 'Read-only' attribute is unchecked.
Following this, the following code works perfectly fine so I do not think the issue is with the folder either.
file.remove("miRTarBase.csv")
I believe the issue lies in how .xlsx files are treated in windows.
When I try to delete the .xlsx file while Rstudio is still running I get a File in use warning message. After closing the R session the .xlsx file can be deleted with no hassle.
This has confused me because I am not used to working with windows. Has anyone had this issue before? Would appreciate any help that can be given. Many thanks.
Have you tried saving as a temporary file in windows?
tmp <- tempfile()
download.file(
'http://mirtarbase.mbc.nctu.edu.tw/cache/download/7.0/miRTarBase_MTI.xlsx', tmp, mode = "wb")
readxl::read_excel(tmp) -> miRTarBase
write.csv(miRTarBase, 'miRTarBase.csv')
read.csv('miRTarBase.csv', row.names = 1) -> miRTarBase
file.remove(tmp)

Why is the working directory overwritten to the directory of the current Rmd file?

I have an R.proj file called Food_Choices.Rproj that is supposed to be setting my working directory to ~/Desktop/Food_Choices, a folder containing reproducibility files according to the TIER system like
But it's not setting the working directory properly, because when I knit my processing file with code like this
food<-read_csv("Original_Data/food_coded.csv")
#imagine some processing code in between here
write.csv(food, file = "Analysis-Data/analysis_data.csv")
I get this error:
Error: 'Original_Data/food_coded.csv' does not exist in current working directory ('/Users/IdanCarre/Desktop/Food_Choices/Command_Files').
Which is not the project directory, it's the directory of the processing file!
I thought I set the working directory when I opened the files in the context of the R project, but that doesn't seem to be happening anymore (even though my files from a year ago with the same setup still work??)
NOTE: I don't want to use
library(knitr)
opts_knit$set(root.dir = '/Users/IdanCarre/Desktop/Food_Choices')
Because then new users who want to reproduce the results have to go manually insert their own directory into each file they want to run. That's a lot of work they shouldn't have to do.
UPDATE TO COMMENTS:
I used the here package, and that works satisfactorily for read.csv (it throws a data column de-duplication warning but I think it's probably okay for now), but when I write out the processed data file to the analysis data folder, I'm trying to use
write.csv(food, file = here("Analysis-Data", "analysis_data.csv"))
And the error I get is
Error in file(file, ifelse(append, "a", "w")) : cannot open the connection
I get this same problem if I use
write.csv(food, file = "Analysis-Data/analysis_data.csv")

Cannot export data to a file in R (write.csv)

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:
I create a simple data vector to export
x <- c(1,3,4,3,5,7,5,8,2,5,7)
I try to export with:
write.csv(x,file='whatever.csv')
And I get an error:
error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open file 'whatever.csv': Permission denied
How can I solve this?
First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.
To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.
I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.
write.csv(data, file="C:/project/file.csv")
If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.
I got this error today and fixed it by granting everyone write permission to the folder.
Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"
I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.
Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:
library(dplyr)
library(hflights)
path_to_hflights_as_csv <-
file.path(path.expand("~/MyHomeSubDir"),
"hflights.csv")
write.csv(hflights, path_to_hflights_as_csv)
path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".
Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):
https://en.wikipedia.org/wiki/Home_directory
https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).
I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'censoredpath.file.csv': Permission denied
I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

Resources