我想在闪亮的页面中显示html表。该表由一个函数生成,并使用以下数据:
rep_loop <- data.frame(Activity = LETTERS[1:3], ID_resource_rep = sample(1:10, 3),
Div_resource_rep = sample(1:10, 3),
ID_resource_loop = sample(1:10, 3),
Div_resource_loop = sample(1:10, 3))
build_table <- function(data){
html.table <-
tags$table(style = "border:1px solid black; padding: 3%; width: 80%",
tags$tr(
tags$th(" "),
tags$th(colspan = 2, "Repeat (Act-X-Act)"),
tags$th(colspan = 2, "Selfloop (Act-Act)")
),
tags$tr(
tags$th(" "),
tags$th("Same Resource"),
tags$th("Other Resource"),
tags$th("Same Resource"),
tags$th("Other Resource")
),
tags$tr(
tags$td(data$Activity),
tags$td(data$ID_resource_rep),
tags$td(data$Div_resource_rep),
tags$td(data$ID_resource_loop),
tags$td(data$Div_resource_loop)
)
)
return(html.table)
}
这是
的结果test <- build_table(rep_loop)
<table style="border:1px solid black; padding: 5%; width: 80%">
<tr>
<th> </th>
<th colspan="2">Repeat (Act-X-Act)</th>
<th colspan="2">Selfloop (Act-Act)</th>
</tr>
<tr>
<th> </th>
<th>Same Resource</th>
<th>Other Resource</th>
<th>Same Resource</th>
<th>Other Resource</th>
</tr>
<tr>
<td>A</td>
<td>1</td>
<td>1</td>
<td>4</td>
<td>6</td>
</tr>
</table>
Warning messages:
1: In charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
2: In charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
3: In charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
4: In charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
5: In charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
我希望build_table将返回一个带有标题的表,其后是数据行。标头与预期的一样,但并未使用data.frame中的所有行,而是仅显示第一行,后跟5条警告。
我在哪里弄错了?
本
答案 0 :(得分:2)
那是因为tags$td
中没有向量。做:
rep_loop <- data.frame(Activity = LETTERS[1:3], ID_resource_rep = sample(1:10, 3),
Div_resource_rep = sample(1:10, 3),
ID_resource_loop = sample(1:10, 3),
Div_resource_loop = sample(1:10, 3))
build_table <- function(data){
html.table <-
tags$table(style = "border:1px solid black; padding: 3%; width: 80%",
tags$tr(
tags$th(" "),
tags$th(colspan = 2, "Repeat (Act-X-Act)"),
tags$th(colspan = 2, "Selfloop (Act-Act)")
),
tags$tr(
tags$th(" "),
tags$th("Same Resource"),
tags$th("Other Resource"),
tags$th("Same Resource"),
tags$th("Other Resource")
),
mapply(function(x1,x2,x3,x4,x5){
tags$tr(
tags$td(x1),
tags$td(x2),
tags$td(x3),
tags$td(x4),
tags$td(x5)
)
}, data$Activity,
data$ID_resource_rep,
data$Div_resource_rep,
data$ID_resource_loop,
data$Div_resource_loop,
SIMPLIFY = FALSE)
)
return(html.table)
}
build_table(rep_loop)
<table style="border:1px solid black; padding: 3%; width: 80%">
<tr>
<th> </th>
<th colspan="2">Repeat (Act-X-Act)</th>
<th colspan="2">Selfloop (Act-Act)</th>
</tr>
<tr>
<th> </th>
<th>Same Resource</th>
<th>Other Resource</th>
<th>Same Resource</th>
<th>Other Resource</th>
</tr>
<tr>
<td>A</td>
<td>5</td>
<td>10</td>
<td>4</td>
<td>2</td>
</tr>
<tr>
<td>B</td>
<td>6</td>
<td>8</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>C</td>
<td>3</td>
<td>7</td>
<td>2</td>
<td>5</td>
</tr>
</table>