|
| 1 | +--- |
| 2 | +title: Tables in R | Examples | Plotly |
| 3 | +name: Tables |
| 4 | +permalink: r/table/ |
| 5 | +description: How to make tables in R with Plotly. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/table.gif |
| 8 | +language: r |
| 9 | +has_thumbnail: true |
| 10 | +display_as: basic |
| 11 | +order: 7.1 |
| 12 | +output: |
| 13 | + html_document: |
| 14 | + keep_md: true |
| 15 | +--- |
| 16 | + |
| 17 | +```{r, echo = FALSE, message=FALSE} |
| 18 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 19 | +Sys.setenv("plotly_username"="RPlotBot") |
| 20 | +Sys.setenv("plotly_api_key"="q0lz6r5efr") |
| 21 | +``` |
| 22 | +### New to Plotly? |
| 23 | + |
| 24 | +Plotly's R library is free and open source!<br> |
| 25 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 26 | +You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br> |
| 27 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 28 | + |
| 29 | +### Version Check |
| 30 | + |
| 31 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 32 | +Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. |
| 33 | +```{r} |
| 34 | +library(plotly) |
| 35 | +packageVersion('plotly') |
| 36 | +``` |
| 37 | + |
| 38 | +### Basic Table |
| 39 | + |
| 40 | +```{r, results = 'hide'} |
| 41 | +library(plotly) |
| 42 | +
|
| 43 | +p <- plot_ly( |
| 44 | + type = 'table', |
| 45 | + columnwidth = c(100, 100), |
| 46 | + columnorder = c(0, 1), |
| 47 | + header = list( |
| 48 | + values = list(list(c("Cut")), |
| 49 | + list(c("Price")) |
| 50 | + ), |
| 51 | + align = c("center", "center"), |
| 52 | + line = list(width = 1, color = 'black'), |
| 53 | + fill = list(color = c("grey", "grey")), |
| 54 | + font = list(family = "Arial", size = 14, color = "white") |
| 55 | + ), |
| 56 | + cells = list( |
| 57 | + values = list(c(diamonds$cut),c(diamonds$price)), |
| 58 | + align = c("center", "center"), |
| 59 | + line = list(color = "black", width = 1), |
| 60 | + font = list(family = "Arial", size = 12, color = c("black")) |
| 61 | + )) |
| 62 | +
|
| 63 | +# Create a shareable link to your chart |
| 64 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 65 | +chart_link = api_create(p, filename="table-basic") |
| 66 | +chart_link |
| 67 | +``` |
| 68 | + |
| 69 | +```{r, echo=FALSE} |
| 70 | +chart_link |
| 71 | +``` |
| 72 | + |
| 73 | +### Styled Table |
| 74 | + |
| 75 | +```{r, results = 'hide'} |
| 76 | +library(plotly) |
| 77 | +
|
| 78 | +p <- plot_ly( |
| 79 | + type = 'table', |
| 80 | + header = list( |
| 81 | + values = list(list('<b>EXPENSES</b>'), |
| 82 | + list('<b>Q1</b>'), |
| 83 | + list('<b>Q2</b>'), |
| 84 | + list('<b>Q3</b>'), |
| 85 | + list('<b>Q4</b>')), |
| 86 | + line = list(color = '#506784'), |
| 87 | + fill = list(color = '#119DFF'), |
| 88 | + align = c('left','center'), |
| 89 | + font = list(color = 'white', size = 12) |
| 90 | + ), |
| 91 | + cells = list( |
| 92 | + values = list( |
| 93 | + c('Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'), |
| 94 | + c(1200000, 20000, 80000, 2000, 12120000), |
| 95 | + c(1300000, 20000, 70000, 2000, 130902000), |
| 96 | + c(1300000, 20000, 120000, 2000, 131222000), |
| 97 | + c(1400000, 20000, 90000, 2000, 14102000)), |
| 98 | + line = list(color = '#506784'), |
| 99 | + fill = list(color = c('#25FEFD', 'white')), |
| 100 | + align = c('left', 'center'), |
| 101 | + font = list(color = c('#506784'), size = 12) |
| 102 | + )) |
| 103 | +
|
| 104 | +# Create a shareable link to your chart |
| 105 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 106 | +chart_link = api_create(p, filename="table-styled") |
| 107 | +chart_link |
| 108 | +``` |
| 109 | + |
| 110 | +```{r, echo=FALSE} |
| 111 | +chart_link |
| 112 | +``` |
| 113 | + |
| 114 | +### Table From a Dataframe |
| 115 | + |
| 116 | +```{r, results = 'hide'} |
| 117 | +library(plotly) |
| 118 | +
|
| 119 | +headerValues <- list() |
| 120 | +for (i in (0:ncol(mtcars))) { |
| 121 | + name <- names(mtcars)[i] |
| 122 | + headerValues[i] <- name |
| 123 | +} |
| 124 | +
|
| 125 | +headerValues <- append(headerValues, "<b>Cars</b>", after = 0) |
| 126 | +
|
| 127 | +cellValues <- list() |
| 128 | +for (i in (0:ncol(mtcars))) { |
| 129 | + row <- mtcars[i] |
| 130 | + cellValues[i] <- row |
| 131 | +} |
| 132 | +
|
| 133 | +cellValues <- append(cellValues, list(rownames(mtcars)), after = 0) |
| 134 | +
|
| 135 | +p <- plot_ly( |
| 136 | + type = 'table', |
| 137 | + header = list( |
| 138 | + values = headerValues, |
| 139 | + align = c('left', rep('center', ncol(mtcars))), |
| 140 | + line = list(width = 1, color = 'black'), |
| 141 | + fill = list(color = 'rgb(235, 100, 230)'), |
| 142 | + font = list(family = "Arial", size = 14, color = "white") |
| 143 | + ), |
| 144 | + cells = list( |
| 145 | + values = cellValues, |
| 146 | + align = c('left', rep('center', ncol(mtcars))), |
| 147 | + line = list(color = "black", width = 1), |
| 148 | + fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')), |
| 149 | + font = list(family = "Arial", size = 12, color = c("black")) |
| 150 | + )) |
| 151 | +
|
| 152 | +# Create a shareable link to your chart |
| 153 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 154 | +chart_link = api_create(p, filename="table-df") |
| 155 | +chart_link |
| 156 | +``` |
| 157 | + |
| 158 | +```{r, echo=FALSE} |
| 159 | +chart_link |
| 160 | +``` |
| 161 | + |
| 162 | +### Changing Size of Rows and Columns |
| 163 | + |
| 164 | +```{r, results = 'hide'} |
| 165 | +values <- list(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", |
| 166 | + "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", |
| 167 | + "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", |
| 168 | + "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", |
| 169 | + "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")) |
| 170 | +
|
| 171 | +p <- plot_ly( |
| 172 | + type = 'table', |
| 173 | + columnorder = c(1,2), |
| 174 | + columnwidth = c(80,400), |
| 175 | + header = list( |
| 176 | + values = list(list('<b>EXPENSES</b><br>as of July 2017'), |
| 177 | + list('<b>DESCRIPTION</b>')), |
| 178 | + line = list(color = '#506784'), |
| 179 | + fill = list(color = '#119DFF'), |
| 180 | + align = c('left','center'), |
| 181 | + font = list(color = 'white', size = 12), |
| 182 | + height = 40 |
| 183 | + ), |
| 184 | + cells = list( |
| 185 | + values = values, |
| 186 | + line = list(color = '#506784'), |
| 187 | + fill = list(color = c('#25FEFD', 'white')), |
| 188 | + align = c('left', 'center'), |
| 189 | + font = list(color = c('#506784'), size = 12), |
| 190 | + height = 30 |
| 191 | + )) |
| 192 | +
|
| 193 | +# Create a shareable link to your chart |
| 194 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 195 | +chart_link = api_create(p, filename="table-text") |
| 196 | +chart_link |
| 197 | +``` |
| 198 | + |
| 199 | +```{r, echo=FALSE} |
| 200 | +chart_link |
| 201 | +``` |
| 202 | + |
| 203 | +#Reference |
| 204 | + |
| 205 | +See [https://plot.ly/r/reference/#table](https://plot.ly/r/reference/#table) for more information and chart attribute options! |
| 206 | + |
0 commit comments