如何为shinyapps.io上的R Shiny应用实现每周数据自动刷新发布
Great question! I’ve tackled exactly this scenario for client projects where local execution isn’t feasible. Here are two robust solutions to automate your weekly CSV refresh and app redeployment:
Solution 1: GitHub Actions + rsconnect Package
This is the most straightforward approach if you’re already using version control (and you should be for client work!). GitHub Actions lets you run scripts remotely on a schedule, no local machine required.
Step-by-Step Setup
Push all code to a GitHub repo
- Include your data-processing script (the one that pulls/merges/cleans data from databases), your Shiny app files (
global.R,ui.R,server.R), and make sure the CSV output path in your data script matches the pathglobal.Rreads from.
- Include your data-processing script (the one that pulls/merges/cleans data from databases), your Shiny app files (
Grab your shinyapps.io authentication credentials
- Log into your shinyapps.io account, go to Account > Tokens, and create a new token. Copy the account name, token, and secret—you’ll need these for GitHub Secrets.
Add secrets to your GitHub repo
- In your repo, go to Settings > Secrets and variables > Actions > New repository secret. Add these three secrets:
SHINYAPPS_ACCOUNT: Your shinyapps.io account nameSHINYAPPS_TOKEN: The token you copiedSHINYAPPS_SECRET: The secret you copied
- Also add any database credentials (e.g.,
DB_USER,DB_PASSWORD,DB_HOST) as secrets here—never hardcode sensitive info!
- In your repo, go to Settings > Secrets and variables > Actions > New repository secret. Add these three secrets:
Create a GitHub Actions workflow file
- In your repo, make a new directory:
.github/workflows/ - Create a file called
weekly-refresh.ymlwith this code (adjust the schedule, R version, and package list to match your needs):name: Weekly Data Refresh & App Deploy on: schedule: # Runs every Sunday at 2 AM UTC (adjust time zone as needed) - cron: '0 2 * * 0' # Add manual trigger for testing workflow_dispatch: jobs: deploy: runs-on: ubuntu-latest steps: - name: Check out repo uses: actions/checkout@v4 - name: Set up R uses: r-lib/actions/setup-r@v2 with: r-version: '4.3.1' # Use your app's R version - name: Install required packages run: | install.packages(c("rsconnect", "dplyr", "DBI", "your-db-driver")) # Add all packages your data script uses shell: Rscript {0} - name: Run data processing script run: | # Fetch database credentials from GitHub Secrets db_user <- Sys.getenv("DB_USER") db_pass <- Sys.getenv("DB_PASSWORD") db_host <- Sys.getenv("DB_HOST") # Run your data script (adjust path if needed) source("path/to/your/data-script.R") shell: Rscript {0} env: DB_USER: ${{ secrets.DB_USER }} DB_PASSWORD: ${{ secrets.DB_PASSWORD }} DB_HOST: ${{ secrets.DB_HOST }} - name: Deploy to shinyapps.io run: | rsconnect::setAccountInfo( name = Sys.getenv("SHINYAPPS_ACCOUNT"), token = Sys.getenv("SHINYAPPS_TOKEN"), secret = Sys.getenv("SHINYAPPS_SECRET") ) rsconnect::deployApp( appDir = ".", # Path to your Shiny app (root of repo if app is here) forceUpdate = TRUE, logLevel = "verbose" ) shell: Rscript {0} env: SHINYAPPS_ACCOUNT: ${{ secrets.SHINYAPPS_ACCOUNT }} SHINYAPPS_TOKEN: ${{ secrets.SHINYAPPS_TOKEN }} SHINYAPPS_SECRET: ${{ secrets.SHINYAPPS_SECRET }}
- In your repo, make a new directory:
Test the workflow
- Use the
workflow_dispatchtrigger in GitHub Actions to run the workflow manually first. Check the logs to make sure the data script runs without errors, the CSV is generated, and the app deploys successfully.
- Use the
Solution 2: Posit Connect (Enterprise-Grade Alternative)
If your client is open to upgrading from shinyapps.io to Posit Connect (Posit’s enterprise Shiny hosting platform), this is even simpler. Posit Connect has built-in scheduled jobs that can:
- Run your data-processing script on a weekly schedule to update the CSV
- Automatically redeploy the Shiny app whenever the CSV file changes (or trigger a redeployment as part of the scheduled job)
This removes the need for GitHub Actions entirely, as all scheduling and deployment is managed within Posit Connect’s web interface. It’s a great option if the client needs more control over hosting and automation.
Key Notes for Both Solutions
- Security first: Always store sensitive credentials (database access, shinyapps.io tokens) in secrets/variables—never commit them to your repo.
- Version control: Keep all code (data script, Shiny app) in version control so you can track changes and roll back if needed.
- Error alerts: Add email/Slack alerts to your GitHub Actions workflow or Posit Connect job so you’re notified if the refresh/deployment fails.
内容的提问来源于stack exchange,提问作者user6883405

