4

I want to run Rmarkdown from the command line that will save an HTML output (I don't care if the command line shows me the plot as it's run or not, as long as the result is saved).

My Rmd script is something like:

---
title: "Report"
output: html_document
---
`r load("C:/Users/durlij/Desktop/Reports/.RData")`
## This is the Report as of `r format(Sys.Date(), format="%B %d %Y")`.

`r library(knitr)`

```{r error = FALSE, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
kable(sumTable, format = "markdown")
```


### Visualization of data

`r library(ggplot2)
library(scales)
options(scipen = 999)`


```{r fig.width=8, fig.height=6, echo=FALSE}
ggplot()
```

```{r fig.width=8, fig.height=6, echo=FALSE}
ggplot()
```

In the command line I run

Rscript -e "library(knitr);require(methods);knit(C:/Users/durlij/Desktop/Reports/myFile.Rmd)"

It starts to run, completes the first in-line code chunk, but gets an error on the ggplot.

The error is:

Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: knit ... <Anonymous> -> png -> .handleSimpleError -> h -> cat -> file
In addition: Warning messages:
1: package 'scales' was built under R version 3.1.3
2: In png(..., res = dpi, units = "in") :
  unable to open file 'figure/unnamed-chunk-2-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
4: In file(file, ifelse(append, "a", "w")) :
  cannot open file 'rmdRun.md': Permission denied

The whole command line echo is:

Warning message:
package 'knitr' was built under R version 3.1.3
Loading required package: methods


processing file: C:/Users/durlij/Desktop/Reports/myFile.Rmd
  |.....                                                            |   8%
   inline R code fragments

  |..........                                                       |  15%
label: unnamed-chunk-1 (with options)
List of 5
 $ error   : logi FALSE
 $ echo    : logi FALSE
 $ message : logi FALSE
 $ warnings: logi FALSE
 $ results : chr "asis"

  |...............                                                  |  23%
   inline R code fragments

  |....................                                             |  31%
label: unnamed-chunk-2 (with options)
List of 3
 $ fig.width : num 8
 $ fig.height: num 6
 $ echo      : logi FALSE

Error in file(file, ifelse(append, "a", "w")) :
  cannot open the connection
Calls: knit ... <Anonymous> -> png -> .handleSimpleError -> h -> cat -> file
In addition: Warning messages:
1: package 'scales' was built under R version 3.1.3
2: In png(..., res = dpi, units = "in") :
  unable to open file 'figure/unnamed-chunk-2-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
4: In file(file, ifelse(append, "a", "w")) :
  cannot open file 'rmdRun.md': Permission denied

Execution halted
0xb36a
  • 41
  • 3
  • inline code is not meant to be used like that, especially the part where you are using inline to run three lines of code. Is the file you're working on `C:/myFile`?, shouldn't it be on your `desktop/reports`? do you have write permissions on c? – rawr Aug 04 '15 at 16:06
  • @rawr Edited my post to clear up confusion. Yes it's on desktop/reports. I can run my Rmd file manually. How would you suggest I edit it to make it work in this context? Thanks! – 0xb36a Aug 04 '15 at 17:42
  • I find this happens when you have a previous version of one of the target files to be created, already open – Henry E May 19 '16 at 13:09
  • @rawr I have the same problem, and `write.csv(..)` works from the console, but not inside a code chunk. I wonder if it's something to do with permissions, and how to test that? – dss Oct 16 '20 at 02:59
  • @dss when you knit, the working directory changes to where the .Rmd file is, and all of your paths should be relative to that file (you could use absolute file paths as well, but that affects portability), so that is more likely the issue – rawr Oct 16 '20 at 15:34
  • @rawr actually, I'm not knitting, just running the code chunk (e.g. cmd + option + c to run current chunk in RStudio)! (it's a very puzzling problem). I'm doing this in rstudio server in the cloud, so I suspect there's possibly some weird bug with permissions, and possibly not even to do with R/Rmd, but to do with the linux machine it's running on. I'll update if I find anything further. Any suggestions appreciated – dss Oct 18 '20 at 15:31

0 Answers0