为什么在R中使用tidyjson包,bind_rows和mutate一起发现对象'..JSON'错误?

时间:2017-05-09 11:40:34

标签: json r dplyr

在以下示例中,是否有人知道为什么使用tidyjson包,bind_rows()和mutate()会发生错误?

library(tidyjson)
library(dplyr)

# Define a simple JSON array
people <- '
[
    {
    "name": "bob",
    "age": 32
    }, 
    {
    "name": "susan", 
    "age": 54
    }
]'

# Structure the data
people_df <- people %>%
    gather_array %>%
    spread_values(
        name = jstring("name"),
        age = jnumber("age"))

同时使用bind_rows()mutate()似乎会导致错误:

people_df2 <- people_df %>%
    bind_rows(people_df) %>%
    mutate(city = "toronto")

输出:

Error in eval(expr, envir, enclos) : object '..JSON' not found

但是,仅使用bind_rows()mutate()或添加as_data_frame()作品:

people_df2 <- people_df %>%
    bind_rows(people_df)

people_df2 <- people_df %>%
    mutate(city = "toronto")

people_df2 <- people_df %>%
    as_data_frame() %>%
    bind_rows(people_df) %>%
    mutate(city = "toronto")

1 个答案:

答案 0 :(得分:0)

为清楚起见,使用data_frame转换为as_data_frame()是解决问题的方法:

people_df2 <- people_df %>%
    as_data_frame() %>%
    bind_rows(people_df) %>%
    mutate(city = "toronto")

看起来似乎不需要