Frankly, I use 2-space indents, but 4-space isn't too bad. It just depends on how much clarity you like to give. For instance, some people like to include a lot of double-space line; I don't. I usually keep things 'tight' and use quad-space lines when I'm separating major blocks, double-space lines when separating processing blocks. You always indent blocks under a process
Code:
f <- function(x) {
some code
goes here
}
for (i in seq(10)) {
indent
stuff here
}
You can pretty much look at any Python code for examples since it doesn't use curly brackets to segment these logical blocks; it uses indentation to identify them (any amount of indent, as long as its uniform).
The real part that people disagree on is multi-line statements, and it really is to taste. For instance,
Code:
df <- read.delim(infile, header = FALSE, colClasses = c("factor", rep("numeric", 3), "character", "factor"),
col.names = c("blah", "boo", "foo", "bar", "joe", "bob"))
df <- read.delim(infile, header = FALSE, colClasses = c("factor", rep("numeric", 3), "character", "factor"),
col.names = c("blah", "boo", "foo", "bar", "joe", "bob")
)
df <- read.delim(infile, header = FALSE, colClasses = c("factor", rep("numeric", 3), "character", "factor"),
col.names = c("blah", "boo", "foo", "bar", "joe", "bob"))
Take your pick. I usually do one of the first two. In other cases, it makes sense to group them together
Code:
x <- function(x, y) (x - y) /
(x + y)
The problem with long multi-line statements, and why I just use normal indentation with a closing bracket, is that you can run out of space.
Code:
someVeryLongNameThatNeedsToGo <- read.delim("ThisFileIsDisgustinglyLongDude.txt", header = FALSE, na = " - ",
colClasses = c("factor", "character", "numeric", "factor", "numeric", "NULL", "numeric"),
col.names = c("bob", "joe", "chris", "frank", "bar", "foo", NA, "job")
);
While it makes it logically grouped together by parameter list, some of those parameter statements are long, too! You're pushing everything up against the right-hand side for no reason, and I don't find it adds any more clarity than
Code:
someVeryLongNameThatNeedsToGo <- read.delim("ThisFileIsDisgustinglyLongDude.txt", header = FALSE, na = " - ",
colClasses = c("factor", "character", "numeric", "factor", "numeric", "NULL", "numeric"),
col.names = c("bob", "joe", "chris", "frank", "bar", "foo", NA, "job")
);
These limitations also make more sense when you consider that your programming window should be at most 80 characters long (some go 60!). We're going way passed that in these examples! In fact, 80 characters wouldn't even fit the first parameter, requiring you, to keep to that limitation, to drop your starting line down
Code:
someVeryLongNameThatNeedsToGo <-
read.delim("ThisFileIsDisgustinglyLongDude.txt", header = FALSE, na = " - ",
colClasses = c("factor", "character", "numeric", "factor", "numeric", "NULL", "numeric"),
col.names = c("bob", "joe", "chris", "frank", "bar", "foo", NA, "job")
);