hafen / trelliscopejs

TrelliscopeJS R Package
https://hafen.github.io/trelliscopejs
Other
263 stars 36 forks source link

Any state option should be set-able from R cmd #49

Open schloerke opened 6 years ago

schloerke commented 6 years ago

I have set state information, but it is stomped at startup. I've seen this for any filter and sidebar values.

It would be nice to be allowed to set anything in the state for the default view.


library(gapminder)
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag():    dplyr, stats
library(trelliscopejs)

gapminder %>%
  group_by(country, continent) %>%
  # condense the data
  tidyr::nest() %>%
  print() ->
gapminder_condensed
#> # A tibble: 142 x 3
#>        country continent              data
#>         <fctr>    <fctr>            <list>
#>  1 Afghanistan      Asia <tibble [12 x 4]>
#>  2     Albania    Europe <tibble [12 x 4]>
#>  3     Algeria    Africa <tibble [12 x 4]>
#>  4      Angola    Africa <tibble [12 x 4]>
#>  5   Argentina  Americas <tibble [12 x 4]>
#>  6   Australia   Oceania <tibble [12 x 4]>
#>  7     Austria    Europe <tibble [12 x 4]>
#>  8     Bahrain      Asia <tibble [12 x 4]>
#>  9  Bangladesh      Asia <tibble [12 x 4]>
#> 10     Belgium    Europe <tibble [12 x 4]>
#> # ... with 132 more rows

gapminder_condensed %>%
  # add metrics and plots
  mutate(
    # for every subset,
    min_lifeExp = purrr::map_dbl(data, function(dt) min(dt$lifeExp)),
    max_lifeExp = purrr::map_dbl(data, function(dt) max(dt$lifeExp)),
    panel = map_plot(data, function(dt) {
      # display a line plot of X:year, Y:life expectancy
      ggplot(dt, aes(year, lifeExp)) + geom_line() + ylim(20, 85)
    })
  ) %>%
  # remove the condensed data
  select(-data) %>%
  print() ->
gap_trellis
#> # A tibble: 142 x 5
#>        country continent min_lifeExp max_lifeExp    panel
#>         <fctr>    <fctr>       <dbl>       <dbl>   <list>
#>  1 Afghanistan      Asia      28.801      43.828 <S3: gg>
#>  2     Albania    Europe      55.230      76.423 <S3: gg>
#>  3     Algeria    Africa      43.077      72.301 <S3: gg>
#>  4      Angola    Africa      30.015      42.731 <S3: gg>
#>  5   Argentina  Americas      62.485      75.320 <S3: gg>
#>  6   Australia   Oceania      69.120      81.235 <S3: gg>
#>  7     Austria    Europe      66.800      79.829 <S3: gg>
#>  8     Bahrain      Asia      50.939      75.635 <S3: gg>
#>  9  Bangladesh      Asia      37.484      64.062 <S3: gg>
#> 10     Belgium    Europe      68.000      79.441 <S3: gg>
#> # ... with 132 more rows

gap_trellis %>%
  trelliscope(
    "gapminder_filter", nrow = 3, ncol = 5,
    self_contained = TRUE,
    state = list(
      filter = list(
        state = list(
          max_lifeExp = list(
            name = "max_lifeExp",
            type = "range",
            varType = "numeric",
            value = list(to = 70),
            valid = TRUE
          ),
          view = list(
            active = list("max_lifeExp"),
            inactive = list("country", "continent", "min_lifeExp")
          )))))