2016-01-22 27 views
2

我想將HTML小部件(如formattable(來自格式表包))放在通過RMarkdown生成的HTML頁面中。我需要從for循環中生成小部件。我怎樣才能做到這一點?無論有沒有print(),兩者都不起作用。在R markdown文檔的循環中生成格式表小部件

這是一個示例代碼(部分地從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"))) 
))) 
} 
``` 

結果應該是這formattable在html_document十倍。

+0

嗨RDATA,你有沒有得到這工作? – Chuck

+0

@chuckM我認爲當時我被另一個項目分散了注意力,我沒有完成這個項目。 – rdatasculptor

+0

悲傷的臉:(謝謝你的迴應 – Chuck

回答

2

試試這個(有對錶輸出一個小包裝,它是從formattable網站,因爲它是更容易代碼讀取:-)

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"))) 
)))) 

})) 
``` 
+0

這看起來非常有希望!謝謝。但是,由於你的解決方案不使用for循環,我還沒有能夠使它在我的腳本中工作,我會繼續嘗試 – rdatasculptor

+0

我一直試圖運行這個,並繼續得到'錯誤match.fun(樂趣):'0.2'不是一個函數,字符或符號'任何想法爲什麼? – Chuck

+0

@Chuck看看'?color_bar',第二個參數'fun'是「將輸入向量映射到0到1的值的轉換函數」。默認情況下使用比例。 – elikesprogramming

相關問題