Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. library(shiny)
  2. library(DT)
  3.  
  4. ui <- fluidPage(
  5. checkboxInput("click_me", "click me"),
  6. DT::dataTableOutput("bad_foo"),
  7. DT::dataTableOutput("good_foo")
  8. )
  9.  
  10. server <- function(input, output, session) {
  11.  
  12. data <- reactive(
  13. round(data.frame(
  14. x = runif(5, 0, 5),
  15. y = runif(5, 0, 10),
  16. z = runif(5, 0, 20)
  17. ), 3)
  18. )
  19.  
  20. break_points <- function(x) stats::quantile(x, probs = seq(.05, .95, .05), na.rm = TRUE)
  21. red_shade <- function(x) round(seq(255, 40, length.out = length(x) + 1), 0) %>% {paste0("rgb(255,", ., ",", ., ")")}
  22.  
  23.  
  24. dt1 <- reactive({
  25. non_reactive_data <- data()
  26.  
  27. brks <- apply(non_reactive_data, 1, break_points)
  28. clrs <- red_shade(break_points(non_reactive_data))
  29.  
  30. rowCallback <- "function(row, non_reactive_data, displayNum, index){"
  31. for(i in 1:ncol(non_reactive_data)){
  32. rowCallback <- c(
  33. rowCallback,
  34. sprintf("var value = non_reactive_data[%d];", i)
  35. )
  36. for(j in 1:nrow(non_reactive_data)){
  37. rowCallback <- c(
  38. rowCallback,
  39. sprintf("if(index === %d){", j-1),
  40. sprintf("$('td:eq(%d)',row).css('background-color', %s);",
  41. i, styleInterval(brks[,j], clrs)),
  42. "}"
  43. )
  44. }
  45. }
  46. rowCallback <- c(rowCallback, "}")
  47.  
  48. return(DT::datatable(non_reactive_data, options = list(rowCallback = JS(rowCallback))))
  49. })
  50.  
  51. dt2 <- reactive({
  52. DT::datatable(data())
  53. })
  54.  
  55.  
  56. output$bad_foo <- DT::renderDT({
  57. output <- dt1()
  58. if(input$click_me==TRUE){
  59. output <- output %>% DT::formatPercentage(1:ncol(data()), 3)
  60. }
  61. output
  62. })
  63.  
  64. output$good_foo <- DT::renderDT({
  65. output <- dt2()
  66. if(input$click_me==TRUE){
  67. output <- output %>% DT::formatPercentage(1:ncol(data()), 3)
  68. }
  69. output
  70. })
  71. }
  72.  
  73. shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement