2017-03-03 1 views
1

이전 표의 값에 따라 DT를 포맷하려고합니다. 예를 들어, 이 증가했거나 감소했거나 그대로 남았는지 표시하고 싶습니다. kable로이 작업을 수행 할 수 있지만 은 셀을 클릭하고 다른 데이터에 관련된 모든 데이터를 과 다른 DT에 표시하려는 다음 단계를 얻을 수 없습니다. 이 경우 reference_df다른 데이터 세트의 값에 따라 반짝이는 데이터 테이블 (DT)의 색을 지정하십시오.

library(shiny) 
library(DT) 
library(dplyr) 
ui <- fluidPage(
    mainPanel(
     dataTableOutput("iris_head") 
) 
) 

server <- function(input, output) { 

    #df_data <- iris 

    df_data <- head(iris[-5]) 

    # Just a dataset describing if iris has changed over a month 
    # If reference data is of the same size as the original data (df_data). 
    # If reference data is negative I want the cell in the df_data to be green; 
    # If zero blue and if positive then green. 
    # I can make changes with ranges within the current range, can we get the color encoding from another table? 
    # set the seed 
    set.seed(42) 
    reference_df <- (sapply(df_data, function(x) jitter(x, amount = 2)) - df_data) %>% 
    round(. , digits = 0) 

    print(reference_df) 


    output$iris_head <- renderDataTable(datatable(df_data, selection = "single")%>% 
             formatStyle('Sepal.Width', 
                color = styleInterval(c(3.4, 3.8), c('green', 'blue', 'red')), 
                backgroundColor = styleInterval(3.4, c('gray', 'yellow'))) %>% 
             formatString('Sepal.Width', suffix = '<font color="red">&uArr; </font>')) 


} 

shinyApp(ui = ui, server = server) 

이다 :

Sepal.Length Sepal.Width Petal.Length Petal.Width 
     2   1   2   0 
     2   -1   -1   0 
     -1   1   0   2 
     1   1   2   -1 
     1   0   2   2 
     0   1   -2   2 

필요한 출력은 I는 텍스트를 착색 할 그림을 그리고 가능하면 배경 reference_df의 값에 따라.

텍스트 색상 부분에 대한

enter image description here

답변

3

, 당신은 formatStyle와 함께 할 수 있지만 값에 따라 1 ~ 4 열 스타일을 한 후, cbinddf_datareference_df 필요 datatable에 전달하고 바꿀 것 열 5에서 8까지 :

datatable(cbind(df_data,reference_df), selection = "single", 
               options=list(columnDefs = list(list(visible=FALSE, targets=c(5:8)))))%>% 
             formatStyle(1:4, valueColumns=5:8, 
                color = JS("value < 0 ? 'red' : value > 0 ? 'green' : 'blue'")) 

columnDefs 부분은 마지막 4 개의 열을 숨 깁니다.

당신이 화살표를 추가하려는 경우, 당신은 datatable에 전달하기 전에 색상과 화살표를 추가 할 df_data을 수정할 수 있도록 값을 기반으로하지 formatString

for(col in 1:dim(df_data)[2]){ 
    df_data[col] <- mapply(function(i,j){ 
     ifelse(i > 0, paste0("<span style='color:red'>",j,"<font>&uArr; </font></span>"), 
      ifelse(i<0, paste0("<span style='color:green'>",j,"<font>&dArr; </font></span>"), 
        paste0("<span style='color:blue'>",j,"<font>&hArr; </font></span>"))) 
    },reference_df[col],df_data[col]) 
    } 

    output$iris_head <- renderDataTable(
    datatable(df_data, selection = "single",escape = F) 
    ) 

이 값을 통해 루프 df_data이고 값은 reference_df에 따라 변경됩니다. HTML 이스케이프를 방지하려면 datatable 호출에서 escape=F을 인수로 사용해야합니다.

당신은 당신이 배경 등

+0

감사 색상을 원하는 경우 span 태그보다 CSS 스타일을 추가 할 수 있습니다! 아주 잘 설명 된 대답. 누군가가 그런 좋은 대답을 받았다는 사실을 고려해 논평없이이 질문을 무감각하게 하긴 이상하지 않다. – discipulus

관련 문제