rstudio / pins-r

Pin, discover, and share resources
https://pins.rstudio.com
Other
312 stars 63 forks source link

Feature request: board_github() for updated API #600

Closed justinmillar closed 1 year ago

justinmillar commented 2 years ago

It would be awesome if a future release could get an updated version of register_board_github() for the new API. In our use case, we've been creating data packages that use pins to provide up-to-date data without requiring package re-installation. The Github workflow is nice because we can keep the package code and datasets together within a single repo. Unfortunately board_url() doesn't quite get there since is it read-only, so we're still using the legacy API and are unable to use some of the new features (such as arrow support).

Thanks, and thank you for this excellent package!

juliasilge commented 1 year ago

Thanks for your patience on this issue! Would it work for your use case to:

Reading would look like this (check out the folder here):

library(pins)
folder_on_gh <- "https://raw.githubusercontent.com/rstudio/pins-r/main/tests/testthat/pin-board/"
b <- board_url(folder_on_gh)

b %>% pin_list()
#> [1] "x" "y"
b %>% pin_versions("y")
#> # A tibble: 2 × 3
#>   version                created             hash 
#>   <chr>                  <dttm>              <chr>
#> 1 20221215T180357Z-9ae7a 2022-12-15 11:03:57 9ae7a
#> 2 20221215T180400Z-b81d5 2022-12-15 11:04:00 b81d5

Created on 2022-12-15 with reprex v2.0.2

justinmillar commented 1 year ago

Thanks for picking this up!

Yes, I think this workflow would work great for our use case. Currently we are using pins inside of an internal R package, which has a wrapper function around pin_get() to load in data. The package is stored in a private Github repo, and the pinned datasets are stored in a separate branch within the package repo.

Would it still be possible to write using board_folder() to a private Github repo (assuming proper credentials)?

juliasilge commented 1 year ago

Ah gotcha, an internal R package in a private repo. 👍

You can use board_folder() to write to the private repo (because then the credentials are handled with git push and such) but you can't read via board_url() from the private repo unless you specify the exact path to the YAML and put the token on the URL itself, like this:

library(pins)
## added token here:
folder_on_gh <- "https://raw.githubusercontent.com/juliasilge/pkgpins/main/pkgdown/assets/pins-board/_pins.yaml?token=GHXXXXXXXXXXXXX"
b <- board_url(folder_on_gh)
b %>% pin_list()
#> [1] "mtcars_metric"

Created on 2023-01-09 with reprex v2.0.2

You could put the token in an environment variable and glue::glue() the URL together, but that's probably not as good an option as what we had before in the old interface for GitHub boards.

If you are eager to use Arrow, I would suggest doing this for now. Sounds like in the longer term, we may want to build up some better support for using GitHub with the modern board infrastructure.

juliasilge commented 1 year ago

Maybe we want to add a new authentication slot to board_url() where someone could pass in their own httr::add_headers() object or similar.

juliasilge commented 1 year ago

In #732 we added a new headers argument to make this a bit easier to use and automate. You can install this from the development version right now via remotes::install_github("rstudio/pins-r"). The way it works is that you set up board_url() with wherever your pins are stored, and then pass in authentication details via headers:

library(pins)
folder_on_gh <- "https://raw.githubusercontent.com/juliasilge/pkgpins/main/pkgdown/assets/pins-board/_pins.yaml"
my_headers <- c(Authorization = paste("token", "github_pat_XXXXXXXXX"))
b <- board_url(folder_on_gh, headers = my_headers)
b %>% pin_list()
#> [1] "mtcars_metric"
b %>% pin_read("mtcars_metric")
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#> Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#> Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
#> Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
#> Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
#> Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
#> Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#> Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
#> Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#> Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
#> Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
#> Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
#> Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
#> Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
#> AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#> Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
#> Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
#> Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
#> Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
#> Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
#>                     lper100km
#> Mazda RX4             11.2007
#> Mazda RX4 Wag         11.2007
#> Datsun 710            10.3164
#> Hornet 4 Drive        10.9914
#> Hornet Sportabout     12.5783
#> Valiant               12.9953
#> Duster 360            16.4486
#> Merc 240D              9.6400
#> Merc 230              10.3164
#> Merc 280              12.2508
#> Merc 280C             13.2143
#> Merc 450SE            14.3424
#> Merc 450SL            13.5962
#> Merc 450SLC           15.4747
#> Cadillac Fleetwood    22.6168
#> Lincoln Continental   22.6168
#> Chrysler Imperial     16.0010
#> Fiat 128               7.2597
#> Honda Civic            7.7373
#> Toyota Corolla         6.9385
#> Toyota Corona         10.9402
#> Dodge Challenger      15.1752
#> AMC Javelin           15.4747
#> Camaro Z28            17.6853
#> Pontiac Firebird      12.2508
#> Fiat X1-9              8.6159
#> Porsche 914-2          9.0467
#> Lotus Europa           7.7373
#> Ford Pantera L        14.8870
#> Ferrari Dino          11.9398
#> Maserati Bora         15.6810
#> Volvo 142E            10.9914

Created on 2023-04-14 with reprex v2.0.2

You can put GitHub credentials in your .Renviron and then use something like Sys.getenv("GITHUB_PAT") for scripts, to avoid hard-coding your credentials in your code.

Let us know if you have further questions! 🙌

juliasilge commented 1 year ago

We also added more docs on this here: https://pins.rstudio.com/dev/reference/board_url.html#authentication-for-board-url-

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.