用于评论R中多线功能的协议

时间:2017-01-03 04:44:11

标签: r function commenting

我知道如果我们想对R中的一行代码发表评论,我们将使用以下格式

my.object <- function(A = arg(1))  #Comment

但是,我编写了一些分为两行的代码。一个例子是

my.object <- function(A = arg(1), B = arg(2),
                      C = arg(3), D = arg(4))

我的问题是:如果我想评论第二个功能,我会把它放在哪里?它应该在第1行之后是两个空格,在第2行之后是两个空格,还是应该把它放在上面?

3 个答案:

答案 0 :(得分:2)

您可以在任何一个地方发表评论(而且您实际上并不需要这些空格。解析器会忽略从散列到下一个换行的所有内容。它们只适用于我们人类:

my.object <- function(A = arg(1), B = arg(2),  # First two args
                      C = arg(3), D = arg(4))  # Second two and closing paren  
 {}

在编译或获取函数时,可能会删除注释。 我使用相同的行注释以及&#34; prefacatory comments&#34;在早期的线上。取决于我想说多少。 (我通常不会说够。)

答案 1 :(得分:1)

另一种可能性是 -

"This function takes 4 args
and does something"
my.object <- function(A = arg(1), B = arg(2),
                      C = arg(3), D = arg(4))

或者 -

if(FALSE){
"We have a function below that takes 4 arguments
and does something"
}
my.object <- function(A = arg(1), B = arg(2),
                      C = arg(3), D = arg(4))

评论是关于使您的代码记录良好并偶尔解释一些怪癖。在RStudio中, Ctrl + Shift + c 有助于使用默认空格对所选行进行评论。

答案 2 :(得分:0)

你必须使用双引号来评论多行代码:

.header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: #cc5350;
    color:#fff;
    z-index: 1000;
    height: 200px;
    overflow: hidden;
    -webkit-transition: height 0.3s;
    -moz-transition: height 0.3s;
    transition: height 0.3s;
    text-align:center;
    line-height:160px;

}
.header.shrink {
    height: 100px;
    line-height:80px;
}
.header h1
{
    font-size:30px;
    font-weight:normal;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
}

.header.shrink h1
{
    font-size:24px;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
}
.content
{
height:2000px;
 /*just to get the page to scroll*/
}