在R markdown文档中循环生成格式化小部件 [英] Generate formattable widgets in a loop in an R markdown document

查看:156
本文介绍了在R markdown文档中循环生成格式化小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将HTML小部件(如formattable(来自格式表包))放在通过RMarkdown生成的HTML页面中。我需要在for循环中生成小部件。我怎样才能做到这一点?有或者没有 print(),两者都不起作用。

这是一个示例代码(部分来自 formattable主页):

  --- 
title:formattable example loop
输出:html_document
---


```{r}
library(formattable)


df< - data.frame(
id = 1:10,
name = c(Bob,Ashley,James,David,Jenny,
Hans ,狮子座,约翰,艾米莉,李),
年龄= c(28,27,30,28,29,29,27,27,31,30),
等级= c(C,A,A,C,B,B,B,A,C,C),
test1_score = c(8.9,9.5,9.6,9.9,9.1,9.3,9.3,9.9,8.5,8.6),
test2_score = c(9.1,9.1,9.2,9.1,8.9,8.5,9.2,9.3,9.1, ,8.8),
final_score = c(9,9.3,9.4,9,9,8.9,9.25,9.6,8.8,8.7),
registered = c(TRUE,FALSE,TRUE,FALSE,TRUE ,(TRUE,TRUE,FALSE,FALSE,FALSE),
stringsAsFactors = FALSE)
for(i in 1:10){
print(formattable(df,list(
age = color_tile(white,orange),
grade = formatter(span,
style = x〜ifelse(x ==A,style(color =green ),
test1_score = color_bar(pink,0.2),
test2_score = color_bar(pink,0.2),
final_score = formatter(span,
style = x〜style(color = ifelse(rank(-x)<= 3,green,gray)),
x〜sprintf(% 2f(rank:%02d),x,rank(-x))),
registered = formatter(span,
style = x〜style(color = ifelse(x, (x,ok,remove),ifelse(x,Yes,No))
)icon)





$ b

结果应该是html_document的十倍。试试这个(这里有一个表格输出的小包装,它是格式化表格的代码,因为它更容易阅读: - )



RP

$

$ b $ title:formattable example loop
output: html_document
---
$ b $``{r setup}
library(formattable)
library(htmltools)

df< - data.frame(
id = 1:10,
name = c(Bob,Ashley,James,David,Jenny,
Hans ,狮子座,约翰,艾米莉,李),
年龄= c(28,27,30,28,29,29,27,27,31,30),
等级= c(C,A,A,C,B,B,B,A,C,C),
test1_score = c(8.9,9.5,9.6,9.9,9.1,9.3,9.3,9.9,8.5,8.6),
test2_score = c(9.1,9.1,9.2,9.1,8.9,8.5,9.2,9.3,9.1, ,8.8),
final_score = c(9,9.3,9.4,9,9,8.9,9.25,9.6,8.8,8.7),
registered = c(TRUE,FALSE,TRUE,FALSE,TRUE ,TRUE,TRUE,FALSE,FALSE,FALSE),
stringsAsFactors = FALSE)

show_plot< - function(plot_object){
div(style =margin:auto; text-align:center,plot_object)
}
```
$ b $```{r}
do.call(div,lapply(1:10 ,福(formattable(df,list(
age = color_tile(white,orange),
grade = formatter(span ,
style = x〜ifelse(x ==A,style(color =green,font.weight =bold),NA)),
test1_score = color_bar(pink ,0.2),
test2_score = color_bar(pink,0.2),
final_score = formatter(span,
style = x〜style(color = ifelse(rank(-x )),
x〜sprintf(%。2f(等级:%02d),x,等级(-x))),
注册= formatter(span,
style = x〜style(color = ifelse(x,green,red)),
x〜icontext(ifelse(x,ok,remove ),ifelse(x,是,否)))
))))

}))
```


I want to put HTML widgets like formattable (from formattable package) in an HTML page generated through RMarkdown. I need the widgets to be generated from within a for loop. How can i do that? With or without print(), both don't work.

This is an example code (partly taken from formattable homepage):

---
title: "formattable example loop"
output: html_document
---


```{r}
library(formattable)


df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
    "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)
for (i in 1: 10){
print(formattable(df, list(
  age = color_tile("white", "orange"),
  grade = formatter("span",
    style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
  test1_score = color_bar("pink", 0.2),
  test2_score = color_bar("pink", 0.2),
  final_score = formatter("span",
    style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
    x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
  registered = formatter("span", 
    style = x ~ style(color = ifelse(x, "green", "red")),
    x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
)))
}
```

The result should be ten times this formattable in an html_document.

解决方案

Try this (there's a small wrapper for the table output and it's the code from the formattable site as it was easier to read :-)

RPubs Preview

---
title: "formattable example loop"
output: html_document
---

```{r setup}
library(formattable)
library(htmltools)

df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
    "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)

show_plot <- function(plot_object) {
  div(style="margin:auto;text-align:center", plot_object)
}
```

```{r}
do.call(div, lapply(1:10, function(i) {

show_plot(print(formattable(df, list(
  age = color_tile("white", "orange"),
  grade = formatter("span",
    style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
  test1_score = color_bar("pink", 0.2),
  test2_score = color_bar("pink", 0.2),
  final_score = formatter("span",
    style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
    x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
  registered = formatter("span", 
    style = x ~ style(color = ifelse(x, "green", "red")),
    x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))))

}))
```

这篇关于在R markdown文档中循环生成格式化小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆