Search code examples
rshinyxtable

Display xtable in Shiny


I want to display a data table in shiny with rotated column headers. I see the option in xtable package to do this using rotate.colheaders I cannot get the table to display (without all the printed formats) in shiny, all I get is a text string displaying the latex generation (see below). I suspect that this is a fairly elementary point as I know that I am missing something - I just don't know what!

See minimised example of server.r code and output below

server.r code:

output$mytable <- renderUI({
    tab <- matrix(rep(1,6),nrow=3)
    rownames(tab) <- c('col1','col2','col3')
    M <- print(xtable(tab, rotate.colnames=TRUE))
})

Output in shiny app:

% latex table generated in R 3.1.1 by xtable 1.7-3 package % Wed Oct 22 13:31:00 2014 \begin{table}[ht] \centering \begin{tabular}{rrr} \hline & 1 & 2 \\ \hline col1 & 1.00 & 1.00 \\ col2 & 1.00 & 1.00 \\ col3 & 1.00 & 1.00 \\ \hline \end{tabular} \end{table}

Solution

  • You should use renderTable in server.r and tableOutput in ui.r

    In server.r:

    output$mytable <- renderTable({
        tab <- matrix(rep(1,6),nrow=3)
        colnames(tab) <- c('col1','col2')
        tab
        })
    

    In ui.r add:

    tags$head( tags$style( HTML('#mytable table {border-collapse:collapse; } 
                                 #mytable table th { transform: rotate(-45deg)}'))),
    column(6,tableOutput("mytable"))