daattali / colourpicker

🎨 A colour picker tool for Shiny and for selecting colours in plots (in R)
https://daattali.com/shiny/colourInput/
Other
215 stars 28 forks source link

Can colourpicker be used inside DT::datatable? #35

Closed jmw86069 closed 5 years ago

jmw86069 commented 5 years ago

I am trying to render a R-shiny datatable that includes colourpicker in each row. It seems to get close, as if it fails to initialize the colourpicker. The input box is shown but is empty and non-functional.

Best I can tell, I can find no working example of colourpicker inside any R-shiny datatable, or DT or any variant. (I could be wrong.)

This question is similar to issue #28 however in that case the input was rendered and functional, but was visibly blocked. In this case the input is not functional.

There were a couple related StackOverflow posts, and in fact they work for checkboxInput() but do not work for colourInput(). (See https://stackoverflow.com/questions/40020600/r-shiny-selectedinput-inside-renderdatatable-cells/40027726#40027726)

A minimal reproducible example:

library(shiny);
library(colourpicker);
library(DT);

shinyApp(
   ui=fluidPage(
      colourInput("col", "Select colour", "purple"),
      DTOutput("select_df")
   ),
   server=function(input, output) {
      df <- data.frame(one=letters[1:3]);
      colorset <- rainbow(3);
      df$colors <- sapply(1:3, function(i){
         as.character(
            colourInput(
               paste0("col_", i),
               label=NULL,
               value=colorset[i]
            )
         )
      })
      output$select_df <- renderDT({
         df %>% 
            datatable(
               editable=FALSE,
               rownames=FALSE,
               escape=FALSE,
               selection="none"
            )
         },
         server=FALSE
      )
   }
)

For kicks you can change escape=FALSE to escape=TRUE to see the exact HTML being used inside each table cell. It appears to be correct.

My best guess is that there is some initialization that is being ignored during the as.character() step. Any advice, pointers would be greatly appreciated!

jmw86069 commented 5 years ago

I did see this old thread in the rstudio/DT github, which in so many words and examples, seems to say "No."

https://github.com/rstudio/DT/issues/410

daattali commented 5 years ago

This is more of a DT issue than a colourpicker issue. Many inputs that aren't purely HTML don't work inside DT. The conversion to character essentially strips away any other related resources of the input and only keep its HTML, but any associated scripts are not included. Similar questions have been asked many times on stackoverflow regarding other inputs. I think eventually DT will need to add a way to support inputs, but I'm closing this issue as it's not something that colourpicker should solve