0

I am trying to stitch an R Markdown and I am running in to the following error message:

Error in file(file, "rt") : cannot open the connection
3.file(file, "rt")
2.read.table(file = file, header = header, sep = sep, quote = quote, dec = dec, fill = fill, comment.char = comment.char, ...)
1.read.csv("~/Desktop/GOVT 300/leaders.csv")

I have tried using:

{r}
setwd("~/Documents/GOVT 300")
leaders.csv <- read.csv("~/Desktop/GOVT 300/leaders.csv")

but this has not worked to my avail. How can I solve this issue?

Update: Thank you for the input, I was able to fix that issue but have bumped into another one. I am receiving this error:

Line 43: Error in ifelse(leader$result == level[1] | leaders$result == level[2] | : object 'level' not found Calls: <Anonymous>... withCallingHandlers -> withVisible -> eval -> eval -> ifelse Execution halted

My code on line 43 in as follows:

leaders$result <- as.factor(leaders$result)

The affected chunk is:

```{r}

leaders$result <- as.factor(leaders$result)

leaders$success <- ifelse(leaders$result == level[1] | leaders$result == level[2] | leaders$result==level[3] | leaders$result==level[4], 1,0)

levels(leaders$result)

mean(leaders$success)

```
Fábio
  • 771
  • 2
  • 14
  • 25
  • 1
    You could try passing the full path explicitly to `read.csv`, without messing about with `setwd` – user20650 Sep 21 '18 at 23:31
  • This could be one of several things, including: file permissions, file locked by another process (e.g., Excel), or as others have suggested, location or spelling of the filename. – r2evans Sep 21 '18 at 23:37
  • 1
    You can always test with `file.exists("~/Desktop/GOVT 300/leaders.csv")` first to verify it's there and rule out a problem with spelling and location. – r2evans Sep 21 '18 at 23:38
  • I fixed with: ```setwd("~/Documents/GOVT 300/.") leaders<-read.csv("~/Documents/GOVT 300/leaders.csv")``` Now, I have run into another error: ```Line 43: Error in ifelse(leader$result == level[1] | leaders$result == level[2] | : object 'level' not found Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> ifelse Execution halted``` – Ian Chamberlin Sep 21 '18 at 23:54
  • 1
    @IanChamberlin ; well that seems to be saying that it can't find the object level - but we dont know as we can't see the actual code. I'd suggest that you write your code in a normal R file and edit it when you get an error. When your script is error free create an rmarkdown file. – user20650 Sep 22 '18 at 00:04
  • There is still no `level` in your example - but taking a punt, do you mean `ifelse(leaders$result == levels(leaders$result)[1] |` etc? If so, you may be able to write it as `ifelse(leaders$result %in% levels(leaders$result)[1:4], 1, 0)`, or more succinctly `as.integer(leaders$result %in% levels(leaders$result)[1:4])` – user20650 Sep 22 '18 at 00:18
  • Please don't change gthe question/add questions after you post ... instead start a new question. – Elin Sep 22 '18 at 03:44

1 Answers1

0

If you have two GOVT 300 folder, one in Documents and other in Desktop you should try what @user20650 suggests. If it is not the case, be sure where the leaders.csv file is. In R

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

usually means path problems. Check here for more details.

Fábio
  • 771
  • 2
  • 14
  • 25
  • I did that, I now have the working directory set and have run into another error. The error being: Line 43: Error in ifelse(leader$result == level[1] | leaders$result == level[2] | : object 'level' not found Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> ifelse Execution halted – Ian Chamberlin Sep 21 '18 at 23:53
  • Ok, then I recommend you to update your original question with this new error. – Fábio Sep 21 '18 at 23:55
  • what's the output of `head(leaders.csv)`? and what is `level` ? a vector, list... – Fábio Sep 22 '18 at 00:03
  • Fabio, I recently updated the question. I am relatively new to R, thank you for the input. Please see my updates, i hope they help you to help me. – Ian Chamberlin Sep 22 '18 at 00:07
  • No problem, but I still need to know what is the `level` object as well as see how R opened your file with the output of `head(leaders.csv)`. – Fábio Sep 22 '18 at 00:14