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


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

i following output code, why can't formatted table?

% latex table generated in r 2.15.1 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}   

what returned xtable pretty formatted, being in latex syntax, worth run through latex compiler pdflatex. return pdf document this:

compiled pdf file

if want formatted table inside r console, rather human-readable version of standard print.data.frame, might give try ascii or 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 return 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 return 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 used 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     |               |            | 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -