Изменение объекта `citation` в `R`

Я пытаюсь изменить объект citation в R следующим образом.

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"

person Crops    schedule 01.08.2017    source источник


Ответы (1)


Объект citation не предназначен для изменения. Операторы подмножества ($, [, а также $<-) специфичны и не допускают простых модификаций. Это не просто так: 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},
#  }
person nicola    schedule 01.08.2017
comment
ОП может пытаться изменить цитирование для своего пакета, чтобы они могли создавать свои собственные пользовательские цитирования, основанные на стандартном (добавить одно поле?), Без необходимости самостоятельно перестраивать существующую функциональность citation(). - person Tom Saleeba; 15.02.2019