Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a table in R with ascii, html, or markdown formatting?

Tags:

r

library(xtable)
library(rattle)
set.seed(42)
obs <- sample(1:nrow(weatherAUS), 5)
vars <- 2:7
xtable(weatherAUS[obs, vars])

I get the following output with the code, why can't I get the formatted table?

% latex table generated in R 2.15.1 by xtable 1.7-1 package  
% Sat Apr  6 17:02:37 2013  
\begin{table}[ht]  
\centering  
\begin{tabular}{rlrrrrr}  
  \hline  
 & Location & MinTemp & MaxTemp & Rainfall & Evaporation & Sunshine \\   
  \hline  
60992 & Hobart & 5.60 & 13.00 & 7.60 & 1.60 & 3.10 \\   
  62476 & Launceston & 7.40 & 13.50 & 8.80 &  &  \\   
  19077 & Williamtown & 18.30 & 29.10 & 3.20 & 1.00 & 7.00 \\   
  55366 & PerthAirport & 9.80 & 21.90 & 0.00 & 3.60 & 9.80 \\   
  42784 & GoldCoast & 23.40 & 30.40 & 0.00 &  &  \\   
   \hline  
\end{tabular}  
\end{table}  
like image 634
showkey Avatar asked Apr 06 '13 09:04

showkey


People also ask

How do I display markdown in R?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

Can you insert a table into R Markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

Is R Markdown HTML?

An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below. --- output: html_document --- This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.


1 Answers

What you get returned from xtable is pretty formatted, but as it is being in LaTeX syntax, it would be worth to run through a LaTeX compiler like pdflatex. That would return a pdf document like this:

compiled pdf file

If you want a formatted table inside the R console, so a rather human-readable version of the standard print.data.frame, you might give a try to the ascii or my pander package. Examples:

  1. A basic ascii call:

    > library(ascii)
    > ascii(weatherAUS[obs, vars])
    |=================================================================================== 
    1.1+| h| Location     h| MinTemp h| MaxTemp h| Rainfall h| Evaporation h| Sunshine 
    | 60992 | Hobart       | 5.60    | 13.00   | 7.60     | 1.60        | 3.10     
    | 62476 | Launceston   | 7.40    | 13.50   | 8.80     |             |          
    | 19077 | Williamtown  | 18.30   | 29.10   | 3.20     | 1.00        | 7.00     
    | 55366 | PerthAirport | 9.80    | 21.90   | 0.00     | 3.60        | 9.80     
    | 42784 | GoldCoast    | 23.40   | 30.40   | 0.00     |             |          
    |=================================================================================== 
    
  2. Calling ascii to return the table in e.g. reStructuredText format:

    > print(ascii(weatherAUS[obs, vars]), type = "rest")
    
    +-------+--------------+---------+---------+----------+-------------+----------+
    |       | Location     | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine |
    +=======+==============+=========+=========+==========+=============+==========+
    | 60992 | Hobart       | 5.60    | 13.00   | 7.60     | 1.60        | 3.10     |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 62476 | Launceston   | 7.40    | 13.50   | 8.80     |             |          |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 19077 | Williamtown  | 18.30   | 29.10   | 3.20     | 1.00        | 7.00     |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 55366 | PerthAirport | 9.80    | 21.90   | 0.00     | 3.60        | 9.80     |
    +-------+--------------+---------+---------+----------+-------------+----------+
    | 42784 | GoldCoast    | 23.40   | 30.40   | 0.00     |             |          |
    +-------+--------------+---------+---------+----------+-------------+----------+
    
  3. Using pander to return the table in different markdown formats:

    > library(pander)
    > panderOptions('table.split.table', Inf)
    > pander(weatherAUS[obs, vars])
    
    --------------------------------------------------------------------------------
      &nbsp;      Location    MinTemp   MaxTemp   Rainfall   Evaporation   Sunshine 
    ----------- ------------ --------- --------- ---------- ------------- ----------
     **60992**     Hobart       5.6      13.0       7.6          1.6         3.1    
    
     **62476**   Launceston     7.4      13.5       8.8                             
    
     **19077**  Williamtown    18.3      29.1       3.2          1.0         7.0    
    
     **55366**  PerthAirport    9.8      21.9       0.0          3.6         9.8    
    
     **42784**   GoldCoast     23.4      30.4       0.0                             
    --------------------------------------------------------------------------------
    
  4. Or in grid format:

    > pandoc.table(weatherAUS[obs, vars], style = 'grid')
    
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    |   &nbsp;    |   Location   |  MinTemp  |  MaxTemp  |  Rainfall  |  Evaporation  |  Sunshine  |
    +=============+==============+===========+===========+============+===============+============+
    |  **60992**  |    Hobart    |    5.6    |   13.0    |    7.6     |      1.6      |    3.1     |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    |  **62476**  |  Launceston  |    7.4    |   13.5    |    8.8     |               |            |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    |  **19077**  | Williamtown  |   18.3    |   29.1    |    3.2     |      1.0      |    7.0     |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    |  **55366**  | PerthAirport |    9.8    |   21.9    |    0.0     |      3.6      |    9.8     |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    |  **42784**  |  GoldCoast   |   23.4    |   30.4    |    0.0     |               |            |
    +-------------+--------------+-----------+-----------+------------+---------------+------------+
    
  5. A more simpler format:

    > pandoc.table(weatherAUS[obs, vars], style = 'simple')
    
      &nbsp;      Location    MinTemp   MaxTemp   Rainfall   Evaporation   Sunshine 
    ----------- ------------ --------- --------- ---------- ------------- ----------
     **60992**     Hobart       5.6      13.0       7.6          1.6         3.1    
     **62476**   Launceston     7.4      13.5       8.8                             
     **19077**  Williamtown    18.3      29.1       3.2          1.0         7.0    
     **55366**  PerthAirport    9.8      21.9       0.0          3.6         9.8    
     **42784**   GoldCoast     23.4      30.4       0.0                             
    
  6. Or in PHPMarkdown Extra/piped syntax to be used with knitr:

    > pandoc.table(weatherAUS[obs, vars], style = 'rmarkdown')
    
    |   &nbsp;    |   Location   |  MinTemp  |  MaxTemp  |  Rainfall  |  Evaporation  |  Sunshine  |
    |:-----------:|:------------:|:---------:|:---------:|:----------:|:-------------:|:----------:|
    |  **60992**  |    Hobart    |    5.6    |   13.0    |    7.6     |      1.6      |    3.1     |
    |  **62476**  |  Launceston  |    7.4    |   13.5    |    8.8     |               |            |
    |  **19077**  | Williamtown  |   18.3    |   29.1    |    3.2     |      1.0      |    7.0     |
    |  **55366**  | PerthAirport |    9.8    |   21.9    |    0.0     |      3.6      |    9.8     |
    |  **42784**  |  GoldCoast   |   23.4    |   30.4    |    0.0     |               |            |
    
like image 169
daroczig Avatar answered Sep 27 '22 21:09

daroczig