How to Print a Data Frame as PDF or txt File in R (Example Code)
In this tutorial, I’ll show how to return a nicely-formatted data frame as PDF or txt file in R programming.
Example Data
data(iris) # Iris data set iris_head <- head(iris) # First six rows of iris iris_head # Print Iris to console # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 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 |
data(iris) # Iris data set iris_head <- head(iris) # First six rows of iris iris_head # Print Iris to console # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 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
Example: Return Nicely-Formatted Data Frame as PDF
install.packages("gridExtra") # Install gridExtra package library("gridExtra") # Load gridExtra |
install.packages("gridExtra") # Install gridExtra package library("gridExtra") # Load gridExtra
pdf("iris_gridExtra.pdf") # Save PDF-file grid.table(iris_head) dev.off() |
pdf("iris_gridExtra.pdf") # Save PDF-file grid.table(iris_head) dev.off()
2 Comments. Leave new
Hi
What if the dataframe has huge data.will the above code work ?
Regards
Anjaneya K
Hi Anjaneya,
It depends on how we define “huge data” 🙂
Joking aside, I think you just have to give it a try to find it out…
Regards,
Matthias