0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

R で複数行のコメント(意外な実装)

Posted at

文字列を定義するのに普通 " を使う人は ' を,その逆の人は " を使って(エスケープするのはやぶさかではないという人・場合はどちらでも),以下のようにします。
実行文だけを含むだけでなく,なんでも書けます。

まずは,関数定義の中での話。

b = function(x) {
'
	これはコメントです
    エラーも出ません
	以下も全部実行されません
	print("foo")
	print(x)
'
	return(x + 1)
}
> b(8)
[1] 9

トップレベルだと文字列がエコーバックされてしまうので,適当な変数に代入してごまかします。
変数名としては意味のあるものを使ってもいいですけど, [.] なんか目立たなくていいかも。

x = 1
.='
	これはコメントです
	以下も全部実行されません
	print("foo")
	print(x)
	x = x + 100000
    エコーバックもされません
'
x = x + 1
print(x)
# [1] 2 が出力される
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?