Is there a way to save a file to a specific folder inside your working directory when exporting from R?

For example, if I want to save the .csv file below in a folder called "output" , is there an argument I am missing from the documentation of this function OR better function to use? write.csv function - RDocumentation

write.csv(data, file='data.csv')

Just pass the desired file path to the file argument.

write.csv(data, file='output/data.csv')

I like to use file.path() as it means I don't have to remember which way the slashes go...

write.csv(data, file=file.path('output','data.csv'))
1 Like

Thanks! what if the folder (output) is inside another folder (data) inside the working directory?

Thanks! Do you know the equivalent for if the folder (output) is inside another folder (data) inside the working directory?

Is this what you mean?

write.csv(data, file='data/output/data.csv')
1 Like

Yes! This format also worked for the here function--thank you very much.

xx_path <- here("data/input","xx_count_relfreq.csv")

Just for you to be aware, this is not specific to R or coding, it seems you are having problems understanding how to define a file path, this is a general computing concept.

That makes sense--thanks. Learnt R for statistics, and no background in computing so this is helpful. Please feel free to link any other resources.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.