Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I break line on R's setwd() command?

Tags:

r

I try to keep all my R script lines under 80 characters. This can prove to be a challenge whenever strings are involved, but usually just breaking lines without using any special character works, like this:

plot(x, y, main = "some reeeealy long title, so long that
                   I need to break it into several lines
                   in order to satisfy my ****-retentive
                   self.")

However, some functions such as setwd() just won't let me do this. For example, running

setwd("/folder/another folder/yet another folder/
      what are you doing, hiding pr0n?/I think I've made my point/")

Returns the following error:

Error in setwd("/folder/another folder/yet another folder/\n
      what are you doing, hiding pr0n?/I think I've made my point/") : 
cannot change working directory

I've tried braking line on different points other than at the slash character, but I couldn't get it to work. The only workaround I could find was running

setwd(paste("/folder/another folder/yet another folder/",
            "what are you doing, hiding pr0n?/I think I've made my point/",
            sep = "")

Which works, but seems like a lot of mess just in order to respect some self-defined rule.

Is there a more elegant way to achieve this?

like image 941
Waldir Leoncio Avatar asked Jan 18 '23 07:01

Waldir Leoncio


1 Answers

In general, paste is the only way I can think of, however, in this special case, file.path is a better choice than paste as it provides the right separation character for your platform automatically.

file.path("/folder", "another folder", "yet another folder",
            "what are you doing, hiding pr0n?",
            "I think I've made my point")
like image 153
Aaron left Stack Overflow Avatar answered Jan 24 '23 22:01

Aaron left Stack Overflow