【问题标题】:R loops with JSON API Source使用 JSON API 源的 R 循环
【发布时间】:2016-11-18 21:53:39
【问题描述】:

我正在尝试根据 ISBN 从 API (http://www.knigoed.info/api-prices.html) 获取图书价格数据。

这个想法是向函数提交 ISBN 的向量,以获取包含所有可用信息的数据框(或至少包含来自不同供应商的价格的 Data.Frame)

isbns<- c("9785170922789", "9785170804801", "9785699834174", "9785699717255", "9785170869237")


getISBNprice <- function(ISBN, source="http://www.knigoed.info/api/Prices?code=") {
        pathA <- source
        for (i in 1:length(ISBN)) {

                ISB <- ISBN[i]
                AAA <- paste(pathA, ISB, "&sortPrice=DESC&country=RU", sep="")
                document <- fromJSON(AAA, flatten = FALSE)

                dfp <- document$prices
                dfp <- cbind(dfp,ISB ) 
                # dfp <- cbind(dfp,BookID=document$bookId) 
                # dfp <- cbind(dfp,Title=document$title) 
                # dfp <- cbind(dfp,Author=document$author) 
                # dfp <- cbind(dfp,Publisher=document$publisher)
                # dfp <- cbind(dfp,Series=document$series)
                # dfp <- cbind(dfp,Picture=document$picture)

                if (!exists("AAAA")) {AAAA<- dfp} else {bind_rows(AAAA, dfp) }
        }

        AAAA
}        

但是函数返回错误: 1. 在 bind_rows_(x, .id) 中:不等因子级别:强制转换为字符 2:在 bind_rows_(x, .id) 中:不等因子级别:强制转换为字符 3:在 bind_rows_(x, .id) 中:不等因子级别:强制转换为字符 4:在 bind_rows_(x, .id) 中:不等因子级别:强制转换为字符

【问题讨论】:

    标签: json r api loops dataframe


    【解决方案1】:

    这是最简单的make a list from the start,这将使以后的简化变得更容易。 purrr 包可以使处理列表变得更加容易,尽管如果您愿意,可以将这里的用法替换为 base 的 lapplymapply/Map

    library(purrr)
    
    # Paste is vectorized, so make a list of URLs all at once. 
    # `httr` can make a URL out of a list of named parameters, if it's more convenient.
    results <- paste0("http://www.knigoed.info/api/Prices?code=", 
                      isbns, 
                      "&sortPrice=DESC&country=RU") %>% 
        # Iterate over vector of URLs, using fromJSON to pull and parse the request.
        # map, like lapply, will put the results into a list. 
        map(jsonlite::fromJSON, flatten = FALSE)
    
                # Grab "prices" element of each top-level list element
    results %>% map('prices') %>% 
        # Iterate in parallel (like mapply/Map) over prices and isbns, making a data.frame of
        # each. map2_df will coerce the resulting list of data.frames to a single data.frame.
        map2_df(isbns, ~data.frame(isbn = .y, .x, stringsAsFactors = FALSE)) %>%
        # For pretty printing
        tibble::as_data_frame()
    
    ## # A tibble: 36 x 10
    ##             isbn shopId       name      domain
    ##            <chr>  <chr>      <chr>       <chr>
    ## 1  9785170922789     29    Магистр    booka.ru
    ## 2  9785170922789      3   Лабиринт labirint.ru
    ## 3  9785170922789     20  LitRes.ru   litres.ru
    ## 4  9785170804801     29    Магистр    booka.ru
    ## 5  9785170804801      2    Read.ru     read.ru
    ## 6  9785170804801      3   Лабиринт labirint.ru
    ## 7  9785170804801     63      Эксмо    eksmo.ru
    ## 8  9785170804801      1    OZON.ru     ozon.ru
    ## 9  9785170804801      4 My-shop.ru  my-shop.ru
    ## 10 9785170804801      1    OZON.ru     ozon.ru
    ## # ... with 26 more rows, and 6 more variables: url <chr>, available <lgl>, downloadable <lgl>,
    ## #   priceValue <dbl>, priceSuffix <chr>, year <int>
    

    【讨论】:

    • 非常感谢完美工作的解决方案(非常清晰和详细的 cmets)!会尽我所能学习咕噜声
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2017-02-03
    • 1970-01-01
    • 2017-08-06
    • 2021-06-25
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多