Displaying commas and conditional highlighting in Rshiny - not compatible









up vote
0
down vote

favorite












I have a Shiny app rendering a datatable within which I would like to incorporate 2 conditional formatting features



  1. Add commas to numbers greater than 1000

  2. Apply blue background to column 2 values when values column 2 values are >= 1.3x values in column 1. Apply red background when column 2 values are <= .7x values in column 1.

I asked a question about how to incorporate commas in this SO post. I I remove the rowcallback argument in the script below, the commas render properly. Similarly, if I comment out the dom and formatCurrency arguments, the highlighting conditional fomatting renders properly, as well.



 js_cont_var_lookup <- reactive(
JS(
'function(nRow, aData)
for (i=2; i < 3; i++)
if (parseFloat(aData[i]) > aData[1]*(1.03))
$("td:eq(" + i + ")", nRow).css("background-color", "aqua");


for (i=2; i < 3; i++)
if (parseFloat(aData[i]) < aData[1]*(.7))
$("td:eq(" + i + ")", nRow).css("background-color", "red");


'
) # close JS
)

shinyApp(
ui = fluidPage(
DTOutput("dummy_data_table")
),
server = function(input, output)
output$dummy_data_table <- DT::renderDataTable(
data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000)) %>%
datatable(extensions = 'Buttons',
options = list(
pageLength = 50,
scrollX=TRUE,
dom = 'T<"clear">lBfrtip',
rowCallback = js_cont_var_lookup()
)
) %>%
formatCurrency(1:2, currency = "", interval = 3, mark = ",")
) # close renderDataTable

)


However, when I leave both in, the datatable hangs with a 'Processing' message.










share|improve this question















This question has an open bounty worth +50
reputation from matsuo_basho ending ending at 2018-11-15 14:43:21Z">in 3 days.


Looking for an answer drawing from credible and/or official sources.



















    up vote
    0
    down vote

    favorite












    I have a Shiny app rendering a datatable within which I would like to incorporate 2 conditional formatting features



    1. Add commas to numbers greater than 1000

    2. Apply blue background to column 2 values when values column 2 values are >= 1.3x values in column 1. Apply red background when column 2 values are <= .7x values in column 1.

    I asked a question about how to incorporate commas in this SO post. I I remove the rowcallback argument in the script below, the commas render properly. Similarly, if I comment out the dom and formatCurrency arguments, the highlighting conditional fomatting renders properly, as well.



     js_cont_var_lookup <- reactive(
    JS(
    'function(nRow, aData)
    for (i=2; i < 3; i++)
    if (parseFloat(aData[i]) > aData[1]*(1.03))
    $("td:eq(" + i + ")", nRow).css("background-color", "aqua");


    for (i=2; i < 3; i++)
    if (parseFloat(aData[i]) < aData[1]*(.7))
    $("td:eq(" + i + ")", nRow).css("background-color", "red");


    '
    ) # close JS
    )

    shinyApp(
    ui = fluidPage(
    DTOutput("dummy_data_table")
    ),
    server = function(input, output)
    output$dummy_data_table <- DT::renderDataTable(
    data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000)) %>%
    datatable(extensions = 'Buttons',
    options = list(
    pageLength = 50,
    scrollX=TRUE,
    dom = 'T<"clear">lBfrtip',
    rowCallback = js_cont_var_lookup()
    )
    ) %>%
    formatCurrency(1:2, currency = "", interval = 3, mark = ",")
    ) # close renderDataTable

    )


    However, when I leave both in, the datatable hangs with a 'Processing' message.










    share|improve this question















    This question has an open bounty worth +50
    reputation from matsuo_basho ending ending at 2018-11-15 14:43:21Z">in 3 days.


    Looking for an answer drawing from credible and/or official sources.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a Shiny app rendering a datatable within which I would like to incorporate 2 conditional formatting features



      1. Add commas to numbers greater than 1000

      2. Apply blue background to column 2 values when values column 2 values are >= 1.3x values in column 1. Apply red background when column 2 values are <= .7x values in column 1.

      I asked a question about how to incorporate commas in this SO post. I I remove the rowcallback argument in the script below, the commas render properly. Similarly, if I comment out the dom and formatCurrency arguments, the highlighting conditional fomatting renders properly, as well.



       js_cont_var_lookup <- reactive(
      JS(
      'function(nRow, aData)
      for (i=2; i < 3; i++)
      if (parseFloat(aData[i]) > aData[1]*(1.03))
      $("td:eq(" + i + ")", nRow).css("background-color", "aqua");


      for (i=2; i < 3; i++)
      if (parseFloat(aData[i]) < aData[1]*(.7))
      $("td:eq(" + i + ")", nRow).css("background-color", "red");


      '
      ) # close JS
      )

      shinyApp(
      ui = fluidPage(
      DTOutput("dummy_data_table")
      ),
      server = function(input, output)
      output$dummy_data_table <- DT::renderDataTable(
      data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000)) %>%
      datatable(extensions = 'Buttons',
      options = list(
      pageLength = 50,
      scrollX=TRUE,
      dom = 'T<"clear">lBfrtip',
      rowCallback = js_cont_var_lookup()
      )
      ) %>%
      formatCurrency(1:2, currency = "", interval = 3, mark = ",")
      ) # close renderDataTable

      )


      However, when I leave both in, the datatable hangs with a 'Processing' message.










      share|improve this question













      I have a Shiny app rendering a datatable within which I would like to incorporate 2 conditional formatting features



      1. Add commas to numbers greater than 1000

      2. Apply blue background to column 2 values when values column 2 values are >= 1.3x values in column 1. Apply red background when column 2 values are <= .7x values in column 1.

      I asked a question about how to incorporate commas in this SO post. I I remove the rowcallback argument in the script below, the commas render properly. Similarly, if I comment out the dom and formatCurrency arguments, the highlighting conditional fomatting renders properly, as well.



       js_cont_var_lookup <- reactive(
      JS(
      'function(nRow, aData)
      for (i=2; i < 3; i++)
      if (parseFloat(aData[i]) > aData[1]*(1.03))
      $("td:eq(" + i + ")", nRow).css("background-color", "aqua");


      for (i=2; i < 3; i++)
      if (parseFloat(aData[i]) < aData[1]*(.7))
      $("td:eq(" + i + ")", nRow).css("background-color", "red");


      '
      ) # close JS
      )

      shinyApp(
      ui = fluidPage(
      DTOutput("dummy_data_table")
      ),
      server = function(input, output)
      output$dummy_data_table <- DT::renderDataTable(
      data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000)) %>%
      datatable(extensions = 'Buttons',
      options = list(
      pageLength = 50,
      scrollX=TRUE,
      dom = 'T<"clear">lBfrtip',
      rowCallback = js_cont_var_lookup()
      )
      ) %>%
      formatCurrency(1:2, currency = "", interval = 3, mark = ",")
      ) # close renderDataTable

      )


      However, when I leave both in, the datatable hangs with a 'Processing' message.







      javascript r shiny






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 17:52









      matsuo_basho

      504827




      504827






      This question has an open bounty worth +50
      reputation from matsuo_basho ending ending at 2018-11-15 14:43:21Z">in 3 days.


      Looking for an answer drawing from credible and/or official sources.








      This question has an open bounty worth +50
      reputation from matsuo_basho ending ending at 2018-11-15 14:43:21Z">in 3 days.


      Looking for an answer drawing from credible and/or official sources.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Here is a soution avoiding the rowCallback:



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))
          myWorkData <- copy(myDisplayData)
          myWorkData[, colors := ifelse(B >= A*1.03, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(B <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, myWorkData$colors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          If you prefer using a data.frame:



          library(shiny)
          library(DT)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))

          MyColors <- vector(mode = 'character', length = 0L)

          for (i in seq(nrow(myDisplayData)))
          A <- myDisplayData$A[i]
          B <- myDisplayData$B[i]
          if (B >= A * 1.03)
          MyColors[i] <- 'rgb(0,255,255)'
          else if (B <= A * .7)
          MyColors[i] <- 'rgb(255, 0, 0)'

          else
          MyColors[i] <- 'rgb(255, 255, 255)'



          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, MyColors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          Here is a multi-column approach making the assumption, that all other columns are refering to column "A":



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(replicate(15,sample(round(runif(20,0,300000)), 20, rep=TRUE)))
          names(myDisplayData) <- LETTERS[1:15]

          targetColumns <- names(myDisplayData)[!names(myDisplayData) %in% "A"]

          myWorkData <- melt.data.table(myDisplayData, id.vars="A", measure.vars=targetColumns)
          myWorkData[, variable := NULL]
          myWorkData[, colors := ifelse(value >= A*1.3, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(value <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle(targetColumns, target = 'cell', backgroundColor = styleEqual(myWorkData$value, myWorkData$colors)) %>%
          formatCurrency(seq(ncol(myDisplayData)), currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          Result:
          Result






          share|improve this answer






















          • This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
            – matsuo_basho
            2 days ago










          • Are all other columns still refering to the first column regarding the color assignment?
            – ismirsehregal
            yesterday










          • Please see my second edit.
            – ismirsehregal
            yesterday










          • Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
            – ismirsehregal
            yesterday










          • Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
            – ismirsehregal
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159633%2fdisplaying-commas-and-conditional-highlighting-in-rshiny-not-compatible%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Here is a soution avoiding the rowCallback:



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))
          myWorkData <- copy(myDisplayData)
          myWorkData[, colors := ifelse(B >= A*1.03, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(B <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, myWorkData$colors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          If you prefer using a data.frame:



          library(shiny)
          library(DT)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))

          MyColors <- vector(mode = 'character', length = 0L)

          for (i in seq(nrow(myDisplayData)))
          A <- myDisplayData$A[i]
          B <- myDisplayData$B[i]
          if (B >= A * 1.03)
          MyColors[i] <- 'rgb(0,255,255)'
          else if (B <= A * .7)
          MyColors[i] <- 'rgb(255, 0, 0)'

          else
          MyColors[i] <- 'rgb(255, 255, 255)'



          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, MyColors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          Here is a multi-column approach making the assumption, that all other columns are refering to column "A":



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(replicate(15,sample(round(runif(20,0,300000)), 20, rep=TRUE)))
          names(myDisplayData) <- LETTERS[1:15]

          targetColumns <- names(myDisplayData)[!names(myDisplayData) %in% "A"]

          myWorkData <- melt.data.table(myDisplayData, id.vars="A", measure.vars=targetColumns)
          myWorkData[, variable := NULL]
          myWorkData[, colors := ifelse(value >= A*1.3, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(value <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle(targetColumns, target = 'cell', backgroundColor = styleEqual(myWorkData$value, myWorkData$colors)) %>%
          formatCurrency(seq(ncol(myDisplayData)), currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          Result:
          Result






          share|improve this answer






















          • This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
            – matsuo_basho
            2 days ago










          • Are all other columns still refering to the first column regarding the color assignment?
            – ismirsehregal
            yesterday










          • Please see my second edit.
            – ismirsehregal
            yesterday










          • Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
            – ismirsehregal
            yesterday










          • Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
            – ismirsehregal
            yesterday














          up vote
          0
          down vote













          Here is a soution avoiding the rowCallback:



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))
          myWorkData <- copy(myDisplayData)
          myWorkData[, colors := ifelse(B >= A*1.03, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(B <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, myWorkData$colors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          If you prefer using a data.frame:



          library(shiny)
          library(DT)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))

          MyColors <- vector(mode = 'character', length = 0L)

          for (i in seq(nrow(myDisplayData)))
          A <- myDisplayData$A[i]
          B <- myDisplayData$B[i]
          if (B >= A * 1.03)
          MyColors[i] <- 'rgb(0,255,255)'
          else if (B <= A * .7)
          MyColors[i] <- 'rgb(255, 0, 0)'

          else
          MyColors[i] <- 'rgb(255, 255, 255)'



          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, MyColors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          Here is a multi-column approach making the assumption, that all other columns are refering to column "A":



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(replicate(15,sample(round(runif(20,0,300000)), 20, rep=TRUE)))
          names(myDisplayData) <- LETTERS[1:15]

          targetColumns <- names(myDisplayData)[!names(myDisplayData) %in% "A"]

          myWorkData <- melt.data.table(myDisplayData, id.vars="A", measure.vars=targetColumns)
          myWorkData[, variable := NULL]
          myWorkData[, colors := ifelse(value >= A*1.3, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(value <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle(targetColumns, target = 'cell', backgroundColor = styleEqual(myWorkData$value, myWorkData$colors)) %>%
          formatCurrency(seq(ncol(myDisplayData)), currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          Result:
          Result






          share|improve this answer






















          • This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
            – matsuo_basho
            2 days ago










          • Are all other columns still refering to the first column regarding the color assignment?
            – ismirsehregal
            yesterday










          • Please see my second edit.
            – ismirsehregal
            yesterday










          • Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
            – ismirsehregal
            yesterday










          • Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
            – ismirsehregal
            yesterday












          up vote
          0
          down vote










          up vote
          0
          down vote









          Here is a soution avoiding the rowCallback:



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))
          myWorkData <- copy(myDisplayData)
          myWorkData[, colors := ifelse(B >= A*1.03, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(B <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, myWorkData$colors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          If you prefer using a data.frame:



          library(shiny)
          library(DT)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))

          MyColors <- vector(mode = 'character', length = 0L)

          for (i in seq(nrow(myDisplayData)))
          A <- myDisplayData$A[i]
          B <- myDisplayData$B[i]
          if (B >= A * 1.03)
          MyColors[i] <- 'rgb(0,255,255)'
          else if (B <= A * .7)
          MyColors[i] <- 'rgb(255, 0, 0)'

          else
          MyColors[i] <- 'rgb(255, 255, 255)'



          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, MyColors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          Here is a multi-column approach making the assumption, that all other columns are refering to column "A":



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(replicate(15,sample(round(runif(20,0,300000)), 20, rep=TRUE)))
          names(myDisplayData) <- LETTERS[1:15]

          targetColumns <- names(myDisplayData)[!names(myDisplayData) %in% "A"]

          myWorkData <- melt.data.table(myDisplayData, id.vars="A", measure.vars=targetColumns)
          myWorkData[, variable := NULL]
          myWorkData[, colors := ifelse(value >= A*1.3, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(value <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle(targetColumns, target = 'cell', backgroundColor = styleEqual(myWorkData$value, myWorkData$colors)) %>%
          formatCurrency(seq(ncol(myDisplayData)), currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          Result:
          Result






          share|improve this answer














          Here is a soution avoiding the rowCallback:



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))
          myWorkData <- copy(myDisplayData)
          myWorkData[, colors := ifelse(B >= A*1.03, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(B <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, myWorkData$colors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          If you prefer using a data.frame:



          library(shiny)
          library(DT)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.frame(A=c(100000, 200000, 300000), B=c(140000, 80000, 310000))

          MyColors <- vector(mode = 'character', length = 0L)

          for (i in seq(nrow(myDisplayData)))
          A <- myDisplayData$A[i]
          B <- myDisplayData$B[i]
          if (B >= A * 1.03)
          MyColors[i] <- 'rgb(0,255,255)'
          else if (B <= A * .7)
          MyColors[i] <- 'rgb(255, 0, 0)'

          else
          MyColors[i] <- 'rgb(255, 255, 255)'



          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle('B', target = 'cell', backgroundColor = styleEqual(myDisplayData$B, MyColors)) %>%
          formatCurrency(1:2, currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          1. Edit -------------------------

          Here is a multi-column approach making the assumption, that all other columns are refering to column "A":



          library(shiny)
          library(DT)
          library(data.table)

          shinyApp(
          ui = fluidPage(
          DTOutput("dummy_data_table")
          ),

          server = function(input, output)

          myDisplayData <- data.table(replicate(15,sample(round(runif(20,0,300000)), 20, rep=TRUE)))
          names(myDisplayData) <- LETTERS[1:15]

          targetColumns <- names(myDisplayData)[!names(myDisplayData) %in% "A"]

          myWorkData <- melt.data.table(myDisplayData, id.vars="A", measure.vars=targetColumns)
          myWorkData[, variable := NULL]
          myWorkData[, colors := ifelse(value >= A*1.3, 'rgb(0,255,255)', 'rgb(255, 255, 255)')]
          myWorkData[colors %in% 'rgb(255, 255, 255)', colors := ifelse(value <= A*.7, 'rgb(255, 0, 0)', 'rgb(255, 255, 255)')]

          output$dummy_data_table <- DT::renderDataTable(
          DT::datatable(
          myDisplayData,
          extensions = 'Buttons',
          options = list(
          pageLength = 50,
          scrollX=TRUE,
          dom = 'T<"clear">lBfrtip'
          )
          ) %>% formatStyle(targetColumns, target = 'cell', backgroundColor = styleEqual(myWorkData$value, myWorkData$colors)) %>%
          formatCurrency(seq(ncol(myDisplayData)), currency = "", interval = 3, mark = ",")
          ) # close renderDataTable


          )


          Result:
          Result







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered 2 days ago









          ismirsehregal

          3378




          3378











          • This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
            – matsuo_basho
            2 days ago










          • Are all other columns still refering to the first column regarding the color assignment?
            – ismirsehregal
            yesterday










          • Please see my second edit.
            – ismirsehregal
            yesterday










          • Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
            – ismirsehregal
            yesterday










          • Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
            – ismirsehregal
            yesterday
















          • This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
            – matsuo_basho
            2 days ago










          • Are all other columns still refering to the first column regarding the color assignment?
            – ismirsehregal
            yesterday










          • Please see my second edit.
            – ismirsehregal
            yesterday










          • Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
            – ismirsehregal
            yesterday










          • Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
            – ismirsehregal
            yesterday















          This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
          – matsuo_basho
          2 days ago




          This is great, thank you. One important point - I may have up to 15 columns (I just included 2 in my example for simplicity). So I would need to retain the for loop as in my example. How would that look?
          – matsuo_basho
          2 days ago












          Are all other columns still refering to the first column regarding the color assignment?
          – ismirsehregal
          yesterday




          Are all other columns still refering to the first column regarding the color assignment?
          – ismirsehregal
          yesterday












          Please see my second edit.
          – ismirsehregal
          yesterday




          Please see my second edit.
          – ismirsehregal
          yesterday












          Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
          – ismirsehregal
          yesterday




          Btw: in your question you are mentioning 1.3x but in your JS function it's 1.03. I made it 1.3 in my code now.
          – ismirsehregal
          yesterday












          Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
          – ismirsehregal
          yesterday




          Just saw there is a problem with the color mapping for the multi-col solution. The displayed values need to be row-unique for this to work otherwise a previously defined color is assigned. Will have another look when back at PC.
          – ismirsehregal
          yesterday

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159633%2fdisplaying-commas-and-conditional-highlighting-in-rshiny-not-compatible%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線