How can I print a tibble to the console along with a message? I want to write a function which outputs a tibble along with a verbose message that contains some information about the nature of that tibble.
如何在控制台上打印一个tibble和一条消息?我想写一个输出一个tibble的函数以及一个包含有关该tibble性质的信息的详细消息。
Here is a highly simplistic example of what I have in mind. In the first attempt, the function doesn't work. In the second attempt, it works but outputs some other unnecessary details.
这是我想到的一个非常简单的例子。在第一次尝试中,该功能不起作用。在第二次尝试中,它可以工作但输出一些其他不必要的细节。
# libraries needed
library(crayon)
library(tibble)
# writing the function
try.fn1 <- function() {
# prepare the tibble
x <- tibble::as.tibble(x = rnorm(1:10))
# output the associated message to the user of the function
base::message(
cat(crayon::blue("The tibble I prepared is-"), x)
)
base::message(print(x))
}
# using the function
try.fn1()
#> The tibble I prepared is-
#> Error in cat(crayon::blue("The tibble I prepared is-"), x): argument 2 (type 'list') cannot be handled by 'cat'
# another attempt
try.fn2 <- function() {
# prepare the tibble
x <- tibble::as.tibble(x = rnorm(1:10))
# output the associated message to the user of the function
base::message(
cat(crayon::blue("The tibble I prepared is-"))
)
base::message(print(x))
}
# using the function
try.fn2()
#> The tibble I prepared is-
#>
#> # A tibble: 10 x 1
#> value
#> <dbl>
#> 1 -0.00529
#> 2 0.562
#> 3 -0.511
#> 4 -0.260
#> 5 -0.232
#> 6 -1.92
#> 7 -0.698
#> 8 2.38
#> 9 1.59
#> 10 -0.585
#> c(-0.00528727617885923, 0.56168758575177, -0.510982641120654, -0.260458372988822, -0.231847890601322, -1.91514178853023, -0.697661618989503, 2.37722341810185, 1.5869372625472, -0.584576993642516)
Created on 2018-03-15 by the reprex package (v0.2.0).
由reprex包(v0.2.0)创建于2018-03-15。
2 个解决方案
#1
2
I suggest you either use or mimic the code within the unexported print
functions from tibble
. To access unexported functions, use triple-colons to access tibble:::print.tbl_df
(and .tbl
).
我建议您使用或模仿未导出的打印功能中的代码来自tibble。要访问未导出的函数,请使用三重冒号来访问tibble ::: print.tbl_df(和.tbl)。
Two things to know to know to look for these functions:
要了解这些功能,需要了解两件事:
-
Many functions in R have different behaviors (even different arguments) based on the object(s) called in them. For instance,
summary
behaves differently if given anlm
object, amatrix, etc. To see all of the different
summaryfunctions, try
methods(summary)`. (A good reference for learning more about this, called "S3", can be seen in Hadley's Advanced R online book.)R中的许多函数基于其中调用的对象具有不同的行为(甚至是不同的参数)。例如,如果给定lm对象,矩阵等,则汇总的行为会有所不同。要查看所有不同的函数,请尝试使用trymethods(summary)`。 (在Hadley的Advanced R在线书籍中可以看到一个很好的参考,可以在Hadley的Advanced R在线书籍中看到更多关于这一点的信息,称为“S3”。)
If you bring this around to this question, running
methods("print")
presents (among others):如果你把这个带到这个问题,运行方法(“打印”)呈现(以及其他):
m <- methods("print") m[ grepl("tbl|frame",m) ] # [1] "print.data.frame" "print.frame" "print.tbl_cube" "print.tbl_df" # [5] "print.tbl_lazy" "print.tbl_sql"
These don't have to be exported by their respective packages to be made available to R. In fact, I seem to find more of them unexported. The fact that
print(my_new_object)
just simply works can be a huge convenience to users new and experienced.这些不必由它们各自的包装导出以供R使用。事实上,我似乎发现它们中有更多未被出口。 print(my_new_object)只是简单的工作这一事实对于新手和有经验的用户来说是一个巨大的便利。
-
You can always find the source for this function online (such as on GitHub, like this source here), but this is not always convenient, and if you aren't careful, it could be for a version of the package other than what you have installed.
你总是可以在网上找到这个函数的源代码(例如在GitHub上,就像这里的源代码一样),但这并不总是方便的,如果你不小心,它可能是一个版本的包而不是你的已安装。
Most objects in R can be inspected (deeply or otherwise) just by typing the name on the console. This works just as well for variables as it does for functions. There are three ways to look at functions:
R中的大多数对象只需在控制台上键入名称即可(深度或其他方式)进行检查。这对于变量和函数一样有效。查看函数有三种方法:
-
After
library(pkgname)
orrequire(pkgname)
, just type the function name. This is how most people learn and interact with R.在库(pkgname)或require(pkgname)之后,只需键入函数名称。这就是大多数人学习和与R互动的方式。
-
Even before loading a package with
library
orrequire
, you can view any package's functions with double-colons, for exampledplyr::filter
. This is also useful when a function name is shared between packages (possible "masking" or collisions), and you want to be clear which one to use.甚至在加载带有库或require的包之前,您可以使用双冒号查看任何包的函数,例如dplyr :: filter。当在包之间共享函数名(可能的“屏蔽”或冲突)时,这也很有用,并且您希望清楚使用哪一个。
-
If a function is not exported, no matter if you've called
library
or not, you can only access it with the triple-colons, such astibble:::print.tbl_df
. (Actually, there are ways to view the source without the colons such as in R'sbrowser
, but that's not important here.)如果未导出函数,则无论您是否调用了库,都只能使用三重冒号访问它,例如tibble ::: print.tbl_df。 (实际上,有很多方法可以在没有冒号的情况下查看源代码,例如在R的浏览器中,但这在这里并不重要。)
-
There is a risk with using unexported functions. They are typically unexported for one of several reasons, including most importantly (a) not central to the function of the package, and (b) the author has no need or intention of maintaining that function's API (arguments, output) between releases. It is possible (and happens frequently) that an unexported function may disappear, change arguments dramatically, or change methods. Essentially an author has invested in maintaining some semblance of connsistency on exported functions, but not the unexported ones.
使用未导出的功能存在风险。它们通常由于以下几个原因之一而未被导出,其中最重要的是(a)不是包的功能的核心,(b)作者不需要或不打算在版本之间维护该函数的API(参数,输出)。有可能(经常发生)未导出的函数可能会消失,显着改变参数或更改方法。基本上,作者已经投入了对出口函数保持一些相似性,而不是未出口函数。
So by suggesting you look at or use tibble:::print.tbl_df
, there's risk: it is possible that they will change or remove that function. Having said that, the print.*
functions are often unexported because they are intended to only be used using the generic S3 version, print
. The only reason I would expect this specific function to change is if the authors changed the spelling or type of objects the package uses. And I don't see that happening any time soon.
因此,通过建议您查看或使用tibble ::: print.tbl_df,存在风险:它们可能会更改或删除该功能。话虽如此,print。*函数通常是未导出的,因为它们仅用于使用通用S3版本打印。我期望这个特定函数改变的唯一原因是作者改变了包使用的对象的拼写或类型。而且我不认为这种情况很快就会发生。
#2
0
Capture standard output, which is where cat()
and print()
send output, then concatenate and pass to message:
捕获标准输出,即cat()和print()发送输出的位置,然后连接并传递给消息:
message(paste(capture.output({
cat(crayon::blue("The tibble I prepared is-\n"))
print(x)
}), collapse = "\n"))
#1
2
I suggest you either use or mimic the code within the unexported print
functions from tibble
. To access unexported functions, use triple-colons to access tibble:::print.tbl_df
(and .tbl
).
我建议您使用或模仿未导出的打印功能中的代码来自tibble。要访问未导出的函数,请使用三重冒号来访问tibble ::: print.tbl_df(和.tbl)。
Two things to know to know to look for these functions:
要了解这些功能,需要了解两件事:
-
Many functions in R have different behaviors (even different arguments) based on the object(s) called in them. For instance,
summary
behaves differently if given anlm
object, amatrix, etc. To see all of the different
summaryfunctions, try
methods(summary)`. (A good reference for learning more about this, called "S3", can be seen in Hadley's Advanced R online book.)R中的许多函数基于其中调用的对象具有不同的行为(甚至是不同的参数)。例如,如果给定lm对象,矩阵等,则汇总的行为会有所不同。要查看所有不同的函数,请尝试使用trymethods(summary)`。 (在Hadley的Advanced R在线书籍中可以看到一个很好的参考,可以在Hadley的Advanced R在线书籍中看到更多关于这一点的信息,称为“S3”。)
If you bring this around to this question, running
methods("print")
presents (among others):如果你把这个带到这个问题,运行方法(“打印”)呈现(以及其他):
m <- methods("print") m[ grepl("tbl|frame",m) ] # [1] "print.data.frame" "print.frame" "print.tbl_cube" "print.tbl_df" # [5] "print.tbl_lazy" "print.tbl_sql"
These don't have to be exported by their respective packages to be made available to R. In fact, I seem to find more of them unexported. The fact that
print(my_new_object)
just simply works can be a huge convenience to users new and experienced.这些不必由它们各自的包装导出以供R使用。事实上,我似乎发现它们中有更多未被出口。 print(my_new_object)只是简单的工作这一事实对于新手和有经验的用户来说是一个巨大的便利。
-
You can always find the source for this function online (such as on GitHub, like this source here), but this is not always convenient, and if you aren't careful, it could be for a version of the package other than what you have installed.
你总是可以在网上找到这个函数的源代码(例如在GitHub上,就像这里的源代码一样),但这并不总是方便的,如果你不小心,它可能是一个版本的包而不是你的已安装。
Most objects in R can be inspected (deeply or otherwise) just by typing the name on the console. This works just as well for variables as it does for functions. There are three ways to look at functions:
R中的大多数对象只需在控制台上键入名称即可(深度或其他方式)进行检查。这对于变量和函数一样有效。查看函数有三种方法:
-
After
library(pkgname)
orrequire(pkgname)
, just type the function name. This is how most people learn and interact with R.在库(pkgname)或require(pkgname)之后,只需键入函数名称。这就是大多数人学习和与R互动的方式。
-
Even before loading a package with
library
orrequire
, you can view any package's functions with double-colons, for exampledplyr::filter
. This is also useful when a function name is shared between packages (possible "masking" or collisions), and you want to be clear which one to use.甚至在加载带有库或require的包之前,您可以使用双冒号查看任何包的函数,例如dplyr :: filter。当在包之间共享函数名(可能的“屏蔽”或冲突)时,这也很有用,并且您希望清楚使用哪一个。
-
If a function is not exported, no matter if you've called
library
or not, you can only access it with the triple-colons, such astibble:::print.tbl_df
. (Actually, there are ways to view the source without the colons such as in R'sbrowser
, but that's not important here.)如果未导出函数,则无论您是否调用了库,都只能使用三重冒号访问它,例如tibble ::: print.tbl_df。 (实际上,有很多方法可以在没有冒号的情况下查看源代码,例如在R的浏览器中,但这在这里并不重要。)
-
There is a risk with using unexported functions. They are typically unexported for one of several reasons, including most importantly (a) not central to the function of the package, and (b) the author has no need or intention of maintaining that function's API (arguments, output) between releases. It is possible (and happens frequently) that an unexported function may disappear, change arguments dramatically, or change methods. Essentially an author has invested in maintaining some semblance of connsistency on exported functions, but not the unexported ones.
使用未导出的功能存在风险。它们通常由于以下几个原因之一而未被导出,其中最重要的是(a)不是包的功能的核心,(b)作者不需要或不打算在版本之间维护该函数的API(参数,输出)。有可能(经常发生)未导出的函数可能会消失,显着改变参数或更改方法。基本上,作者已经投入了对出口函数保持一些相似性,而不是未出口函数。
So by suggesting you look at or use tibble:::print.tbl_df
, there's risk: it is possible that they will change or remove that function. Having said that, the print.*
functions are often unexported because they are intended to only be used using the generic S3 version, print
. The only reason I would expect this specific function to change is if the authors changed the spelling or type of objects the package uses. And I don't see that happening any time soon.
因此,通过建议您查看或使用tibble ::: print.tbl_df,存在风险:它们可能会更改或删除该功能。话虽如此,print。*函数通常是未导出的,因为它们仅用于使用通用S3版本打印。我期望这个特定函数改变的唯一原因是作者改变了包使用的对象的拼写或类型。而且我不认为这种情况很快就会发生。
#2
0
Capture standard output, which is where cat()
and print()
send output, then concatenate and pass to message:
捕获标准输出,即cat()和print()发送输出的位置,然后连接并传递给消息:
message(paste(capture.output({
cat(crayon::blue("The tibble I prepared is-\n"))
print(x)
}), collapse = "\n"))