2014-07-14 98 views
1

我對R相對較新,嘗試使用Shiny包。 我有類似的東西:http://shiny.rstudio.com/gallery/basic-datatable.html所以我們可以在那裏使用代碼。R:突出顯示Shiny數據表

我想補充的是一些單元格/行變得突出顯示(彩色背景或彩色字體)。如果你看例子,我想有行/單元格(整行或單元格,無所謂),如果汽車有8個或更多汽缸,則爲紅色,如果汽車具有6個或更少,則爲黃色,如果是有4個或更少。

有沒有辦法用Shiny軟件包來做到這一點?我嘗試過使用ShinyBS,但它什麼也沒做。

我也看到了這個:http://shiny.rstudio.com/gallery/datatables-demo.html。排序的列在這裏獲得新的顏色。這至少有點符合我想要的,所以我認爲CSS可能是答案,但沒有得到它的任何地方。

希望有人能幫助!

+0

在www文件夾中,您可以爲hilighting定義style.css :) – Aashu

+0

您能舉一個例子嗎? – user3804488

回答

1

您可以創建一個flag字段以匹配顏色,並使用styleEqual的``DT```

df <- mtcars 

df$colr_flag <- ifelse(df$cyl <= 4, 1, 
        ifelse(df$cyl <=6, 2, 
          ifelse(df$cyl >=8, 3, 0))) 


library(DT) 
options(DT.options = list(pageLength = 5)) 
# style V6 based on values of V6 
datatable(df) %>% formatStyle(
    'colr_flag', target = 'row', 
    backgroundColor = styleEqual(c(1, 2,3), c('green', 'yellow','red')) 
) 

enter image description here

1

,而無需創建一個新的列:

jscode <- " 
value <= 4 ? 'green' : (value <= 6 ? 'yellow' : (value >= 8 ? 'red' : 'white')) 
" 

datatable(mtcars) %>% formatStyle(
    "cyl", target = "row", 
    backgroundColor = JS(jscode)) 

或使用功能styleInterval

datatable(mtcars) %>% formatStyle(
    "cyl", target = "row", 
    backgroundColor = styleInterval(c(4, 6, 7), c("green", "yellow", "white", "red")))