Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc09caf

Browse files
committed
use non-list datatypes in R table examples to send data to api via grid
src elements
1 parent 47fc868 commit bc09caf

File tree

2 files changed

+34
-134
lines changed

2 files changed

+34
-134
lines changed

_posts/r/basic/2018-11-23-table.Rmd

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ p <- plot_ly(
4545
columnwidth = c(100, 100),
4646
columnorder = c(0, 1),
4747
header = list(
48-
values = list(list(c("Cut")),
49-
list(c("Price"))
50-
),
48+
values = c("Cut","Price"),
5149
align = c("center", "center"),
5250
line = list(width = 1, color = 'black'),
5351
fill = list(color = c("grey", "grey")),
5452
font = list(family = "Arial", size = 14, color = "white")
5553
),
5654
cells = list(
57-
values = list(c(diamonds$cut),c(diamonds$price)),
55+
values = rbind(diamonds$cut, diamonds$price),
5856
align = c("center", "center"),
5957
line = list(color = "black", width = 1),
6058
font = list(family = "Arial", size = 12, color = c("black"))
@@ -78,23 +76,19 @@ library(plotly)
7876
p <- plot_ly(
7977
type = 'table',
8078
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>')),
79+
values = c('<b>EXPENSES</b>', '<b>Q1</b>','<b>Q2</b>','<b>Q3</b>','<b>Q4</b>'),
8680
line = list(color = '#506784'),
8781
fill = list(color = '#119DFF'),
8882
align = c('left','center'),
8983
font = list(color = 'white', size = 12)
9084
),
9185
cells = list(
92-
values = list(
86+
values = rbind(
9387
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)),
88+
c(1200000, 20000, 80000, 2000, 1212000),
89+
c(1300000, 20000, 70000, 2000, 1392000),
90+
c(1300000, 20000, 120000, 2000, 1442000),
91+
c(1400000, 20000, 90000, 2000, 1412000)),
9892
line = list(color = '#506784'),
9993
fill = list(color = c('#25FEFD', 'white')),
10094
align = c('left', 'center'),
@@ -116,33 +110,20 @@ chart_link
116110
```{r, results = 'hide'}
117111
library(plotly)
118112
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-
135113
p <- plot_ly(
136114
type = 'table',
137115
header = list(
138-
values = headerValues,
116+
values = c("<b>Cars</b>", names(mtcars)),
139117
align = c('left', rep('center', ncol(mtcars))),
140118
line = list(width = 1, color = 'black'),
141119
fill = list(color = 'rgb(235, 100, 230)'),
142120
font = list(family = "Arial", size = 14, color = "white")
143121
),
144122
cells = list(
145-
values = cellValues,
123+
values = rbind(
124+
rownames(mtcars),
125+
t(as.matrix(unname(mtcars)))
126+
),
146127
align = c('left', rep('center', ncol(mtcars))),
147128
line = list(color = "black", width = 1),
148129
fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
@@ -162,7 +143,7 @@ chart_link
162143
### Changing Size of Rows and Columns
163144

164145
```{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",
146+
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",
166147
"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",
167148
"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",
168149
"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",
@@ -173,8 +154,7 @@ p <- plot_ly(
173154
columnorder = c(1,2),
174155
columnwidth = c(80,400),
175156
header = list(
176-
values = list(list('<b>EXPENSES</b><br>as of July 2017'),
177-
list('<b>DESCRIPTION</b>')),
157+
values = c('<b>EXPENSES</b><br>as of July 2017', '<b>DESCRIPTION</b>'),
178158
line = list(color = '#506784'),
179159
fill = list(color = '#119DFF'),
180160
align = c('left','center'),

_posts/r/basic/2018-11-23-table.md

Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ p <- plot_ly(
4747
columnwidth = c(100, 100),
4848
columnorder = c(0, 1),
4949
header = list(
50-
values = list(list(c("Cut")),
51-
list(c("Price"))
52-
),
50+
values = c("Cut","Price"),
5351
align = c("center", "center"),
5452
line = list(width = 1, color = 'black'),
5553
fill = list(color = c("grey", "grey")),
5654
font = list(family = "Arial", size = 14, color = "white")
5755
),
5856
cells = list(
59-
values = list(c(diamonds$cut),c(diamonds$price)),
57+
values = rbind(diamonds$cut, diamonds$price),
6058
align = c("center", "center"),
6159
line = list(color = "black", width = 1),
6260
font = list(family = "Arial", size = 12, color = c("black"))
@@ -65,25 +63,10 @@ p <- plot_ly(
6563
# Create a shareable link to your chart
6664
# Set up API credentials: https://plot.ly/r/getting-started
6765
chart_link = api_create(p, filename="table-basic")
68-
```
69-
70-
```
71-
## Error: Client error: (400) Bad Request
72-
## Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure [('data', 0, u'cells', u'values'), ('data', 0, u'header', u'values')]
73-
```
74-
75-
```r
7666
chart_link
7767
```
7868

79-
```
80-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
81-
```
82-
83-
84-
```
85-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
86-
```
69+
<iframe src="https://plot.ly/~RPlotBot/5628.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
8770

8871
### Styled Table
8972

@@ -94,23 +77,19 @@ library(plotly)
9477
p <- plot_ly(
9578
type = 'table',
9679
header = list(
97-
values = list(list('<b>EXPENSES</b>'),
98-
list('<b>Q1</b>'),
99-
list('<b>Q2</b>'),
100-
list('<b>Q3</b>'),
101-
list('<b>Q4</b>')),
80+
values = c('<b>EXPENSES</b>', '<b>Q1</b>','<b>Q2</b>','<b>Q3</b>','<b>Q4</b>'),
10281
line = list(color = '#506784'),
10382
fill = list(color = '#119DFF'),
10483
align = c('left','center'),
10584
font = list(color = 'white', size = 12)
10685
),
10786
cells = list(
108-
values = list(
87+
values = rbind(
10988
c('Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'),
110-
c(1200000, 20000, 80000, 2000, 12120000),
111-
c(1300000, 20000, 70000, 2000, 130902000),
112-
c(1300000, 20000, 120000, 2000, 131222000),
113-
c(1400000, 20000, 90000, 2000, 14102000)),
89+
c(1200000, 20000, 80000, 2000, 1212000),
90+
c(1300000, 20000, 70000, 2000, 1392000),
91+
c(1300000, 20000, 120000, 2000, 1442000),
92+
c(1400000, 20000, 90000, 2000, 1412000)),
11493
line = list(color = '#506784'),
11594
fill = list(color = c('#25FEFD', 'white')),
11695
align = c('left', 'center'),
@@ -120,59 +99,31 @@ p <- plot_ly(
12099
# Create a shareable link to your chart
121100
# Set up API credentials: https://plot.ly/r/getting-started
122101
chart_link = api_create(p, filename="table-styled")
123-
```
124-
125-
```
126-
## Error: Client error: (400) Bad Request
127-
## Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure [('data', 0, u'cells', u'values'), ('data', 0, u'header', u'values')]
128-
```
129-
130-
```r
131102
chart_link
132103
```
133104

134-
```
135-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
136-
```
137-
138-
139-
```
140-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
141-
```
105+
<iframe src="https://plot.ly/~RPlotBot/5629.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
142106

143107
### Table From a Dataframe
144108

145109

146110
```r
147111
library(plotly)
148112

149-
headerValues <- list()
150-
for (i in (0:ncol(mtcars))) {
151-
name <- names(mtcars)[i]
152-
headerValues[i] <- name
153-
}
154-
155-
headerValues <- append(headerValues, "<b>Cars</b>", after = 0)
156-
157-
cellValues <- list()
158-
for (i in (0:ncol(mtcars))) {
159-
row <- mtcars[i]
160-
cellValues[i] <- row
161-
}
162-
163-
cellValues <- append(cellValues, list(rownames(mtcars)), after = 0)
164-
165113
p <- plot_ly(
166114
type = 'table',
167115
header = list(
168-
values = headerValues,
116+
values = c("<b>Cars</b>", names(mtcars)),
169117
align = c('left', rep('center', ncol(mtcars))),
170118
line = list(width = 1, color = 'black'),
171119
fill = list(color = 'rgb(235, 100, 230)'),
172120
font = list(family = "Arial", size = 14, color = "white")
173121
),
174122
cells = list(
175-
values = cellValues,
123+
values = rbind(
124+
rownames(mtcars),
125+
t(as.matrix(unname(mtcars)))
126+
),
176127
align = c('left', rep('center', ncol(mtcars))),
177128
line = list(color = "black", width = 1),
178129
fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
@@ -182,31 +133,16 @@ p <- plot_ly(
182133
# Create a shareable link to your chart
183134
# Set up API credentials: https://plot.ly/r/getting-started
184135
chart_link = api_create(p, filename="table-df")
185-
```
186-
187-
```
188-
## Error: Client error: (400) Bad Request
189-
## Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure [('data', 0, u'cells', u'values'), ('data', 0, u'header', u'values')]
190-
```
191-
192-
```r
193136
chart_link
194137
```
195138

196-
```
197-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
198-
```
199-
200-
201-
```
202-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
203-
```
139+
<iframe src="https://plot.ly/~RPlotBot/5630.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
204140

205141
### Changing Size of Rows and Columns
206142

207143

208144
```r
209-
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",
145+
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",
210146
"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",
211147
"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",
212148
"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",
@@ -217,8 +153,7 @@ p <- plot_ly(
217153
columnorder = c(1,2),
218154
columnwidth = c(80,400),
219155
header = list(
220-
values = list(list('<b>EXPENSES</b><br>as of July 2017'),
221-
list('<b>DESCRIPTION</b>')),
156+
values = c('<b>EXPENSES</b><br>as of July 2017', '<b>DESCRIPTION</b>'),
222157
line = list(color = '#506784'),
223158
fill = list(color = '#119DFF'),
224159
align = c('left','center'),
@@ -237,25 +172,10 @@ p <- plot_ly(
237172
# Create a shareable link to your chart
238173
# Set up API credentials: https://plot.ly/r/getting-started
239174
chart_link = api_create(p, filename="table-text")
240-
```
241-
242-
```
243-
## Error: Client error: (400) Bad Request
244-
## Figure field is invalid. Reason: Raw data arrays are not allowed at this endpoint. Use grid references instead. Raw data found at the following paths in the figure [('data', 0, u'cells', u'values'), ('data', 0, u'header', u'values')]
245-
```
246-
247-
```r
248175
chart_link
249176
```
250177

251-
```
252-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
253-
```
254-
255-
256-
```
257-
## Error in eval(expr, envir, enclos): object 'chart_link' not found
258-
```
178+
<iframe src="https://plot.ly/~RPlotBot/5631.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
259179

260180
#Reference
261181

0 commit comments

Comments
 (0)