如何将元数据添加到小标题 [英] How to add metadata to a tibble

查看:77
本文介绍了如何将元数据添加到小标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将元数据添加到小标题?

How does one add metadata to a tibble?

我想用一个句子描述我的每个变量名,这样我就可以打印出带有相关元数据的小标题,如果将它交给以前没有看过数据的人,他们可能会有所理解.

I would like a sentence describing each of my variable names such that I could print out the tibble with the associated metadata and if I handed it to someone who hadn't seen the data before, they could make some sense of it.

as_tibble(iris)

# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
# ... with 140 more rows

# Sepal.length. Measured from sepal attachment to stem
# Sepal.width. Measured at the widest point
# Petal.length. Measured from petal attachment to stem
# Petal.width. Measured at widest point
# Species. Nomenclature based on Integrated Taxonomic Information System (ITIS), January 2018.

谢谢!

推荐答案

这似乎很棘手.原则上,@ hrbrmstr的注释是处理方法(即使用?comment?attr将属性添加到任何对象),但是默认情况下不会打印出这些属性 .属性似乎是为 atomic 对象自动打印的:

This seems tricky. In principle @hrbrmstr's comment is the way to go (i.e. use ?comment or ?attr to add attributes to any object), but these attributes will not be printed out by default. Attributes seem to be printed automatically for atomic objects:

> z <- 1:6
> attr(z,"hello") <- "goodbye"
> z
[1] 1 2 3 4 5 6
attr(,"hello")
[1] "goodbye"

...但可惜的是,对于数据帧或小标题:

... but not, alas, for data frames or tibbles:

dd <- tibble::data_frame(x=1:4,y=2:5)
> attr(dd,"metadata") <- c("some stuff","some more stuff")
> dd
# A tibble: 4 x 2
      x     y
  <int> <int>
1     1     2
2     2     3
3     3     4
4     4     5

您可以使用其自己的S3类包装该对象,以打印出以下内容:

You can wrap the object with its own S3 class to get this stuff printed:

class(dd) <- c("my_tbl",class(dd))
> print.my_tbl <- function(x) {
+    NextMethod(x)
+    print(attr(x,"metadata"))
+    invisible(x)
+ }
> dd
# A tibble: 4 x 2
      x     y
  <int> <int>
1     1     2
2     2     3
3     3     4
4     4     5
[1] "some stuff"      "some more stuff"

例如,您可以使打印更加精美或漂亮

You could make the printing more elaborate or pretty, e.g.

cat("\nMETADATA:\n")
cat(sprintf("# %s",attr(x,"metadata")),sep="\n")

如果其他用户未定义print.my_tbl,则不会发生任何不良情况(打印方法将退回到小标题的打印方法),但是仅当元数据具有您的print.my_tbl定义时才会打印元数据.

Nothing bad will happen if the other user hasn't defined print.my_tbl (the print method will fall back to the print method for tibbles), but the metadata will only be printed if they have your print.my_tbl definition ...

这篇关于如何将元数据添加到小标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆