如何指定运算符%+%的库,例如pkg ::或pkg :::

时间:2018-11-21 10:20:15

标签: r ggplot2

运算符%+%在两个软件包中使用,即ggplot2crayou

%+%中的功能crayon有时不起作用,我认为%+%中考虑了运算符ggplot2。我尝试通过crayon::%+%进行识别,但这不起作用。

cat(green(
 'I am a green line ' %+%
 blue$underline$bold('with a blue substring') %+%
 ' that becomes green again!'
))

----基于评论:我认为由于我的错过理解而发生以下错误--------

我尝试使用crayon::(backtick)%+%(backtick)而不是%+%来运行以下代码

cat(green(
    'I am a green line ' crayon::`%+%`
        blue$underline$bold('with a blue substring') crayon::`%+%`
        ' that becomes green again!'
))

但是发生以下错误:

  > cat(green(
+     'I am a green line ' crayon::`%+%`
Error: unexpected symbol in:
"cat(green(
    'I am a green line ' crayon"
>         blue$underline$bold('with a blue substring') crayon::`%+%`
Error: unexpected symbol in "        blue$underline$bold('with a blue substring') crayon"
>         ' that becomes green again!'
[1] " that becomes green again!"
> ))
Error: unexpected ')' in ")"

1 个答案:

答案 0 :(得分:1)

一个选择是不library(crayon)requireNamespace(crayon)并完全限定所有crayon引用:

cat(crayon::green(
  'I am a green line ' %c+%
    crayon::blue$underline$bold('with a blue substring') crayon::`%+%`
    ' that becomes green again!'
))

但这很烦人。因此,您还可以手动导入并重命名您需要的内容,而无需单独的非核心软件包

`%c+%` <- getFromNamespace("%+%", "crayon") # changed to not break other things
green <- getFromNamespace("green", "crayon")
blue <- getFromNamespace("blue", "crayon")

cat(green(
  'I am a green line ' %c+%
    blue$underline$bold('with a blue substring') %c+%
    ' that becomes green again!'
))