Giter VIP home page Giter VIP logo

Comments (5)

philibe avatar philibe commented on July 22, 2024 1

I've forgotten that DT can do that:

library(shiny)
library(DT)
library(dplyr)



ui <- basicPage(
  h2("The mtcars data"),
  p("with base DT formatStyle() and styleColorBar()"),  
  DT::dataTableOutput("mytable2")
)

server <- function(input, output) {
  
  
  output$mytable2 = DT::renderDataTable({
    datas <- (mtcars
              %>% dplyr::select (mpg,cyl,disp)
    )            
    
    res<-DT::datatable(datas,
                       options = list(
                         pageLength =5
                       )
    )
    
    res<- (res
           %>% formatStyle(
             columns = c("disp"),
             background = styleColorBar(datas$disp, 'steelblue',angle = -90),
             backgroundSize = '90% 100%',
             backgroundRepeat = 'no-repeat',
             backgroundPosition = 'center'
           )
    )
    res
  })
}

shinyApp(ui, server)

percent_shiny_DT1

And my previous answer updated:

library(shiny)
library(DT)
library(sparkline)
library(dplyr)



ui <- basicPage(
  h2("The mtcars data"),
  sparklineOutput("only to load sparkline"),
  p("with sparkline"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  
  
  output$mytable = DT::renderDataTable({
    datas <- (mtcars
              %>% select (mpg,cyl,disp)
              %>% mutate(
                max_disp=max(disp),
                disp_pct=round(100*disp/max_disp,2),
                sparkline_disp=paste(disp_pct,100,sep=","),
                sparkline_disp_graph=sparkline_disp
              )
              %>% select(mpg,cyl,disp,sparkline_disp_graph, everything())
    )            
    
    escape_vector<- -(which(colnames(datas) %in% c(
      "sparkline_disp_graph",
      use.names = FALSE
    ))    +1)
    
    vector_value_sparkline=(which(colnames(datas) =="sparkline_disp_graph")    )
    vector_disp_pct=(which(colnames(datas) =="disp_pct")    )
    
    sparkline_line_string <- "type: 'bullet', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"
    
    res<-DT::datatable(datas,
                       # escape =escape_vector,
                       options = list(
                         pageLength =5,
                         columnDefs= list(
                           list(
                             targets = vector_value_sparkline,
                             render = JS(paste0(
                               "function(data, type, full){",
                               "  return '<span class=sparkSamples>' + data + '</span>&nbsp;'",
                               "  +full[",vector_disp_pct[1],"].toString()+'%'",
                               "}"
                             ))
                           )
                         ),
                         fnDrawCallback = JS(paste0("function (oSettings, json) {\n  $('.sparkSamples:not(:has(canvas))').sparkline('html', { ",
                                                    sparkline_line_string, " });\n}"), collapse = "")
                       )
    )
    res
  })
  
  
}

shinyApp(ui, server)

percent_shiny_DT2

from dt.

cwilligv avatar cwilligv commented on July 22, 2024 1

hey @philibe , thank you so much. This is great stuff!!! I was so focused on getting the JS part working that I ignored these options. I guess this is good enough.

Cheers.

from dt.

stla avatar stla commented on July 22, 2024 1

In order to use a plugin, it is always possible to download it and then to add it to the HTML dependencies of the DT table.

library(shiny)
library(DT)
library(htmltools)

dat <- iris[, 1:3]
dat$percentage <- paste0(rpois(nrow(dat), 50), "%")

dep <- htmlDependency(
  "percentageBars",
  version = "1.1",
  src = normalizePath("www"),
  script = "percentageBars.js",
  all_files = FALSE
)

dtable <- datatable(
  dat,
  rownames = FALSE,
  options = list(
    columnDefs = list(
      list(
        targets = list(3),
        render = 
          JS("DataTable.render.percentBar( 'round','#FFF', '#269ABC', '#31B0D5', '#286090', 1, 'groove' )")
      )
    )
  )
)
dtable$dependencies <- c(dtable$dependencies, list(dep))


ui <- fluidPage(
  br(),
  DTOutput("dtable")
)

server <- function(input, output, session) {
  
  output[["dtable"]] <- renderDT({
    dtable
  })
  
}

shinyApp(ui, server)

DTpercentageBars

Looks good.

from dt.

philibe avatar philibe commented on July 22, 2024

You can try this with sparkline to workaround:

library(shiny)
library(DT)
library(sparkline)
library(dplyr)



ui <- basicPage(
  h2("The mtcars data"),
  sparklineOutput("only to load sparkline"),
  p("with sparkline"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  
  
  output$mytable = DT::renderDataTable({
    datas <- (mtcars
              %>% select (mpg,cyl,disp)
              %>% mutate(
                max_disp=max(disp), 
                sparkline_disp=paste(100*disp/max_disp,100,sep=","),
                sparkline_disp_graph=sparkline_disp
              )
              %>% select(mpg,cyl,disp,sparkline_disp_graph, everything())
    )            
    
    escape_vector<- -(which(colnames(datas) %in% c(
      "sparkline_disp_graph",
      use.names = FALSE
    ))    +1)
    
    vector_value_sparkline=(which(colnames(datas) =="sparkline_disp_graph")    )
    
    sparkline_line_string <- "type: 'bullet', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"
    
    res<-DT::datatable(datas,
                       escape =escape_vector,
                       options = list(
                         columnDefs= list(
                           list(
                             targets = vector_value_sparkline,
                             render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")
                           )
                         ),
                         fnDrawCallback = JS(paste0("function (oSettings, json) {\n  $('.sparkSamples:not(:has(canvas))').sparkline('html', { ",
                                                    sparkline_line_string, " });\n}"), collapse = "")
                       )
    )
    res
  })
}

shinyApp(ui, server)

sparkiline_percent_shiny

from dt.

philibe avatar philibe commented on July 22, 2024

Ha yes :). I have tried to do that (JS("DataTable.render.percentBar()")) ie like in the datatable net js page, but DataTable.render.percentBar was unknown at this point, after some tries.

it is always possible (..) to add it to the HTML dependencies of the DT table.

Thanks for the tips htmlDependency( "percentageBars") :)

from dt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.