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

我得到以下带有代码的输出,为什么我不能得到格式化的表格?

% 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}  
4

1 回答 1

21

你得到的返回xtable是非常格式化的,但是因为它是 LaTeX 语法,所以值得通过一个 LaTeX 编译器来运行,比如pdflatex. 这将返回一个像这样的pdf文档:

编译的pdf文件

如果您想要 R 控制台中的格式化表格,即标准的可读版本print.data.frame,您可以尝试使用 the asciior my panderpackage。例子:

  1. 一个基本的ascii调用:

    > 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. 调用ascii以例如 reStructuredText 格式返回表格:

    > 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. 用于以pander不同的降价格式返回表格:

    > 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. grid格式:

    > 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. 更多simpler 格式:

    > 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. 或者在 PHPMarkdown Extra/管道语法中使用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     |               |            |
    
于 2013-04-06T10:06:53.340 回答