0

I have finished my analysis. And my final output is matrix even with row and colomn names. Now I just need to make a nice table with lines and output it as (may be a pdf) or (may be like an editable word file).

I did my own research and saw that there are many packages like xtable and knitr. But when i install those packages and execute a code. I just get bunch of codes as Output.

mat <- matrix(c(1:91), ncol = 7, byrow=F)
rownames(mat) <- c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10", 
"Y11", "Y12", "Total water")
colnames(mat) <- c("Runoff", "RM", "DEEP Percolation", "ET", 
  "Lateralflow", "Change in SW", "Change in FW")
   mat
library(knitr)
kable(mat)
library(xtable)

xtable(mat, type="latex")

When i use kable, i get a table but i dont get the formatted table (like you can make in words). When i use xtable, i just get bunch of codes as output.

I feel like i am missing something very simple here. Maybe i need to add Latex to my R? so that when i run the code i will get table instead of code as an output.

I would appreciate if someone could nudge me in the right direction. Or any simple solution would be helpful too.

Samrat
  • 79
  • 1
  • 10
  • does it work if you use `format = "html"` ? – heck1 Aug 28 '19 at 06:44
  • @heck1 No even if i do format = "html" I still get bunch of codes as outputs – Samrat Aug 28 '19 at 06:54
  • 1
    Then there is probably something wrong with your matrix. Can you provide a reproducible example? Consider how to make a good example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and see how you can change your question accordingly. – heck1 Aug 28 '19 at 06:55
  • `knitr::kable` is mainly used within R Markdown files. This makes it easy to have code and prose for you analysis in one reproducible file. However, you could also save the LaTeX code produced by `knitr::kable` in a variable, write that to a file and include it in a `tex` file. – Ralf Stubner Aug 28 '19 at 08:21
  • @heck1 I just edited the question to make a reproducable code. The data is simple but my final output is same just the data is different. Now i want to get a table (like i can get a nice image for plots) as an output. I am not sure how to do that. – Samrat Aug 28 '19 at 17:17
  • @RalfStubner I have never used Latex before. I think that is why i am struggling at this. I just need a table (like i can make in word). I edited the code to make a reproducible example. Please let me know if i can output a pdf of a table like i can do for plots. – Samrat Aug 28 '19 at 17:19
  • @Samrat the example works perfectly fine for me - you get the latex output you can just copy to your latex file. – heck1 Aug 29 '19 at 06:41
  • @heck1 thank you for your help! – Samrat Aug 29 '19 at 22:32
  • Possible duplicate of [Create a PDF table](https://stackoverflow.com/questions/3881278/create-a-pdf-table) – dash2 Sep 05 '19 at 20:49

1 Answers1

2

I do not know a simple way to format only a matrix or data.frame into a table as PDF or similar, like you can do for plots. However, You can easily use R Markdown to create the entire report including tables and plots with embedded R code. This way you don't have to bother with exporting plots or tables in the first place. The following example extends the default Rmd file created by RStudio with your code:

---
title: "My Analysis"
author: "Samrat"
date: "`r Sys.Date()`"
output:
  html_document: default
  pdf_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

## Including Tables

If you have tabular data as `matrix` or `data.frame` you can output it in a nicely formatted way. First the "analysis" that produces the data:

```{r analysis}
mat <- matrix(c(1:91), ncol = 7, byrow=F)
rownames(mat) <- c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10", 
"Y11", "Y12", "Total water")
colnames(mat) <- c("Runoff", "RM", "DEEP Percolation", "ET", 
  "Lateralflow", "Change in SW", "Change in FW")
```

Now we print the data as a table:

```{r table, echo=FALSE}
library(knitr)
kable(mat)
```

You can convert this file to different output formats (HTML, PDF, DOCX) via the "Knit" button in RStudio or the rmarkdown::render function. Prerequisites:

  • rmarkdown package from CRAN
  • pandoc program, which comes with RStudio
  • For PDF output you will need a TeX system. Easiest way in many cases is tinytex::install_tinytex() from R.
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75