R `scales::comma` 工作但 `scales::comma()` 不工作 - 为什么?
R `scales::comma` works yet `scales::comma()` does not - why?
当我在包中使用一个函数时,我通常可以将其写成 function()
或不带括号的 function
形式。 scales::comma
似乎不是这种情况。为什么下面第7行可以,第8行不行
library(tidyverse)
mtcars %>%
count(cyl) %>%
ungroup() %>%
mutate(n = n * 1000) %>%
ggplot(aes(cyl, n)) +
scale_y_continuous(labels = scales::comma) + # line 7
# scale_y_continuous(labels = scales::comma()) + # line 8
geom_line()
第 8 行错误
Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix, :
argument "x" is missing, with no default
这取自 scale_y_continuous
关于输入参数 labels
:
的帮助页面
标签其中之一:
- NULL 表示没有标签
- 转换对象计算的默认标签的 waiver()
- 给出标签的字符向量(必须与中断长度相同)
- 一个函数,它将休息作为输入,returns标签作为输出
在本例中,最后一个是重要的。
Labels 期望 scales::comma
是一个函数。另一方面,scales::comma()
是函数 returns 但不再是函数。
当我在包中使用一个函数时,我通常可以将其写成 function()
或不带括号的 function
形式。 scales::comma
似乎不是这种情况。为什么下面第7行可以,第8行不行
library(tidyverse)
mtcars %>%
count(cyl) %>%
ungroup() %>%
mutate(n = n * 1000) %>%
ggplot(aes(cyl, n)) +
scale_y_continuous(labels = scales::comma) + # line 7
# scale_y_continuous(labels = scales::comma()) + # line 8
geom_line()
第 8 行错误
Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix, :
argument "x" is missing, with no default
这取自 scale_y_continuous
关于输入参数 labels
:
标签其中之一:
- NULL 表示没有标签
- 转换对象计算的默认标签的 waiver()
- 给出标签的字符向量(必须与中断长度相同)
- 一个函数,它将休息作为输入,returns标签作为输出
在本例中,最后一个是重要的。
Labels 期望 scales::comma
是一个函数。另一方面,scales::comma()
是函数 returns 但不再是函数。