Here is how to read a file from a private Github repo
Note that this method requires you to send your GitHub Personal Access Token (PAT) to Shinyapps. Therefore we will use a fine-grained token to limit access to only the repo that contains the data.
Create a private Github repo and place your .csv file there. The data file in my example code is My_CSV.csv.
Generate a fine-grained GitHub Personal Access Token (PAT) for the repo(s):
a. Go to your Github account settings (not repo settings): Settings --> Developer Settings --> Personal Access Token --> Fine-Grained Token --> Generate New Token
b. If you have 2FA set up you may have to enter a 2FA code here.
c. Name the token, choose the expiration date, and select the repo(s) that the token grants access to.
d. Select the appropriate permissions (since I only READ data from my repo, under Repository Permissions I set Actions and Contents to "read-only").
e. Click Generate Token and copy it.
If you don't have an .Renviron file in your Shiny app folder, create one. This is simply a text file called .Renviron.
Open the .Renviron file and add this line to it, pasting in your own generated PAT:
GITHUB_PAT=github_pat_78s6fads6f7asd6f7a6sd767sd6f7as6d7fasd7f7asd76f7adf67
Deploy your app including the .Renviron file.
Use this code in your app to authenticate and load your data:
library(readr) # Or not, read.csv() can be substituted for read_csv()
library(httr)
# Create path to Github file
repo_owner <- "My_Github_Name" # Put the owner of the Repo here
repo_name <- "My_Repo_Name" # Put the name of the Repo here
branch <- "main" # Put the Repo branch here
repo_file <- "My_CSV.csv" # The name of the .csv file you want
url <- paste0(
"https://raw.githubusercontent.com/",
repo_owner, "/", repo_name, "/", branch, "/", repo_file
)
# Get github PAT from .Renviron (!!make sure you include the .Renviron file in your push to shinyapps.io!!)
github_pat <- Sys.getenv("GITHUB_PAT")
# Request content from Github, use the PAT for authentication
# The .csv fill will be returned in response$content
response <- GET(
url,
authenticate(user = "", password = github_pat)
)
# Check for errors and assign response$content to a variable
# If you're reading e.g. images, you can use `raw` in place of `text` and the proper function that reads images
if (response$status_code == 200) {
data <- read_csv(content(response, "text"))
} else {
stop("Failed to fetch data: ", response$status_code)
}
PINSpackage to post the data (csv to rda) toPosit_Board(), and your app should use the data in thePosit_Board(). This way you can update the data in thePosit_Board()whenever you need to and the app uses the latest data. No need to redeploy.