worldbank / pipr

R client to the PIP API
https://worldbank.github.io/pipr
Other
13 stars 4 forks source link

get list of clickable auxiliary tables #44

Closed randrescastaneda closed 1 year ago

randrescastaneda commented 1 year ago

hi @tonyfujs ,

This functions is part of some of the features that are available in the Stata wrapper but are still not available in the R wrapper. Basically what what this does is that, instead of just displaying a data frame with the names of the auxiliary tables, it will display a clickable list that will retrieve the table that the user wants. The argument assign_tb is FALSE by default, so it won't to anything to the .pip env but it is character or TRUE it will create an object with the desired auxiliary table in the .pip env. We can talk about it in more detail next week. For now, the package is working as before. This addition does not modify in any sense the output or performance of the get_aux() function.

the .pip environment is hidden environment, child to the Global Env, in which we can store any pip object. I have several ideas of why this could extremely useful, specially for advanced users.

Best,

randrescastaneda commented 1 year ago

Task linked: CU-2wuyw3f Display list of auxiliary tables and load by clicking

randrescastaneda commented 1 year ago

Hi @tonyfujs ,

I Improved the functionalities of these features, following the recommendation of Hadley Wickham in here. It passed all the checks.

Best,

randrescastaneda commented 1 year ago

HI @tonyfujs ,

Thank you so much for your comment. I apologize for not including a more detailed explanation in my PR. I am planning to write a vignette about this feature, but for now, this is briefly how it works and the rational behind.

The main objective of display_aux() is to show the user all the auxiliary tables available in pip and giving them the ability to download them by just clicking on their names (ala Stata). Given that, on the one hand, you need to bind any object to a name to keep it in the environment and, on the other, R CMD check complains when you assign something to the global environment, I needed to find a way to make the object accessible to the user.

So, the solution was the following: I added the option assign_tb to get_aux(), which, If TRUE, assigns the requested auxiliary table to the .pip environment. If FALSE, it behaves as usual. So, display_aux() is just a wrapper around get_aux() with assign_tb = TRUE. Now, what I did not mention in my PR is that I added the function call_aux() to call the tables in .pip. In this way, you have the best of both words:

  1. You can download aux tables by only clicking on their names (ala Stata).
  2. You don't mess up with the global env
  3. you can call the tables you downloaded with call_aux()
  4. The assign_tb option and call_aux are a powerful idea for developers who need to keep their environments clean.
    
    library(pipr)

display_aux()

>

> -- Click on any of the tables below --

>

> aux versions

> countries

> country coverage

> country list

> cpi

> decomposition

> dictionary

> framework

> gdp

> incgrp coverage

> indicators

> interpolated means

> missing data

> pce

> pop

> pop region

> poverty lines

> ppp

> region coverage

> regions

> survey means

this simulates a display_aux() call

get_aux("gdp", assign_tb = TRUE, force = TRUE)

now you can call it

call_aux("gdp")

> # A tibble: 219 x 46

> country~1 data~2 1977 1978 1979 1980 1981 1982 1983 1984

>

> 1 ABW nation~ NA NA NA NA NA NA NA NA

> 2 AFG nation~ 405. 424. 405. 404. 453. 503. 534. 530.

> 3 AGO nation~ 3476. 3545. 3538. 3550. 3276. 3162. 3179. 3252.

> 4 ALB nation~ 1713. 1720. 1729. 1741. 1804. 1818. 1800. 1740.

> 5 AND nation~ 36082. 35552. 34462. 34014. 32669. 31709. 30897. 30085.

> 6 ARE nation~ 104937. 90890. 98553. 111574. 108563. 95338. 86109. 85033.

> 7 ARG nation~ 10104. 9506. 10321. 10318. 9630. 9408. 9660. 9655.

> 8 ARM nation~ NA NA NA 1586. 1630. 1674. 1684. 1738.

> 9 ASM nation~ NA NA NA NA NA NA NA NA

> 10 ATG nation~ 5570. 5826. 6306. 6830. 7099. 7094. 7475. 8234.

> # ... with 209 more rows, 36 more variables: 1985 , 1986 ,

> # 1987 , 1988 , 1989 , 1990 , 1991 ,

> # 1992 , 1993 , 1994 , 1995 , 1996 ,

> # 1997 , 1998 , 1999 , 2000 , 2001 ,

> # 2002 , 2003 , 2004 , 2005 , 2006 ,

> # 2007 , 2008 , 2009 , 2010 , 2011 ,

> # 2012 , 2013 , 2014 , 2015 , 2016 , ...

for developers who may need several tables

tb <- c("cpi", "ppp", "pop") lapply(tb, get_aux, assign_tb = TRUE, force = TRUE)

> [[1]]

> cpi

> TRUE

>

> [[2]]

> ppp

> TRUE

>

> [[3]]

> pop

> TRUE

call_aux()

> [1] "gdp" "ppp" "cpi" "pop"

call_aux("pop")

> # A tibble: 650 x 46

> country_code data_l~1 1977 1978 1979 1980 1981 1982 1983 1984

>

> 1 ABW national 6.04e4 6.01e4 6.00e4 6.01e4 6.06e4 6.13e4 6.22e4 6.28e4

> 2 ABW rural 2.99e4 2.97e4 2.97e4 2.98e4 3.00e4 3.04e4 3.08e4 3.12e4

> 3 ABW urban 3.05e4 3.04e4 3.03e4 3.03e4 3.06e4 3.09e4 3.14e4 3.17e4

> 4 AFG national 1.32e7 1.33e7 1.34e7 1.34e7 1.32e7 1.29e7 1.25e7 1.22e7

> 5 AFG rural 1.13e7 1.13e7 1.13e7 1.12e7 1.10e7 1.07e7 1.03e7 9.96e6

> 6 AFG urban 1.91e6 2.00e6 2.07e6 2.14e6 2.18e6 2.21e6 2.23e6 2.24e6

> 7 AGO national 7.53e6 7.79e6 8.06e6 8.34e6 8.64e6 8.95e6 9.28e6 9.61e6

> 8 AGO rural 5.95e6 6.07e6 6.19e6 6.31e6 6.44e6 6.57e6 6.70e6 6.82e6

> 9 AGO urban 1.59e6 1.72e6 1.87e6 2.03e6 2.20e6 2.38e6 2.58e6 2.79e6

> 10 ALB national 2.51e6 2.57e6 2.62e6 2.67e6 2.73e6 2.78e6 2.84e6 2.90e6

> # ... with 640 more rows, 36 more variables: 1985 , 1986 ,

> # 1987 , 1988 , 1989 , 1990 , 1991 ,

> # 1992 , 1993 , 1994 , 1995 , 1996 ,

> # 1997 , 1998 , 1999 , 2000 , 2001 ,

> # 2002 , 2003 , 2004 , 2005 , 2006 ,

> # 2007 , 2008 , 2009 , 2010 , 2011 ,

> # 2012 , 2013 , 2014 , 2015 , 2016 , ...



<sup>Created on 2023-02-03 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
randrescastaneda commented 1 year ago

Hi @tonyfujs ,

As agreed, I have updated the messages of display_aux() and call_aux() so that the user knows what to do and I have drafted a short vignette explaining how to use these new features and the .pip env. Please, let me know if you have any question. Best,