R 处理JSON文件并将其下载到特定文件夹中

R 处理JSON文件并将其下载到特定文件夹中,r,purrr,R,Purrr,我有一些数据看起来像: index title 1 2.1.1 Indic

我有一些数据看起来像:

  index                                                                                                                          title
1 2.1.1                                                                                           Indicadores de renta media y mediana
2 2.1.2                                                                                            Distribución por fuente de ingresos
3 2.1.3                  Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo
4 2.1.4 Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y tramos de edad
5 2.1.5   Porcentaje de población con ingresos por unidad de consumo por debajo de determinados umbrales fijos por sexo y nacionalidad
6 2.1.6       Porcentaje de población con ingresos por unidad de consumo por debajo/encima de determinados umbrales relativos por sexo
                                             link provincia                            provincesFolderLocations
1 https://www.ine.es/jaxiT3/Tabla.htm?t=30656&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
2 https://www.ine.es/jaxiT3/Tabla.htm?t=30813&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
3 https://www.ine.es/jaxiT3/Tabla.htm?t=30657&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
4 https://www.ine.es/jaxiT3/Tabla.htm?t=30659&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
5 https://www.ine.es/jaxiT3/Tabla.htm?t=30660&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
6 https://www.ine.es/jaxiT3/Tabla.htm?t=30661&L=0  Albacete /home/bscuser/Escritorio/mobility/data/INE/Albacete
                                                          json_link
1 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30656?tip=AM&
2 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30813?tip=AM&
3 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30657?tip=AM&
4 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30659?tip=AM&
5 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30660?tip=AM&
6 https://servicios.ine.es/wstempus/js/es/DATOS_TABLA/30661?tip=AM&
我正在尝试使用
map2
对其中两列进行
map
。我想转到
json\u link
列中的链接,并使用
jsonlite
包中的
fromJSON
读取json文件。然后我想将该文件保存到
provincesFolderLocations
列中指定的位置

我想到了类似于以下的事情(这不起作用):

我可以使用以下方法获取单个JSON文件:

(收集每次观察大约需要一分钟)

然后将列绑定到gether

jsonLINK <- DATA$json_link[4]
JSONin <- fromJSON(jsonLINK)
DATA_Final <- bind_cols(JSONin, DATA[4, ])

jsonLINK提供在
write\u json
-

downloadAndStoreJSONData <- function(jsonLink, provinceFolderLoc){
  JSON_in = fromJSON(jsonLink)
  write_json(JSON_in, paste0(sub('\\?.*', '', basename(jsonLink)), '.json'))
}

purrr::map2(DATA$json_link, DATA$provincesFolderLocations, downloadAndStoreJSONData)
jsonLINK <- DATA$json_link[4]
JSONin <- fromJSON(jsonLINK)
DATA_Final <- bind_cols(JSONin, DATA[4, ])
downloadAndStoreJSONData <- function(jsonLink, provinceFolderLoc){
  JSON_in = fromJSON(jsonLink)
  write_json(JSON_in, paste0(sub('\\?.*', '', basename(jsonLink)), '.json'))
}

purrr::map2(DATA$json_link, DATA$provincesFolderLocations, downloadAndStoreJSONData)
paste0(sub('\\?.*', '', basename(DATA$json_link)), '.json')
#[1] "30656.json" "30813.json" "30657.json" "30659.json" "30660.json" "30661.json"