简体   繁体   中英

To write the output of this function into a pdf file in R

I have written the following function,

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))
}

This function prints the dimension of a given dataset when we run,

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

My next step is to print this result on to a properly-formatted pdf file.

How can you write the output of the function CSVDimension into a pdf file in R?

Assuming you have pandoc installed (and in your path), you can modify your function to

    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)
}

where .csv is the path to your csv file, and .pdf the path to the resulting pdf output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM