R/ RStudio: Error in file(file, "r") : cannot open connection // German // Macbook Air - r

I'm trying to do a confirmatory factor analysis on R for the first time.
I've researched this error and nothing helped so far (changing the working directory and so on).
Creating my model worked. But when I try to run the "fit" I get the error with
"Error in file(file, "r"): cannot open connection".
My first data frame is saved in "downloads" folder of my Mac. But I'm working with data Frame "data4" which is saved in a different folder. data4 is in the same folder as the whole project. It seems like the error has something to do with the path.
I also tried:
fit <- cfa(model, data = "~/Documents/BACHELORARBEIT/Daten/data4.RData, missing = "ML")
instead of:
fit <- cfa(model, data = data4, missing = "ML")
but I still get the same error message.
thank you in advance!!!

It seems you have set up the wrong working direction.
The easiest Solution would be to put all your files and the script in the same folder and just use on the toolbar:
Session -> Set working direction ->to source file.
Or check this post and see it can
help.

Related

"Error in file(file, "r") : cannot open the connection"

I'm trying to conduct CFA but suddenly this error appear and now I cannot use r anymore since no metter what data I use it always give me the "Error in file(file, "r") : cannot open the connection".
This is the code that I'm using right now:
data
data <- read.delim("C:/Users/Nyname/Desktop/data.txt")
View(data)
model
overall_data <- '
f1 =~ item_3 + item_4 + item_12 + item_16
f2 =~ item_1 + item_5 + item_9 + item_14 + item_18
fit
overall_datafit <- cfa(overall_data, data=data, meanstructure = TRUE)
Error in file(file, "r") : cannot open the connection
I'v tried to change the directory, using setwd() and to unistall and install but nothing seems to work. I really hope somebody can help me because I've no idea what to do next and I really need to finish my work.
Thank you
I also came across similar kind of error when I worked on windows, and was not able to resolve what was the problem with my code.
If you are windows below will help, as I finally found out what the problem actually was:
When you name your file "blabla.txt" then windows name it "blabla.txt.txt"... That's the same with .CSV files so windows create a file named "bla.csv.csv" if you called it "bla.csv"
So, when you create your .txt file, just rename it "data" and open it in R using data <- read.delim("C:/Users/Nyname/Desktop/data.txt")
It worked for me.
It is a bit late for answering this question, but others may also face it.
It is possible that you have loaded another package that masks the function cfa() from lavaan (e.g., package "sem" or other packages that require it such as "faoutlier"). If so, you will face this error.
If this is the case, using lavaan::cfa() instead of cfa() should resolve the problem.

send2cy doesn't work in Rstudio ~ Cyrest, Cyotoscape

When I run "send2cy" function in R studio, I got error.
# Basic setup
library(igraph)
library(RJSONIO)
library(httr)
dir <- "/currentdir/"
setwd(dir)
port.number = 1234
base.url = paste("http://localhost:", toString(port.number), "/v1", sep="")
print(base.url)
# Load list of edges as Data Frame
network.df <- read.table("./data/eco_EM+TCA.txt")
# Convert it into igraph object
network <- graph.data.frame(network.df,directed=T)
# Remove duplicate edges & loops
g.tca <- simplify(network, remove.multiple=T, remove.loops=T)
# Name it
g.tca$name = "Ecoli TCA Cycle"
# This function will be published as a part of utility package, but not ready yet.
source('./utility/cytoscape_util.R')
# Convert it into Cytosccape.js JSON
cygraph <- toCytoscape(g.tca)
send2cy(cygraph, 'default%20black', 'circular')
Error in file(con, "r") : cannot open the connection
Called from: file(con, "r")
But I didn't find error when I use "send2cy" function from terminal R (Run R from terminal just calling by "R").
Any advice is welcome.
I tested your script with local copies of the network data and utility script, and with updated file paths. The script ran fine for me in R Studio.
Given the error message you are seeing "Error in file..." I suspect the issue is with your local files and file paths... somehow in an R Studio-specific way?
FYI: an updated, consolidated and update set of R scripts for Cytoscape are available here: https://github.com/cytoscape/cytoscape-automation/tree/master/for-scripters/R. I don't think anything has significantly changed, but perhaps trying in a new context will resolve the issue you are facing.

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

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
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.
You have two separate problems.
The connection error appears because the fig/ folder does not exist. Create the folder and the save command will work. R has functions to check the existance of directories and create new ones if you would like to do it in your code.
The second problem comes from the way you save, you should use n1$save('fig/n1.html', standalone = TRUE). Here you have a similar situation.
As a side-note, I would say rCharts is not currently developed or mantained at all, so I would recommend you to use another library for your charts. In my opinion Plotly is quite nice. rCharts brought the NVD3 project to R and the chart style is in my opinion really nice. However, as far as I know both projects are stopped so I would look for a library that is still alive.
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.

In R, after installed a package,' pixmap', Error in file(file, open = "rb") : cannot open the connection

I'm learning R programming, using the book, "The Art of R Programming".
In chapter 3.2.3 Extended Example: Image Manipulation. The author Matloff tries to use a Mount Rushmore gray-scale image to illustrate that the image is stored in matrix. He used a library called pixmap. And I downloaded the package, installed it.
> library(pixmap)
> mtrush1 <- read.pnm("mtrush1.pgm")
> mtrush1
Pixmap image
Type : pixmapGrey
Size : 194x259
Resolution : 1x1
Bounding box : 0 0 259 194
> plot(mtrush1)
This is what the book has written, and I tried to run this, but got the error message,
> library(pixmap)
> mtrush1 <- read.pnm("mtrush1.pgm")
Error in file(file, open = "rb") : cannot open the connection
In addition: Warning message:
In file(file, open = "rb") :
cannot open file 'mtrush1.pgm': No such file or directory
starting httpd help server ... done
What does this mean? cannot open the connection? And also the mtrush1.pgm does not exist? How should I fix it here? Any help? Much appreciated.
Summary:
Add the argument cellres=1 to your function call and you should be fine.
Answer:
The second error you saw--Warning message: In rep(cellres, length = 2) : 'x' is NULL so the result will be NULL--is because you haven't set the cellres argument and, as a result, "cellres" assumes its default value (i.e. 'NULL'--hence the warning). For what you're working on, setting the cellres argument to 1 will do the trick (though you can pass in a two-element vector with unequal values and see just how it affects your figure by plotting the resulting object).
Note: Though it's a little late to be answering, I figure that since I had the same problem earlier today (and since Google was no help) a response was probably warranted.
This means that the file mtrush1.pgm is not in current directory. You should either setwd to the directory that contains this file, or specify the complete path in read.pnm.
For the file mtrush1.pgm, you can download it from http://heather.cs.ucdavis.edu/~matloff/
The file mtrush1.pgm and the R scripts from the book "The Art Of R Programming" can be found at this GitHub site.

Error in ls(envir = envir, all.names = private)?

The below error keeps coming up inconsistently when I try to read excel files into R using the 'XLConnect' package.
Error in ls(envir = envir, all.names = private) :
invalid 'envir' argument
I have actually run into this error while even using other packages that read excel files like package 'xlsx' and 'xlsReadWrite'. Many times restarting the R session solves this problem, which leads me to think that something else I am doing in my R session is changing the environment and not allowing me to load excel files anymore. Below is the latest example of code that is causing this error. In this case I know that the following coding sequence is causing the error to appear - but why is that happening? And how can I get past this error if I need the chron package.
library("XLConnect")
wb2 <- loadWorkbook("excel_file", create = FALSE)
library(chron)
wb2 <- loadWorkbook("excel_file", create = FALSE)
Anyone else run into this issue before? Any help on this issue is greatly appreciated!
Before reopening the workbook try removing the reference to previously opened one, so:
rm(wb2)
wb2 <- loadWorkbook("excel_file", create = FALSE)
Also, make sure that "excel_file" is not open by excel or any other program while you run the R test.
I've seen the same error come up when using XLConnect and the above seemed to help.
Had this problem a couple of times and the call stack looks like this message is generated when a "OutOfMemory" Exception is thrown.
To solve this problem I used:
options( java.parameters = "-Xmx4g" )
to increase the heap size rJava is able to use.
Debugging with options(error=utils::recover) helped a lot, because the R error messages are not very specific.

Resources