'R: Pathfile created in variable is not readable although visually exactly the same as typed file path which works

I would like to create file paths to over 70 files. I am reading in csv-tables and am exporting png with the same name, so the file extension should be changeable.

I tried to create the path in different ways, as you can see in my code example, but in either way I get an error message:

Error: "In file(file, "rt") : No such file or directory"

When I type the file directory in directly the file is found. Visually I can see no difference between the created file path and the typed file path. But when I compare them, there is a difference. Here is my code for better explanation:

These are one example ordner and one example filename

ordner <- "2017-11-05–2017-11-06_fog_far_inland"
filename <- "2017-11-05–2017-11-07_fog_far_inland_Station_11_CaletaLoa

type_1 <- ".csv"
type_2 <- ".png"

These are the two ways I tried to create my file path

csvname = paste(getwd(), ordner, filename, type_1, sep = "")
csvname <- file.path(getwd(), ordner, paste(filename,".csv",sep=""))

The following throws the error message:

Error: "In file(file, "rt"): cant find file 'workingDirectory/2017-11-05–2017-11-06_fog_far_inland/2017-11-05–2017-11-07_fog_far_inland_Station_11_CaletaLoa.csv: No such file or directory"

datatable <- read.csv(csvname, header = T)

Copying the path from the error message in a second variable gives the same error message

csvname2 <- "workingDirectory/2017-11-05–2017-11-06_fog_far_inland/2017-11-05–2017-11-07_fog_far_inland_Station_11_CaletaLoa.csv"
datatable <- read.csv(csvname2, header = T)

Typing exactly the same in the variable manually works:

csvname3 <- "workingDirectory/2017-11-05–2017-11-06_fog_far_inland/2017-11-05–2017-11-07_fog_far_inland_Station_11_CaletaLoa.csv"
datatable <- read.csv(csvname3, header = T)

As there is visually no difference between the file paths I tested them in an if-statement comparing the created path file variable with the variable with the typed path file it saids "false".

if (csvname == csvname3){
  print("true")
} else {
  print("false")
}

Any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source