Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print tables in slidify?

I want to show contents of data.frame as a table in slidify. I know how to create Markdown tables from data.frames using ascii library, but when I try to use it with slidify, instead of seeing the table in output html, I see a bunch of information about the inner structure of an ascii table.

So how do you print e.g. head(some.data.frame) in slidify?

Edit:

In fact I want to show a table of Views in CRAN Task Views, Right now I typed the table manually in Markdown:

Views | Content
--------|--------
Bayesian| Bayesian Inference
ChemPhys| Chemometrics and Computational Physics
ClinicalTrials| Clinical Trial Design, Monitoring, and Analysis

I want to create this table automatically from ctv package. I have gathered my needed information in a data.frame:

library(ctv)
list.of.views <- available.views()
X <- data.frame(View=NA,Description=NA)
for(i in 1:length(list.of.views))
{
  X[i,1] <- list.of.views[[i]]$name
  X[i,2] <- list.of.views[[i]]$topic
}
head(X)

which results in

                   View                                     Description
1              Bayesian                              Bayesian Inference
2              ChemPhys          Chemometrics and Computational Physics
3        ClinicalTrials Clinical Trial Design, Monitoring, and Analysis
4               Cluster        Cluster Analysis & Finite Mixture Models
5 DifferentialEquations                          Differential Equations
6         Distributions                       Probability Distributions

I make markdown using ascii package

library(ascii)
print(ascii(X[1:6,1:2]), type = 'pandoc')

which shows this in R terminal:

    **View**                **Description**                                  
 --- ----------------------- -------------------------------------------------
 1   Bayesian                Bayesian Inference                               
 2   ChemPhys                Chemometrics and Computational Physics           
 3   ClinicalTrials          Clinical Trial Design, Monitoring, and Analysis  
 4   Cluster                 Cluster Analysis & Finite Mixture Models         
 5   DifferentialEquations   Differential Equations                           
 6   Distributions           Probability Distributions                        
 --- ----------------------- -------------------------------------------------

Warning messages:
1: In rep(rownames, length = nrow(x)) :
  'x' is NULL so the result will be NULL
2: In rep(colnames, length = ncol(x)) :
  'x' is NULL so the result will be NULL

but when this last print line in a code chunk in my Rmd file and slidify it, I see the following content in my slide:

## <S4 Type Object>
## attr(,".xData")
## <environment: 0x03b904d8>
## attr(,"class")
## [1] "asciiTable"
## attr(,"class")attr(,"package")
## [1] "ascii"
like image 770
Majid Einian Avatar asked Jan 13 '23 09:01

Majid Einian


2 Answers

Thanks to Tyler Rinker I managed to create the table I wanted using xtable

---
```{r, results='asis'}
print(xtable(X[1:6,1:2]), type = "html")
```
like image 65
Majid Einian Avatar answered Jan 18 '23 11:01

Majid Einian


If you want markdown, I can strongly recommend my pander package that can transform R objects to different dialects of markdown formats. Quick example:

  1. Load the package

    library(pander)
    
  2. Create a markdown table on your demo data with the default multi-line format of Pandoc:

    > pander(X[1:6,1:2])
    
    -----------------------------------------------
            View                 Description       
    --------------------- -------------------------
          Bayesian           Bayesian Inference    
    
          ChemPhys            Chemometrics and     
                            Computational Physics  
    
       ClinicalTrials      Clinical Trial Design,  
                          Monitoring, and Analysis 
    
           Cluster        Cluster Analysis & Finite
                               Mixture Models      
    
    DifferentialEquations  Differential Equations  
    
        Distributions     Probability Distributions
    -----------------------------------------------
    
  3. Or in grid format:

    > pander(X[1:6,1:2], style = 'grid')
    
    +-----------------------+---------------------------+
    |         View          |        Description        |
    +=======================+===========================+
    |       Bayesian        |    Bayesian Inference     |
    +-----------------------+---------------------------+
    |       ChemPhys        |     Chemometrics and      |
    |                       |   Computational Physics   |
    +-----------------------+---------------------------+
    |    ClinicalTrials     |  Clinical Trial Design,   |
    |                       | Monitoring, and Analysis  |
    +-----------------------+---------------------------+
    |        Cluster        | Cluster Analysis & Finite |
    |                       |      Mixture Models       |
    +-----------------------+---------------------------+
    | DifferentialEquations |  Differential Equations   |
    +-----------------------+---------------------------+
    |     Distributions     | Probability Distributions |
    +-----------------------+---------------------------+
    
  4. Simple sytle without automatic line breaks:

    > pander(X[1:6,1:2], style = 'simple', split.cells = Inf)
    
            View                            Description                  
    --------------------- -----------------------------------------------
          Bayesian                      Bayesian Inference               
          ChemPhys            Chemometrics and Computational Physics     
       ClinicalTrials     Clinical Trial Design, Monitoring, and Analysis
           Cluster           Cluster Analysis & Finite Mixture Models    
    DifferentialEquations             Differential Equations             
        Distributions                Probability Distributions         
    
  5. And the PHP Extra Markdown/rmarkdown format:

    > pander(X[1:6,1:2], style = 'rmarkdown', split.cells = Inf)
    
    |         View          |                   Description                   |
    |:---------------------:|:-----------------------------------------------:|
    |       Bayesian        |               Bayesian Inference                |
    |       ChemPhys        |     Chemometrics and Computational Physics      |
    |    ClinicalTrials     | Clinical Trial Design, Monitoring, and Analysis |
    |        Cluster        |    Cluster Analysis & Finite Mixture Models     |
    | DifferentialEquations |             Differential Equations              |
    |     Distributions     |            Probability Distributions            |
    

There are bunch of global or custom options to tweak the tables (like alignment, split settings, highlight cells etc.)

like image 36
daroczig Avatar answered Jan 18 '23 11:01

daroczig