在 RStudio 中,如何用字符串在一行中打印出积分结果?

In RStudio, how can I print out the result of integration in one single line with a string?

miu <- integrate(func, lower=0, upper=inf)
print(miu)

我在 RStudio 中通过积分计算平均值,如果我直接打印输出,它是一个句子,"10.60002 with absolute error < 0.00014"。如何在 "The true mean is" 之后打印句子? cat() 无法处理它,因为 miu 本身实际上是一个列表。如果我执行以下操作,这两个部分将在 pdf 输出中分成两个块。

print("The true mean is:")
print(miu)

像这样:

print(paste("The true mean is:", miu$value, "with absolute error <", miu$abs.error))

您可以通过运行 str(miu)查看miu的结构,它会显示列表中每个对象的名称。

此外,您可以通过 运行 getAnywhere(print.integrate) 查看打印函数对 integrate 的输出做了什么,这将显示函数代码。

我们可以使用glue来做到这一点

glue::glue("The true mean is: {round(miu$value, 3)} with absolute error < {round(min$abs.error, 3)}")