Skip to content

Commit

Permalink
#29 improve the complex do{} example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Stanley committed Apr 6, 2015
1 parent fdc23a4 commit c73a06c
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions vignettes/introduction-to-tidyjson.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ can parse the JSON:
```{r}
library(jsonlite)
# Parse the JSON into a data.frame
purch_df <- jsonlite::fromJSON(purch_json)
purch_df <- jsonlite::fromJSON(purch_json, simplifyDataFrame = TRUE)
# Examine results
purch_df
```
Expand All @@ -166,18 +166,14 @@ Reasoning about code like this is nearly impossible, and further, the relational
structure of the data is lost (we no longer have the name of the user).

We can instead try to use dplyr and the `do{}` operator to get at the
data in the nested data.frames, but this is equally challenging:
data in the nested data.frames, but this is equally challenging and confusing:

```{r}
purch_df %>% group_by(name) %>%
do(
data.frame(
name = .$name,
items = .$purchases[[1]] %>% rowwise %>% do({.$items}),
stringsAsFactors = FALSE
)
) %>%
summarize(price = sum(items.price))
purch_df %>% group_by(name) %>% do({
.$purchases[[1]] %>% rowwise %>% do({
.$items[, "price", drop = FALSE]
})
}) %>% summarize(price = sum(price))
```

Using tidyjson, we can build a pipeline to turn this JSON into a tidy data.frame
Expand Down

0 comments on commit c73a06c

Please sign in to comment.