I am trying to set up a task that runs a batch-file, that runs a .rmd file which should knitr me a lovely .html file each day.
Everything works fine, if I run the batch-file manually. However, when I run it through the task scheduler, I get the following error from the command prompt:
Error in file(con, "w") : cannot open the connection
Calls: <Anonymous> -> knit -> writeLines -> file
In addition: Warning message:
In file<con, "w") : cannot open file 'residual_v1.md" : Permission denied
Execution halted
The same user is listed as the "author" in the task-scheduler, as the user when you open the start menu.
Batch-file code:
"C:\R\R-3.0.3\bin\x64\Rscript.exe" -e "library(knitr,dplyr); knitr::knit2html('C:/R/Rapporter/residual_model/Residual_v1.Rmd')"
Im am at a loss of what to do.
It looks you do not have write permission in the working directory of R. I'd recommend you to set the working directory before you run knit2html(), e.g.
setwd('C:/R/Rapporter/residual_model/')
knitr::knit2html('Residual_v1.Rmd')
i.e.
"C:\R\R-3.0.3\bin\x64\Rscript.exe" -e "setwd('C:/R/Rapporter/residual_model/'); knitr::knit2html('Residual_v1.Rmd')"
Or any other output directory in which you have write permission:
setwd('any/output/directory/you/want')
knitr::knit2html('C:/R/Rapporter/residual_model/Residual_v1.Rmd')
Related
Shiny app runs fine on RStudio, but it doesn’t run on public server, throwing this error instead:
Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
cannot open file 'Rplots.pdf'
Calls: runApp ... ..stacktraceon.. -> <Anonymous> -> par -> <Anonymous>
Execution halted
This error appears because there is code in your app that tries to generate a plot in a non-interactive R session (usually server.r or global.r).
To quote Yihui Xie:
From my experience, this is often an indication that your have some
code that generates plots in a non-interactive R session, and the
default graphics device for a non-interactive R session is pdf(). R
tried to capture the plot and writes Rplots.pdf, but failed probably
due to the fact that you don't have write permission to the working
directory. Your server.R contains a call to palette(), which I
believe will trigger Rplots.pdf. You can try to move it inside
renderPlot({}) and see if the problem goes away.
In my case, it was a leftover call to scales::show_col() in my global.r file, from testing some color pallettes.
I'm trying to get an R script to run from a batch file so it can be nice and clean for other users. Currently, you drag and drop a CSV file onto the batch file and it passes the file name to the R script for input.
When there's a space in the file path/name it works fine in RStudio but causes problems when I call it from the batch file. When I do that it tries to open the path before the space.
I've tried to reformat the file path from within R by using shortPathName(inputPath) and by replacing spaces with "\ " but it doesn't seem to work.
At the moment, the script is launched with
"%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~1" "%~dp0\Script.R"
with the script containing
args <- commandArgs(TRUE)
inputPath <- args[1]
inputPath <- shortPathName(inputPath)
inputData <- read.csv(inputPath)
It runs fine from within RStudio but crashes when launched from the batch producing this error message in the output file:
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
cannot open file 'file path up to the space': No such file or directory
Execution halted
By no means a R expert, but I'd try
%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~s1" "%~dp0\Script.R"
The %~s1 should supply the short filename as the argument.
After trying several formulations of the batch file and some debugging, I found that the batch file was passing the first part of the file before the space as the first argument.
After finding that the use of R in CMD BATCH mode is no longer advisable so switched to running using Rscript mode as
"%~dp0\R-3.6.0\bin\Rscript.exe" --vanilla "%~dp0\Script.R" "%~1"
This allowed for the argument to be passed to R with "", and hence with the space.
Since v3.5.1, R accepts file paths with spaces.
So I have a script written in R that pulls a table, named claims, from HUE. I now need to save that table onto my laptop. I tried this line:
readr::write_csv(claims, "C:/Users/user_name/Desktop/claims.csv")
But go this error message:
Error in open.connection(path, "wb") : cannot open the connection
In addition: Warning message:
In open.connection(path, "wb") :
cannot open file 'C:/Users/A0765618/Desktop/claims.csv': No such file or directory
I'm trying to publish an RMarkdown document to my RPubs account, but I get the next error when clicking on "Publish":
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:/Users/.../File.Rmd': No such file or directory
It also pops up a window saying the next two warnings:
First pop up: "Could not Publish - Error ocurred while executing method"
Second pop up: "Could not Publish - (TypeError): null is not an object evaluating 'b.g.')
My R version is 3.4.2, RStudio version is 1.1.383, knitr version is 1.17.
I have also checked is the working directory contains the files I'm using at the script, and all seems properly set.
This error only happens when I click on Publish. If I try to knit to HTML or to PDF it works. I have tried to restart and reinstall also both R and RStudio but the error persists.
Has anyone had the same error and knows how to solve it?
Thank you very much!
Is the file saved? Are you in the correct directory?
Please check getwd().
I managed to solve it. The problem was that the path of the working directory contained an accent. I changed the path to another folder without accents and it worked.
This is windows 10 context. R Script (as function) listens to some realtime data and then is supposed to play an audible alarm once some level is tripped. The alarm is activated by a separate function call, and that function simply plays an mp3 file which I play via the following
command: system("powershell.exe", input="C:/Users/ThisUser/Music/Alarms/alarm_alarm_alarm2.mp3").
Additional background is that I'm typically running 3 (or more) other rstudio sessions on the machine. This is the error:
Error in file(con, "w") : cannot open the connection In addition: Warning messages:
1: package ‘httpuv’ was built under R version 3.4.1
2: In file(con, "w") :
cannot open file 'C:\Users\ThisUser\AppData\Local\Temp\Rtmp9w7SM3\file44982a915cf4': No such file or directory
I can test that system command from the console in another rstudio session and it works fine, but if I test in the console of the session where the error resides, I continue to get the error. Also, when I check the tempdir this is what I get:
tempdir()
[1] "C:\Users\ThisUser\AppData\Local\Temp\Rtmp9w7SM3"
Are my other sessions messing up the Temp files or directory path? How can I fix? Thank you.