Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] is there a better way to do this in rlist package? #112

Open
happyshows opened this issue Jan 8, 2016 · 1 comment
Open

[Question] is there a better way to do this in rlist package? #112

happyshows opened this issue Jan 8, 2016 · 1 comment

Comments

@happyshows
Copy link

Hi,

I was experimenting on d3radar package and encountered the following transformation need. I'm wondering if rlist has a more efficient way of achieving the same?
The approach might have wider application as it's json like.

Thanks.

mtcars %>%
add_rownames( var = "key" ) %>%
mutate_each(funs(rescale), -key) %>%
#limit to 8 rows for manageability
head(8) %>%
by_row(
function(row){
list(
key = row[["key"]],
values = lapply(
names(row)[-match("key",names(row))],
function(nm){
list(
"axis" = nm,
"value" = row[[nm]]
)
}
)
)
}
)

http://www.buildingwidgets.com/blog/2015/12/9/week-49-d3radarr

@timelyportfolio
Copy link
Contributor

I am sure that @renkun-ken probably has a more elegant answer, but here is my first way to do in rlist.

library(dplyr)
library(scales)
library(rlist)
library(d3radarR)

mtcars %>%
  add_rownames( var = "key" ) %>%
  mutate_each(funs(rescale), -key) %>%
  head(8) %>%
  list.parse() %>%
  list.map(
    list(
      "key"=key,
      values=lapply(
        names(.)[-match("key",names(.))],
        function(nm){
          list(
            "axis" = nm,
            "value" = .[[nm]]
          )
        }
      )
    )
  ) %>%
  unname() %>%
  d3radar()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants