粘贴函数以在 r 中添加反斜杠

Paste function to add backslash in r

这可能是一个非常特殊的要求,但我想在 R 中每个文件之前的 file.path 字符串末尾粘贴一个反斜杠 (\)。出于某种原因,R 不喜欢该函数: sep="\" 我不知道为什么...任何帮助将不胜感激

# Image files 
files <- c("image1.bmp", "image2.bmp", "image3.bmp", "image4.bmp", "image5.bmp")

# Pasting file paths and file names 
file.paths <- paste("C:/Users/John/Desktop/images/", files, sep="\")

# Desired output 
C:/Users/John/Desktop/images\image1.bmp
C:/Users/John/Desktop/images\image2.bmp
C:/Users/John/Desktop/images\image3.bmp
C:/Users/John/Desktop/images\image4.bmp
C:/Users/John/Desktop/images\image5.bmp

\ 有效,但它在控制台输出中显示为双反斜杠,但是您不能使用 messagecat 来查看它的自然外观是正确的。

file.paths <- paste0("C:/Users/John/Desktop/images\" , files)

message(file.paths[1])
cat(file.paths[1])

fileConn<-file("outputtest.txt")
writeLines(file.paths, fileConn)
close(fileConn)

保存在文本文件输出中: