1

I just installed R and RStudio, then ran the following sample code from a textbook:

# Install Packages
install.packages(fpp3)

# Load Packages
library(fpp3)

# Plot one time serices
aus_retail %>%
  filter(`Series ID` == 'A3349640L') %>%
  autoplot(Turnover)

# Produce some forecasts
aus_retail %>%
  filter(`Series ID` == 'A3349640L') %>%
  model(ETS(Turnover)) %>%
  forecast(h = '2 years')

I get this error:

Error in file(con, "w") : cannot open the connection
In addition: Warning message:
In file(con, "w") :
  cannot open file 'C:/Users/Cindy SC/OneDrive/Documents/.active-rstudio-document': No such file or directory

When I do getwd() I get:

> getwd()
[1] "C:/Users/Cindy SC/Documents/R"

I have already looked through the following pages, but their solutions don't seem to be what I need (I could be wrong and misunderstand what I am looking at): R: Error in file(con, "r") : cannot open the connection Error in file(con, "w") : cannot open the connection [Using R-Studio to plot interactive bar graphs using rCharts, knitr] Change temporary directory Cannot run scripts in Rstudio

I also don't know why it is looking for active-rstudio-document. It is not a file or folder I created and when I searched my computer for it, it didn't show up anywhere.

Any help would be greatly appreciated. Thank you.

UPDATE: I reread the advice on Change temporary directory and the second solution seems to have solved this issue...only to introduce another issue, but I won't present it here. I did have to edit both an Rprofile and an Renviron file to make it work.

Cinji18
  • 619
  • 8
  • 22
  • Hmm. I don't know anything about rstudio or R, but if i were you i'd do the following - first, i wouldn't use oneDrive as my workspace, there's a slight chance it's messing stuff up. (like read/write permissions. perhaps antivirus protection on a documents folder? unlikely though). You can switch back to onedrive once you rule that out. Then, i would try a simpler software. like a "hello world", see if that works, then try to figure out how to move forward. – Yarin_007 Dec 25 '22 at 23:11
  • I never wanted to use OneDrive as my workspace. But I don't remember the installer asking about my workspace nor do I know how to change it. Apparently, permanently changing my working directory didn't do the trick. – Cinji18 Dec 25 '22 at 23:34
  • You could try *changing your tmp directory* as suggested in this solution (which received quite a few upvotes)? https://stackoverflow.com/a/17108351/20513099 – I_O Dec 26 '22 at 00:58
  • @I_O But isn't that just temporary? – Cinji18 Dec 26 '22 at 06:13
  • I'd run getwd() and tempdir() and check that the paths they return both exist, and that you can save to them. Same with C:/Users/Cindy SC/OneDrive/Documents/, though that's the standard OneDrive folder, and it's hard to imagine that's the issue. I've found OneDrive works very well. – Isaiah Dec 26 '22 at 07:07

1 Answers1

1

If you are using Windows 10, it's possible that Controlled Access Folder is blocking something called "Rterm.exe" and you need to add it to the allowed apps list.

For context, I was trying to knit the prepopulated RMarkdown file template but got an error similar to what you received:

processing file: Lesson01_R_Markdown_Intro.Rmd
  |...................................................        |  86% [pressure]Error in file(con, "w") : cannot open the connection
Calls: <Anonymous> ... signalCondition -> <Anonymous> -> write_utf8 -> writeLines -> file
In addition: Warning messages:
1: In dir.create(dirname(name), recursive = TRUE) :
  cannot create dir 'Lesson01_R_Markdown_Intro_files', reason 'No such file or directory'
2: In png(..., res = dpi, units = "in") :
  unable to open file 'Lesson01_R_Markdown_Intro_files/figure-html/pressure-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
4: In file(con, "w") :
  cannot open file 'Lesson01_R_Markdown_Intro.knit.md': No such file or directory
                                                                                                     
Execution halted

At first, I thought it was a caching issue after reading some Stackoverflow posts. The answers I saw suggested restarting RStudio. However, this didn't work for me. I then looked to see if Windows may be blocking something because Windows blocks a lot of R and RStudio related files.

An executable file called "Rterm.exe" seems to be the one that is trying to trigger the RMarkdown HTML page. However, due to security protocols on Windows 10 end, this (and other R/RStudio executable files) are blocked because they are trying to access protected folders. You have to allow this executable to access your protected folders.

If you navigate to the Controlled Access Folder (just search for it using the Windows search), you'll be able to see the blocked list by clicking "Block History".

To allow an executable access to your protected folders, you need to:

  1. Click "Allow an app through Controlled Folder access"
  2. Click "Add an allowed app"
  3. Click "Recently blocked apps"
  4. Click the + for all R/RStudio related executables

Please note: There may be two Rterm.exe (one possibly within your Programs folder and another one within a PROGA~1 folder). Add both to the allowed apps list.

You may have other files you need to add as well. Please see the image link below:

Image of all R and RStudio executables to allow

Once you do that, restart RStudio and you should be able to knit (should look like below)

processing file: Lesson01_R_Markdown_Intro.Rmd
                                                                                                     
"C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS Lesson01_R_Markdown_Intro.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output Lesson01_R_Markdown_Intro.html --lua-filter "C:\Users\~~~~~~\AppData\Local\R\WIN-LI~1\4.3\RMARKD~1\RMARKD~1\lua\PAGEBR~1.LUA" --lua-filter "C:\Users\~~~~~~\AppData\Local\R\WIN-LI~1\4.3\RMARKD~1\RMARKD~1\lua\LATEX-~1.LUA" --embed-resources --standalone --variable bs3=TRUE --section-divs --template "C:\Users\~~~~~~~~~\AppData\Local\R\WIN-LI~1\4.3\RMARKD~1\rmd\h\DEFAUL~1.HTM" --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\~~~~~~\AppData\Local\Temp\RtmpMF1t6W\rmarkdown-str34846a645384.html" 
output file: Lesson01_R_Markdown_Intro.knit.md


Output created: Lesson01_R_Markdown_Intro.html

I hope this helps :)!