1

I have an R Shiny application that currently draws on a .csv file as its main data source. That file sits in the app's folder, so that when I deploy the app to shinyapps.io with RSConnect, it uploads with it. The problem is that I then have to redeploy the entire app if I have any updates to the back-end data file.

A workaround I'm thinking SHOULD work is to make the read.csv line in the setup of the app's code look to a csv file I have pushed to a GitHub repository. Where I have come up short in my own online research is that the repo needs to be Private, so I need to pass some sort of authentication with it. But all of what I can find online is for downloading whole packages from GitHub and stuff like that, not reading single files.

And I know you can just read.csv("raw file link") for public repos, but when I do that and try to deploy to shinyapps.io, the app just crashes because it says it can't authenticate.

I have also already allowed authentication/access to GitHub from shinyapps.io AND to shinyapps.io from GitHub. Hoping I'm just missing a stored token or something of that nature.

3
  • 1
    Try to use PINS package to post the data (csv to rda) to Posit_Board(), and your app should use the data in the Posit_Board(). This way you can update the data in the Posit_Board() whenever you need to and the app uses the latest data. No need to redeploy. Commented Dec 3, 2024 at 19:44
  • Unfortunately, after looking into it, that would be awesome except that it requires a Posit Connect subscription, which we do not have. I saw a free way would be to set it up with Google drive, but I'm trying to avoid that because I've had issues in the past with Drive and the API dying after getting hit too many times too quickly. Is there a free option somewhere in that package? Commented Dec 6, 2024 at 15:58
  • Yes, it does require Posit Connect subscription. I am not aware of any free option. Commented Dec 6, 2024 at 16:03

1 Answer 1

0

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.

  1. Create a private Github repo and place your .csv file there. The data file in my example code is My_CSV.csv.

  2. 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.

  3. If you don't have an .Renviron file in your Shiny app folder, create one. This is simply a text file called .Renviron.

  4. Open the .Renviron file and add this line to it, pasting in your own generated PAT: GITHUB_PAT=github_pat_78s6fads6f7asd6f7a6sd767sd6f7as6d7fasd7f7asd76f7adf67

  5. 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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.