【问题标题】:Modifying a `citation` object in `R`在“R”中修改“引文”对象
【发布时间】:2017-08-01 07:58:19
【问题描述】:

我正在尝试修改R 中的citation 对象,如下所示

cit <- citation("ggplot2")

cit$textVersion
#[1] "H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2009."

cit$textVersion <- "Hadley Wickham and Winston Chang (2016). ggplot2: Create Elegant Data Visualisations Using
  the Grammar of Graphics. R package version 2.2.1."

但是没有变化。

cit$textVersion
#[1] "H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2009."

如果我们检查cit 的结构,现在有两个textVersion 属性。如何单独修改原来的textVersion

str(cit)
List of 1
 $ :Class 'bibentry'  hidden list of 1
  ..$ :List of 6
  .. ..$ author   :Class 'person'  hidden list of 1
  .. .. ..$ :List of 5
  .. .. .. ..$ given  : chr "Hadley"
  .. .. .. ..$ family : chr "Wickham"
  .. .. .. ..$ role   : NULL
  .. .. .. ..$ email  : NULL
  .. .. .. ..$ comment: NULL
  .. ..$ title    : chr "ggplot2: Elegant Graphics for Data Analysis"
  .. ..$ publisher: chr "Springer-Verlag New York"
  .. ..$ year     : chr "2009"
  .. ..$ isbn     : chr "978-0-387-98140-6"
  .. ..$ url      : chr "http://ggplot2.org"
  .. ..- attr(*, "bibtype")= chr "Book"
  .. ..- attr(*, "textVersion")= chr "H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2009."
  .. ..- attr(*, "textversion")= chr "Hadley Wickham and Winston Chang (2016). ggplot2: Create Elegant Data Visualisations Using\n  the Grammar of Gr"| __truncated__
 - attr(*, "mheader")= chr "To cite ggplot2 in publications, please use:"
 - attr(*, "class")= chr "bibentry"

【问题讨论】:

    标签: r attributes bibtex citations


    【解决方案1】:

    citation 对象不会被修改。子集运算符($[,还有$&lt;-)是特定的,不允许轻易修改。这是有原因的:citation 信息被写入包的特定文件中,并且不会被修改。 我不知道你为什么要尝试它,但如果你真的需要,这里有一个小技巧。

    #store the class of the object, so can be reassigned later
    oc<-class(cit)
    #unclass the object to be free to modify
    tmp<-unclass(cit)
    #assign the new "textVersion"
    attr(tmp[[1]],"textVersion")<-"Hadley Wickham and Winston Chang (2016). ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. R package version 2.2.1."
    #assign the class back
    class(tmp)<-oc
    tmp
    #To cite ggplot2 in publications, please use:
    #
    #  Hadley Wickham and Winston Chang (2016). ggplot2: Create Elegant Data
    #  Visualisations Using the Grammar of Graphics. R package version
    #  2.2.1.
    #
    #A BibTeX entry for LaTeX users is
    #
    #  @Book{,
    #    author = {Hadley Wickham},
    #    title = {ggplot2: Elegant Graphics for Data Analysis},
    #    publisher = {Springer-Verlag New York},
    #    year = {2009},
    #    isbn = {978-0-387-98140-6},
    #    url = {http://ggplot2.org},
    #  }
    

    【讨论】:

    • OP 可能正在尝试修改他们包的引用,以便他们可以生成自己的自定义引用,该引用基于开箱即用的引用(添加一个字段?)而无需重建现有的 citation() 功能本身。
    猜你喜欢
    • 2021-09-18
    • 2017-06-28
    • 1970-01-01
    • 2018-06-29
    • 2014-12-19
    • 2012-01-29
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多