r - Conditional formatting of a table based on values in a second dataframe -
assume example:
library(shiny) library(tidyverse) df1 <- data.frame(a = 1:5, b = 6:10, c = 11:15) df2 <- data.frame(a = c(1:3,7,5), b = c(11, 7:10), c = 16:20) server <- function(input, output) { output$table1 <- rendertable({ df1 }, digits = 0 ) output$table2 <- rendertable({ df2 }, digits = 0 ) } ui <- fluidpage( div(h3("table formatting"), align = "center"), div(tableoutput("table1"),align = "center"), div(tableoutput("table2"), align = "center") ) shinyapp(ui = ui, server = server) is possible format first table based on data of second table? let's want font of first table's values becomes green if value in second table @ same position same. red if values not equal.
for example df1[1,1] = df2[1,1] = 1, should green. df1[4,1] = 4 not same df2[4,1] = 7, font in df1[4,1] should red.
i don't care if formatting based on data.table, formattable or different, long usable in shiny.
thanks in advance!
you can find full answer in own post. see below.
here code you:
library("shiny") library("formattable") df1 <- data.frame(a = 1:5, b = 6:10, c = 11:15) df2 <- data.frame(a = c(1:3,7,5), b = c(11, 7:10), c = 16:20) ident <- function(...){ args <- c(...) if( length( args ) > 2l ){ # recursively call ident() out <- c( identical( args[1] , args[2] ) , ident(args[-1])) }else{ out <- identical( args[1] , args[2] ) } return( all( out ) ) } ui <- fluidpage( div(h3("table formatting"), align = "center"), div(formattableoutput("table1"),align = "center"), div(formattableoutput("table2"), align = "center") ) server <- function(input, output) { output$table2 <- renderformattable({formattable(df2, list(a = formatter("span", style = x ~ style(color= ifelse(x == df1$a & x == 1,"green", ifelse(!x == df1$a, "red", na))))))}) output$table1 <- renderformattable({formattable(df1)}) } shinyapp(ui = ui, server = server) i have used purpose formattable package. have used format df2 , same thing should use df1 in server. hope keep going. wanted: when df1$a == df2$a , = 1 green, if !df1$a == df2$a red.

Comments
Post a Comment