How to save file into a path containing special characters such as '&'? ('&' which is different from '&' typed in English Keyboard)

Viewed 299

I need to write out a file to a certain path that contains a special character in R. the path is something like this: C:/Users/Technology & Innovation/Webscraping files/US_data/data

It works totally fine when I access this path through python, but I cannot access the same path in R. And I cannot change this path name or remove '&' as this path is used by a lot of people. Does anyone have a good idea on how to solve it?

I found out it is '&' which has subtle difference from '&' that we usually type in through English Keyboard. May be that's the reason causing the problem?

Here is what I have tried:

write.csv(df, 'C:/Users/Technology & Innovation/Webscraping files/US_data/data/file.csv').
write.csv(df, 'C:\\Users\\Technology & Innovation\\Webscraping files\\US_data/data/file.csv')

Not matter whether I try to read or write a file, it is not working in my case.

I also tried reset the working directory path and got the error message:

Error in setwd("C:/Users/Technology & Innovation/Webscraping files/US_data/data") : cannot change working directory

1 Answers

Write it like this

C:\\Users\\Technology & Innovation\\Webscraping files\\US_data\\data

also, you can change your current directory.enter image description here

Changing your current directory will help you because you can write read.csv("filename.csv") or write.csv(name_of_file, "filename.csv") as it is without mentioning path.

If you have to write a file you have to use syntax properly.

write.csv(C:\\Users\\Technology & Innovation\\Webscraping files\\US_data\\data,"filename.csv")

Related