Error in file(con, "w") : cannot open the connection [Using R-Studio to plot interactive bar graphs using rCharts, knitr]

Viewed 11595

I am getting an error when I am trying to run the code below in R-Studio 3.3.2 on a Mac (OS Sierra)

devtools::install_github('ramnathv/rCharts')
install.packages("knitr")
require(rCharts)
require(knitr)

haireye <- as.data.frame(HairEyeColor)
n1 <- nPlot(Freq ~ Hair, group = 'Eye', type = 'multiBarChart',
        data = subset(haireye, Sex == 'Male')
        )

n1$save('fig/n1.html', cdn = TRUE)
cat('<iframe src="fig/n1.html" width= 100%, height=600</iframe>')

Pls see output below:

Error in file(con, "w") : cannot open the connection In addition: Warning message: In file(con, "w") : cannot open file 'fig/n1.html': No such file or directory

But I am able to generate the reqd bar graph in the viewer when I use:

n1$show(cdn = TRUE)

in lieu of n1$save('fig/n1.html', cdn = TRUE)

To take care of write permission issues (if any), I also tried including the line below, altering the WD path wherever necessary.

knitr::knit2html('Users/documents/n1.html')

But it did not help. I see the n1.html file created but it only opens an empty browser.

Any help to resolve this is appreciated.

Best, S

3 Answers

A lot of times we face this error due to caching in RStudio and in that case, actual code errors don't show up. Restart RStudio and this error will be gone and actual code errors would show.

I have fixed this problem with good old rm(list=ls()) . I know I have fallen into sequences where the error stops execution of my script. I fix the error, and then it won't run. This is likely due to lazy evaluation but it is a near impossible problem to diagnose, so the solution at the top works almost all the time.

Related