Comments (7)
Not sure how @renkun-ken feels about adding sparkline
integration to formattable
proper, but I thought I could just continue this discussion into a clean interface for sparklines
inclusion. Here is a simple example to demonstrate one way to include sparklines
. We can leverage dplyr
groups
.
#devtools::install_github("timelyportfolio/sparkline@update/sparkline")
library(sparkline)
library(formattable)
library(dplyr)
iris %>%
group_by(Species) %>%
summarize_all(list) %>%
formattable(
formatters = list(
area(col=2:5) ~ function(x){
lapply(x,function(xx){
as.character(as.tags(
sparkline(xx,type="box",chartRangeMin=0,chartRangeMax=max(unlist((x))))
))
})
}
),
table.attr="class='table table-condensed table-striped'"
) %>%
formattable:::as.htmlwidget() %>%
htmlwidgets:::as.tags.htmlwidget() %>%
tagList() %>%
attachDependencies(
htmlwidgets:::widget_dependencies("sparkline","sparkline")
) %>%
browsable()
from formattable.
Thanks! It would be great to support embedding existing htmlwidgets. @timelyportfolio, any ideas?
from formattable.
That is definitely doable, but I struggle with how @pfuhlert data might look, since a sparkline generally requires multiple data.points whereas a table/data.frame
represents a single tuple. A use case would be very helpful. I'll try to think through/imagine a situation to illustrate.
from formattable.
For instance with a vector
, we could do
formattable( 1:10, formatter=function(x) sparkline(x,type="box") )
but that isn't helpful, since we could just as easily
sparkline(1:10,type="box")
from formattable.
Mangling the sparkline
example a little bit, maybe this is something like what you want.
---
title: "jquery Sparkline htmlwidget in formattable"
author: "Kent Russell"
date: "June 10, 2015"
output: html_document
---
```{r results = 'asis'}
library(formattable)
library(sparkline)
set.seed(1234)
x = rnorm(10)
y = rnorm(10)
df = data.frame(id=c("x","y"),"sparkline"=NA, "sparkbox" = NA)
formattable(
df,
list(
"sparkline"=function(z){
sapply(
paste0(text='`r sparkline(',df$id,')`')
,function(md) knitr::knit(text=md, quiet=T)
)
}
,sparkbox =function(z){
sapply(
paste0(text='`r sparkline(',df$id,',type="box")`')
,function(md) knitr::knit(text=md, quiet=T)
)
}
)
)
```
with this result
from formattable.
Hi and thanks for your great help. It has already helped me a lot already!
What I had in mind was something like the following:
library(formattable)
library(sparkline)
library(dplyr)
# So lets say I have this data frame and I now want to show the Names
# and a Sparkline
df <- data.frame(Name = LETTERS[1:5], Name2 = letters[5:1],
RankMonth1 = sample(5), RankMonth2 = sample(5),
RankMonth3 = sample(5), RankMonth4 = sample(5),
RankMonth5 = sample(5))
A <- as.numeric(df[1,3:7])
B <- as.numeric(df[2, 3:7])
C <- as.numeric(df[3, 3:7])
D <- as.numeric(df[4, 3:7])
E <- as.numeric(df[5, 3:7])
df2 <- data.frame(Name = df$Name, Name2 = df$Name2,
Rank = NA)
formattable(
df2,
list(
"Rank"=function(z){
sapply(
paste0(text='`r sparkline(', df$Name, ')`')
,function(md) knitr::knit(text=md, quiet=T)
)
}
)
)
The only thing that bothers me for now is the Referencing of A B C D E which is of course not very generic. Do you have any idea how to optimize this?
from formattable.
Relevant post at StackOverflow - Add sparkline graph to a table
from formattable.
Related Issues (20)
- Embed hyperlink into Glyphicon HOT 3
- Support fontawesome HOT 1
- Feature Request: easy creation of image columns HOT 2
- Make formattable work with promises-package HOT 3
- Warning: Error in writeImpl: Text to be written must be a length-one character vector HOT 5
- Extract vector classes to a separate package?
- latex output
- Retire Travis and AppVeyor HOT 9
- Change header text by icontext
- invisible white text when rendering a table in knitr chunk HOT 3
- Update NEWS and create tag HOT 2
- Feature Request - Implement vctrs methods to support tidyr
- digit number HOT 2
- Is there a way to save formattable to pure html, not through htmlwidgets?
- Does formattable has similar function as `merge_v` in `flextable`?
- Formattable + DT Sorting Issue with multi colour bars.
- No formatted table produced HOT 4
- Use of hex codes within style() HOT 1
- conflict with patchwork HOT 2
- replace glyphicons with FontAwesome ? HOT 1
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 formattable.