Say I wanted to scrutinise a specific value in my dataset, and rather than copy/rewrite the code, I could comment out a portion of the line, such as in this (theoretical) example where the commented out section would occur between /#
and #/
:
ggplot() +
geom_col(data = df/#[df$type == "after", ]#/, aes(x = date, y = count, fill = type))
Naturally this code won't work, but it is something like how I envisage it working if it were a feature.
Effectively, the code above will run the same as this:
ggplot() +
geom_col(data = df, aes(x = date, y = count, fill = type))
Is there a way to comment out part of a line in R/RStudio?
NOTE:
I know that entire lines can be commented out and I am fully aware of how to achieve this; if this is what I wanted to do, I wouldn't have asked this question. However, I am curious to know if there is such a feature as the one I pose in my question, or if such a feature is in the RStudio pipeline/backlog and would appreciate only serious answers. Thank you.
As a backdoor answer to your question, R parses everything within parentheses, and line breaks are fine in there. So in these cases I put parentheses around parts of code and can comment out certain parts. For instance, this...
ggplot() +
geom_col(
data = df#[df$type == "after",]
, aes(x = date, y = count, fill = type)
)
or this
ggplot() +
geom_col(
data = df
#[df$type == "after",]
, aes(x = date, y = count, fill = type)
)
... would work. You can even just remove the comment from the 2nd example and it will correctly infer the subsetting of df
from the line above. The dangling comma is unsightly, but you're probably just iterating if you're commenting partial lines. This works generally for parentheses (not just within function calls), so this...
a <- (
1+
# 2+
3
)
sets a to 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With