Comments (5)
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)
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> '",
" +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)
from dt.
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.
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)
Looks good.
from dt.
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)
from dt.
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)
- datatable column filters clash with shiny::selectizeInput when invoked first HOT 4
- DateTime date picker CSS is broken in SearchBuilder
- Disabling editable table by row (and not just by column)
- autoFill extension returns `NaN` instead of incrementing HOT 5
- Docs do not state whether shift+click works for multi-row selection
- #828 breaks maintaining selected columns upon re-render HOT 3
- signif() + datatable() bug HOT 3
- `renderDT()` evaluates `...` in `expr`'s `env`
- Unable to test `renderDT()` in a `MockShinySession`
- First option entry ignored for default search values per column HOT 3
- "locked" proxy structure not accommodating new data HOT 4
- Extract search functionality
- upgrade to datatables v2 HOT 2
- R Shiny application designed to navigate through multiple sections within a single page
- Utilizing Scroller DataTable extension with `datatable(fillContainer = TRUE, ...)`
- Upgrade DataTables to v1.13.11 HOT 2
- Interaction between searching options parameter and `datatable::filter` HOT 2
- Possible bug: styleRow() appears to do nothing when viewed in browser
- Low text contrast with pkgdown dark theme HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dt.