【问题标题】:R `scales::comma` works yet `scales::comma()` does not - why?R `scales::comma` 工作但 `scales::comma()` 不工作 - 为什么?
【发布时间】:2019-04-14 17:29:06
【问题描述】:

当我在一个包中使用一个函数时,我通常可以将它写成function() 或不带括号的functionscales::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

【问题讨论】:

    标签: r ggplot2 scale


    【解决方案1】:

    这摘自scale_y_continuous关于输入参数labels的帮助页面:

    标签之一:

    • 无标签为 NULL
    • 由转换对象计算的默认标签的waiver()
    • 提供标签的字符向量(必须与中断长度相同)
    • 一个函数,将中断作为输入并返回标签作为输出

    在这种情况下,最后一个是重要的。 标签期望 scales::comma 是一个函数。另一方面,scales::comma() 是该函数返回但不再是函数的内容。

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 2013-07-31
      • 2016-03-18
      • 1970-01-01
      • 2017-11-02
      • 2013-08-27
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多