Home > OS >  Error in file(file, "rt") : cannot open the connection in r even after setting right path
Error in file(file, "rt") : cannot open the connection in r even after setting right path

Time:12-19

loading required library

install.packages("tidyverse")
install.packages("dplyr")
library("tidyverse")
library("dplyr")

vector for file names

files<- list.files(path = "C:/Users/91932/Downloads/archive (2)/Fitabase Data 4.12.16-5.12.16",pattern =".csv")

concat directory to file names

files<-str_c("C:/Users/91932/Downloads/archive (2)/Fitabase Data 4.12.16-5.12.16",files)

applying function to each element of vector


> map_df(.x = files, .f = read.csv,)

in above function map_df() i am getting an error in file (file,"rt"), even after using setwd() to set path and checking using getwd() paths seems to be correct for .csv file .

why this error occurred? how to avoid such errors?

CodePudding user response:

The error is because the path is wrong: you forgot the trailing slash in the path prefix in str_c. However, rather than using str_c, you can instruct list.files to give you full paths from the get-go:

files <- list.files(
    path = "C:/Users/91932/Downloads/archive (2)/Fitabase Data 4.12.16-5.12.16",
    pattern = ".csv",
    full.names = TRUE
)
  •  Tags:  
  • r
  • Related