【问题标题】:To write the output of this function into a pdf file in R将此函数的输出写入 R 中的 pdf 文件
【发布时间】:2016-09-27 06:27:30
【问题描述】:

我写了如下函数,

CSVDimension <- function(.csv) {
  csv <- read.csv(.csv)
  dimValue <- dim(csv)
  print("The dimension of the dataset is:")
  headValue <- head(csv)
  print("The head of the dataset is:")
  return(list(dimValue,headValue))
}

这个函数在我们运行时打印给定数据集的维度,

> CSVDimension("path/to/file/dataset.csv")

我的下一步是将此结果打印到格式正确的 pdf 文件中。

如何将函数CSVDimension 的输出写入 R 中的 pdf 文件?

【问题讨论】:

  • 你可以使用rmarkdown,看这里:rmarkdown.rstudio.com/lesson-1.html
  • 我去看看。我究竟如何将函数的输出打印到pdf?您提供的链接将我带到 R-md 的主页,这很好。我只是想让你给我指出确切的地方。
  • @LéoLéopoldHertz준영 你本可以提出修改建议;无论如何,downvoting 对质量没有任何帮助。
  • @PragyadityaDas 完成。我的意思是要明确标题,这不是写入 pdf 文件的一般方法,而只是针对这种情况,所以 this function.

标签: r function pdf


【解决方案1】:

假设您已安装 pandoc(并在您的路径中),您可以将函数修改为

    CSVDimension <- function(.csv, .pdf) {
      csv <- read.csv(.csv)
      dimValue <- dim(csv)
      msg <- paste("The dimension of the dataset is:", paste(dimValue, collapse = " : "))
      print(msg)
      md <- sub("\\.pdf", ".md", .pdf)
      cat(msg, file = md)
      system(paste("pandoc -o", .pdf, md))
      return(dimValue)
}

其中 .csv 是 csv 文件的路径,而 .pdf 是生成的 pdf 输出的路径。

【讨论】:

  • 我应该使用 .msi 安装程序进行安装吗?我使用的是 Windows 8 笔记本电脑。
  • 错误:子错误(“\\.pdf”,“.md”,.pdf):缺少参数“.pdf”,没有默认值
  • .pdf 是一个没有默认值的参数:使用它来为 pdf 输出提供所需的路径。不知道windows,但msi可能没问题。注意:检查安装程序是否将 pandoc 可执行文件放在您的路径中,如果没有,请相应地调整以 system 开头的行。
  • 我只是在上面添加一个小修改。
  • 我对你提供的函数做了一些改动,我添加了msg1 &lt;- paste("The dimension of the dataset is:", paste(dimValue, collapse = " : ")),然后我添加了headValue &lt;- head(csv),并写了msg2 &lt;- paste("The head of the dataset is:", paste(headValue, collapse = " : ")),然后我添加了cat(msg1, msg2 , file = md)。代码运行正常,但输出的 pdf 文件看起来很奇怪。 The dimension of the dataset is: 244 : 8 The head of the dataset is: 1:4 : c(16.99, 10.34, 21.01,23.68) : c(1.01, 1.66, 3.5, 3.31) : c(1, 2, 2, 2) : c(1, 1, 1, 1) : c(3, 3, 3, 3) : c(2, 2, 2, 2) : c(2, 3, 3, 2).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2017-02-02
  • 2015-01-21
  • 2019-09-25
相关资源
最近更新 更多