glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
633 stars 80 forks source link

Can sparkline plot in every cells has its own cell range #10

Closed shangguandong1996 closed 4 years ago

shangguandong1996 commented 4 years ago

just like the example code

library(dplyr)
library(sparkline)

data <- chickwts %>%
  group_by(feed) %>%
  summarise(weight = list(weight)) %>%
  mutate(boxplot = NA, sparkline = NA)

reactable(data, columns = list(
  weight = colDef(cell = function(values) {
    sparkline(values, type = "bar")
  }),
  boxplot = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]], type = "box")
  }),
  sparkline = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]])
  })
))

And I noticed the plot y-axis range is according to the data column min and max. So I am wondering whether can set its own range just like ggplot2 facet

Best wisher And Thanks for this awesome package.

Guandong Shang

glin commented 4 years ago

I think sparkline supports passing in any of the options from https://omnipotent.net/jquery.sparkline/#common to sparkline(), like chartRangeMin and chartRangeMax.

So for example, you can set a y-axis min and max like this:

sparkline(chickwts$weight[1:10], chartRangeMin = 0, chartRangeMax = 250, width = 300, height = 70)
shangguandong1996 commented 4 years ago

I think sparkline supports passing in any of the options from https://omnipotent.net/jquery.sparkline/#common to sparkline(), like chartRangeMin and chartRangeMax.

So for example, you can set a y-axis min and max like this:

sparkline(chickwts$weight[1:10], chartRangeMin = 0, chartRangeMax = 250, width = 300, height = 70)

cool! I will try it.

shangguandong1996 commented 4 years ago

I indeed set the range from the chartRangeMax = 250, width = 300. But I failed to set the range accodring to its own cell range using this code. Am I misundering the function……

reactable(data, columns = list(
  weight = colDef(cell = function(values) {
    sparkline(values, type = "bar",chartRangeMin = min(values), chartRangeMax = max(values))
  }),
  boxplot = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]], type = "box")
  }),
  sparkline = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]])
  })
))
glin commented 4 years ago

I think you're using chartRangeMin and chartRangeMax right, but are you trying to set the same y-min and y-max for each bar chart in the column? If so, you'll need the min and max of the entire column, rather than just the single cell.

In this example, it would be the min and max of the chickwts weights:

reactable(data, columns = list(
  weight = colDef(cell = function(values) {
    sparkline(values, type = "bar", chartRangeMin = min(chickwts$weight), chartRangeMax = max(chickwts$weight))
  }),
  boxplot = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]], type = "box")
  }),
  sparkline = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]], chartRangeMin = min(chickwts$weight), chartRangeMax = max(chickwts$weight))
  })
))

screenshot of table

Let me know if this is different from what you're trying to achieve.

shangguandong1996 commented 4 years ago

Thanks for your reply! Actually, chartRangeMin = min(chickwts$weight) is indeed what I want. I am sorry I make a wrong issue. Thanks for your finally plot a right solution :)

Best wishes.