Tables in R

How to make tables in R with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Basic Table

library(plotly)

fig <- plot_ly(
  type = 'table',
  columnwidth = c(100, 100),
  columnorder = c(0, 1),
  header = list(
    values = c("Cut","Price"),
    align = c("center", "center"),
    line = list(width = 1, color = 'black'),
    fill = list(color = c("grey", "grey")),
    font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(diamonds$cut, diamonds$price),
    align = c("center", "center"),
    line = list(color = "black", width = 1),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig
5424233313254544522323333343333333322235Cut326326327334335336336337337338339340342344345345348351351351351352353353353354355357357357402402402402402402402402403403Price

Styled Table

library(plotly)

fig <- plot_ly(
  type = 'table',
  header = list(
    values = c('<b>EXPENSES</b>', '<b>Q1</b>','<b>Q2</b>','<b>Q3</b>','<b>Q4</b>'),
    line = list(color = '#506784'),
    fill = list(color = '#119DFF'),
    align = c('left','center'),
    font = list(color = 'white', size = 12)
  ),
  cells = list(
    values = rbind(
      c('Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'),
      c(1200000, 20000, 80000, 2000, 1212000),
      c(1300000, 20000, 70000, 2000, 1392000),
      c(1300000, 20000, 120000, 2000, 1442000),
      c(1400000, 20000, 90000, 2000, 1412000)),
    line = list(color = '#506784'),
    fill = list(color = c('#25FEFD', 'white')),
    align = c('left', 'center'),
    font = list(color = c('#506784'), size = 12)
    ))

fig
SalariesOfficeMerchandiseLegalTOTALEXPENSES1200000200008000020001212000Q11300000200007000020001392000Q213000002000012000020001442000Q31400000200009000020001412000Q4

Table From a Dataframe

library(plotly)

fig <- plot_ly(
  type = 'table',
  header = list(
    values = c("<b>Cars</b>", names(mtcars)),
  align = c('left', rep('center', ncol(mtcars))),
  line = list(width = 1, color = 'black'),
  fill = list(color = 'rgb(235, 100, 230)'),
  font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(
      rownames(mtcars), 
      t(as.matrix(unname(mtcars)))
    ),
    align = c('left', rep('center', ncol(mtcars))),
    line = list(color = "black", width = 1),
    fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

fig
MazdaRX4MazdaRX4WagDatsun710Hornet4DriveHornetSportaboutValiantDuster360Merc240DMerc230Merc280Merc280CMerc450SEMerc450SLMerc450SLCCadillacFleetwoodLincolnContinentalChryslerImperialFiat128HondaCivicToyotaCorollaToyotaCoronaDodgeChallengerAMCJavelinCamaroZ28PontiacFirebirdFiatX1-9Porsche914-2LotusEuropaFordPanteraLFerrariDinoMaseratiBoraVolvo142ECars212122.821.418.718.114.324.422.819.217.816.417.315.210.410.414.732.430.433.921.515.515.213.319.227.32630.415.819.71521.4mpg66468684466888888444488884448684cyl160160108258360225360146.7140.8167.6167.6275.8275.8275.847246044078.775.771.1120.131830435040079120.395.1351145301121disp110110931101751052456295123123180180180205215230665265971501502451756691113264175335109hp3.93.93.853.083.152.763.213.693.923.923.923.073.073.072.9333.234.084.934.223.72.763.153.733.084.084.433.774.223.623.544.11drat2.622.8752.323.2153.443.463.573.193.153.443.444.073.733.785.255.4245.3452.21.6151.8352.4653.523.4353.843.8451.9352.141.5133.172.773.572.78wt16.4617.0218.6119.4417.0220.2215.842022.918.318.917.417.61817.9817.8217.4219.4718.5219.920.0116.8717.315.4117.0518.916.716.914.515.514.618.6qsec00110101111000000111100001010001vs11100000000000000111000001111111am44433334444333333444333334555554gear44112142244333444121122421224682carb

Changing Size of Rows and Columns

values <- rbind(c('Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL<br>EXPENSES</b>'), c("Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad", 
     "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad", 
     "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad", 
     "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad", 
    "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad"))

fig <- plot_ly(
  type = 'table',
  columnorder = c(1,2),
  columnwidth = c(80,400),
  header = list(
    values = c('<b>EXPENSES</b><br>as of July 2017', '<b>DESCRIPTION</b>'),
    line = list(color = '#506784'),
    fill = list(color = '#119DFF'),
    align = c('left','center'),
    font = list(color = 'white', size = 12),
    height = 40
  ),
  cells = list(
    values = values,
    line = list(color = '#506784'),
    fill = list(color = c('#25FEFD', 'white')),
    align = c('left', 'center'),
    font = list(color = c('#506784'), size = 12),
    height = 30
    ))

fig
SalariesOfficeMerchandiseLegalTOTALEXPENSESEXPENSESas of July 2017Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam,an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeamvel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam,an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeamvel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam,an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeamvel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam,an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeamvel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam,an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeamvel adDESCRIPTION

Reference

See https://plotly.com/r/reference/#table for more information and chart attribute options!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)