使用github操作运行r rmd文件

发布于 2025-02-07 07:29:13 字数 2039 浏览 1 评论 0原文

我有两个存储库每天只需在Rstudio打开项目并编织RMD文件,然后将结果推向存储库,可以每天更新。

我很想通过github动作自动化这一点,但是我发现文档很困惑。

在做这件事时,是否有“像我5岁的人”来解释吗?

因此,我制作了一个bash文件,位于./ bash/data_refresh.sh.sh和一个github操作文件data_refresh.yaml

bash文件:

#!/bin/bash

echo "Rendering the page..."

Rscript -e "rmarkdown::render(input = 'README.Rmd')"

if [[ "$(git status --porcelain)" != "" ]]; then
    git config --global user.name 'my_user_name'
    git config --global user.email 'my_email_address'
    git add *
    git commit -m "Auto update Report"
    git push
fi

这是我的yaml

# This is a basic workflow to help you get started with Actions

name: Package Data Refresh

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    name: refresh the dashboard
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: checkout_repo
        uses: actions/checkout@v2
        with:
          ref:  'master'
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Render Rmarkdown
        run: bash ./bash/data_refresh.sh

我遇到以下错误,不确定如何纠正它。

Run bash ./bash/data_refresh.sh
  bash ./bash/data_refresh.sh
  shell: /usr/bin/bash -e {0}
Rendering the page...
Error in loadNamespace(x) : there is no package called ‘rmarkdown’
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

I have two repositories that I update daily by simply opening the project in RStudio and knitting the RMD file, then pushing the results to the repo.

I would love to automate this with GitHub actions but I find the documentation highly confusing.

Is there an "Explain it like I'm 5" on doing this particular thing?

So I made a bash file located at ./bash/data_refresh.sh and a github actions file data_refresh.yaml

Bash file:

#!/bin/bash

echo "Rendering the page..."

Rscript -e "rmarkdown::render(input = 'README.Rmd')"

if [[ "$(git status --porcelain)" != "" ]]; then
    git config --global user.name 'my_user_name'
    git config --global user.email 'my_email_address'
    git add *
    git commit -m "Auto update Report"
    git push
fi

Here is my yaml:

# This is a basic workflow to help you get started with Actions

name: Package Data Refresh

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    name: refresh the dashboard
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: checkout_repo
        uses: actions/checkout@v2
        with:
          ref:  'master'
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Render Rmarkdown
        run: bash ./bash/data_refresh.sh

I am getting the following error and not sure how to rectify it.

Run bash ./bash/data_refresh.sh
  bash ./bash/data_refresh.sh
  shell: /usr/bin/bash -e {0}
Rendering the page...
Error in loadNamespace(x) : there is no package called ‘rmarkdown’
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2025-02-14 07:29:13

您可以取r-lib 作为起点并添加您的零件(渲染& cron作业)。

当手动触发时,这起作用了,我尚未测试cron作业:

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2

      - uses: r-lib/actions/setup-renv@v2
      
      - name: Render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "[email protected]"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi

这使用renv安装依赖项并缓存GHA中的依赖项(据我了解)。因此,您需要在存储库中使用renv。只需使用renv :: init()并提交结果文件。当您使用.rmd时,这包括rmarkDown。有关更多信息,请检查 renv rentv文档

使其起作用的关键部分是,您必须在参数周围使用rscript的单个引号,而不是双引号。否则它将无法正常工作。

另外,我是GHA的新手,因此您可能还可以优化其他方面。

在此处编辑

是一个无需renv的版本,但是我建议使用renv,因为它允许缓存依赖关系(套件)。

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh 2

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2
      
      - name: Install packages, render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'install.packages(c("knitr", "rmarkdown"))'
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "[email protected]"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi

You can take the workflows from r-lib as a starting point and add your parts (rendering & cron job).

This works when triggered manually, I haven't tested the cron job:

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2

      - uses: r-lib/actions/setup-renv@v2
      
      - name: Render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "[email protected]"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi

This uses renv to install the dependencies and cache the dependencies in GHA (as far as I understand). Therefore, you need to use renv in your repository. Simply use renv::init() and commit the resulting files. As you use .Rmd, this includes rmarkdown. For more information, check the renv documentation.

The crucial part to make it work is that you have to use single quotes around the argument for Rscript, not double quotes. Otherwise it won't work.

Also, I'm quite new to GHA, so there might be other aspects you could optimise.

Edit

Here is a version that works without renv, however I recommend using renv as it allows to cache your dependencies (packages).

# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  schedule:
    - cron: '0  12 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

name: Package Data Refresh 2

jobs:
  render-rmarkdown:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2
      
      - name: Install packages, render Rmarkdown Readme file and Commit Results
        run: |
          echo "Rendering the page..."
          Rscript -e 'install.packages(c("knitr", "rmarkdown"))'
          Rscript -e 'rmarkdown::render(input = "README.Rmd")'
          if [[ "$(git status --porcelain)" != "" ]]; then
            git config --local user.name "$GITHUB_ACTOR"
            git config --local user.email "[email protected]"
            git add *
            git commit -m "Auto update Report"
            git push origin
          fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文