0

I am trying to loop trough a directory its sub directories to print all images.

My setup is RStudio 1.4 on Ubuntu 20.10.

This is my RMarkup R chunk:

```{r, echo=FALSE}
path = "/home/bruker/Pictures"
out.file<-""
file.names <- dir(path)
for(i in 1:length(file.names)){

  file <- read.table(file.names[i],header=TRUE, sep=";", stringsAsFactors=FALSE)

}
```

It gives the following error:

Error in file(file, "rt") : cannot open the connection Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> read.table -> file Exectuion halted

The directory exists. I get this in the terminal:

bruker@ubuntu:~$ tree /home/bruker/Pictures
locales-launch: Data of en_US locale not found, generating, please wait...
/home/bruker/Pictures
├── dog.jpeg
└── samsung.png

0 directories, 2 files

Also this code prints the files, so it the "file" variable assignemtns that doesnt work:

```{r, echo=FALSE}
path = "/home/bruker/Pictures"
out.file<-""
file.names <- dir(path)
dir(path)
```

Output:

## [1] "dog.jpeg"    "samsung.png"
Europa
  • 974
  • 12
  • 40
  • Can you print the path before importing? Can you make sure it exists? Do you have read permission on the file? Can you provide a `tree` of the folder you're trying to import? – Roman Luštrik Feb 13 '21 at 18:38
  • I've added the output of tree command. – Europa Feb 13 '21 at 18:44
  • 1
    Use `dir(full.names=TRUE)`. Otherwise, it returns the filename without the directory prepended to them, and nothing should try to infer location of directory-less filenames that are not in the current working directory. – r2evans Feb 13 '21 at 22:22
  • @r2evans feel free to post that as an answer, as it is one. – Roman Luštrik Feb 14 '21 at 18:26
  • That's a little funny to me, @RomanLuštrik ... in several instances, I close a question as a dupe (or somebody closes it) and then I post a short answer with the specifics of *that* question ... and I receive downvotes and/or comments discouraging adding answers to dupe questions. I don't care about the small comment here or there or the downvotes, and I agree with the premise of keeping original Q/A as the dominant, so I laugh a little that I'm not the only one who thinks it can still be okay to do that. (I don't think this one benefits from personalization, so I'll pass this time. Thanks!) – r2evans Feb 14 '21 at 18:38

0 Answers0