Code Monkey home page Code Monkey logo

Comments (7)

timelyportfolio avatar timelyportfolio commented on June 15, 2024 6

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()

image

from formattable.

renkun-ken avatar renkun-ken commented on June 15, 2024

Thanks! It would be great to support embedding existing htmlwidgets. @timelyportfolio, any ideas?

from formattable.

timelyportfolio avatar timelyportfolio commented on June 15, 2024

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.

timelyportfolio avatar timelyportfolio commented on June 15, 2024

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.

timelyportfolio avatar timelyportfolio commented on June 15, 2024

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

image

from formattable.

pfuhlert avatar pfuhlert commented on June 15, 2024

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)
      )
    }
  )
)

With my desired output:
unbenannt

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.

zx8754 avatar zx8754 commented on June 15, 2024

Relevant post at StackOverflow - Add sparkline graph to a table

from formattable.

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.