Skip to content

Commit

Permalink
add R example
Browse files Browse the repository at this point in the history
  • Loading branch information
MeWu-IDM committed Sep 24, 2024
1 parent 1058724 commit 3e26d0e
Show file tree
Hide file tree
Showing 8 changed files with 815 additions and 1 deletion.
11 changes: 10 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
// "mounts": [
// "source=${localWorkspaceFolder},target=/workspaces/${localWorkspaceFolder},type=bind,consistency=cached"
// ],
"postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",
"portsAttributes": {
"8888": {
"label": "Jupyter Lab",
"onAutoForward": "always",
},
"8787": {
"label": "R studio",
"onAutoForward": "always"
}
},
"forwardPorts": [8787, 8888]
}
3 changes: 3 additions & 0 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ if [ -f "R/renv.lock" ]; then
cd R && R -e 'renv::restore()'
fi

# change the default working directory for RStudio
echo "session-default-working-dir=$localworkspace" >> /etc/rstudio/rsession.conf

rstudio-server start && jupyter lab --ip=0.0.0.0 --no-browser --allow-root --NotebookApp.token=''

echo "Dev environment setup completed!"
99 changes: 99 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Render Quarto Files and Deploy to GitHub Pages

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch: # Manual trigger

jobs:
render_quarto:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build Docker image
run: |
docker build -t demo-quarto-image .
- name: Run Quarto in Docker container
run: |
docker run --rm -v ${{ github.workspace }}:/workspace -w /workspace my-quarto-image bash -c "
echo 'Creating gallery directory...'
mkdir -p gallery && \
echo 'gallery directory created.'
FOLDERS=(python, R)
echo 'Folders to process: ${FOLDERS[@]}'
for FOLDER in \"\${FOLDERS[@]}\"; do
echo 'Searching for .qmd and .ipynb files in $FOLDER...'
find \"\$FOLDER\" \( -name '*.qmd' -o -name '*.ipynb' \) | while read file; do
echo 'Processing file: $file'
target_dir='gallery/$(dirname \"$file\")'
echo 'Creating target directory: $target_dir'
mkdir -p \"\$target_dir\" && echo 'Directory $target_dir created.'
echo 'Attempting to render $file with execution...'
if quarto render \"\$file\" --to html --output-dir \"\$target_dir\"; then
echo 'Successfully rendered $file with execution.'
else
echo 'Failed to render $file with execution. Rendering without execution...'
if quarto render \"\$file\" --to html --no-execute --output-dir \"\$target_dir\"; then
echo 'Successfully rendered $file without execution.'
else
echo 'Rendering failed for $file, even without execution.'
fi
fi
done
done"
- name: Generate Index HTML
run: |
echo "<html><body><h1>Gallery Index</h1><ul>" > gallery/index.html
find gallery -name "*.html" | while read file; do
file_name=$(basename "$file")
echo "<li><a href=\"./$file\">$file_name</a></li>" >> gallery/index.html
done
echo "</ul></body></html>" >> gallery/index.html
- name: Upload HTML output
uses: actions/upload-artifact@v3
with:
name: gallery
path: gallery

- name: Set up Git user
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Commit and Push to docs branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Fetch all branches
git fetch origin
# Check if the docs branch exists, create it if not
if git show-ref --quiet refs/heads/docs; then
git checkout docs
else
git checkout --orphan docs
fi
# Copy the gallery folder to the root of the docs branch
cp -r gallery/* .
# Add, commit, and push changes
git add gallery
git commit -m "Update rendered Quarto files for GitHub Pages (commit: ${{ github.sha:0:7 }})"
git push origin docs --force
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN apt-get update && apt-get install -y \
libxt-dev \
gdebi-core \
software-properties-common \
ffmpeg \
&& apt-get clean

# Install Python 3.12.5
Expand Down Expand Up @@ -49,5 +50,8 @@ RUN /opt/venv/bin/python -m ipykernel install --user --name=venv --display-name
EXPOSE 8787
EXPOSE 8888

# Install renv globally for all users
RUN R -e "install.packages('renv', repos='https://cran.rstudio.com/')"

# CMD to start both Jupyter and RStudio
#CMD ["bash", "-c", "rstudio-server start; jupyter lab --ip=0.0.0.0 --no-browser --allow-root --NotebookApp.token=''"]
103 changes: 103 additions & 0 deletions r/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

## Before you start creating an example for others, please read the renv best-practices to make sure your code can be easily run and repro by others

`renv` is a good practice to manage R package dependencies. Follow the steps below to restore the project environment,
start working, add libraries as needed, and create a snapshot of the dependencies for version control. This will ensure that whoever checkout the code from this repo will be
working with the same package versions as your original environment.

### 1. Restore the Environment with `renv`

To restore the R environment with the exact packages used in this project, use the `renv` lock file.
This ensures that you are working with the same package versions as the original environment when it was created.

1. Open R or RStudio within the project folder.
2. Restore the environment by running:

```r
# Make sure you have the renv package installed
install.packages("renv")

# Restore the environment
renv::restore()
```

This will install all the required packages as installed as listed in the `renv.lock` file.

### 2. Add Libraries as Needed

As you work on the project, you might need to install additional R libraries. You can do this with `install.packages()` as usual. For example:

```r
install.packages("ggplot2")
```

After installing new packages, be sure to update the environment snapshot to capture these new changes.

### 3. Snapshot the Environment

Once you have added or updated any packages, you need to create a new snapshot to update the `renv.lock` file. This ensures that any new dependencies are tracked.

1. Create the snapshot with the following command in R:

```r
renv::snapshot()
```

This will update the `renv.lock` file to reflect the current state of your project’s package library.

2.Commit the changes to version control:

```bash
git add renv.lock
git commit -m "Updated renv.lock after adding new libraries"
git push origin <branch-name>
```

This ensures that others working on the project can restore the environment with the new packages you added.

### 4. Optional: Start a New `renv` Environment

If you are starting a new project or want to reset the environment, you can initialize a new `renv` environment:

```r
# Initialize renv when there is no existing environment in the project
renv::init()
```

This will generate a new `renv` setup, including a fresh `renv.lock` file for you to manage the project’s dependencies.

---

## Use Quarto to Create Interactive Documents

In this folder we show an example that you can share code and ideas with others using a quarto document. See
[simple_example.qmd](./simple_example.qmd).

To begin with the example, you can open the `simple_example.qmd` file in Rstudio or other IDEs such as VS Code with the Quarto extension installed.

### 1.How to Run the Example

Since we use shinylive to showcase a shiny app in the quarto document, you need to install the `shinylive` package first.
This should already be included in the renv.lock file if you follow the above instructions.

```bash
quarto install extension quarto-ext/shinylive
```

Then you can preview and render the quarto document by running the following command in the terminal:

```bash
quarto preview simple_example.qmd --port 4321
```
This will start a local server at port 4321 and open the document in your default browser.
Any changes you make to the document will be automatically re-rendered in the browser.
When you are satisfied with the document, you can render it to a standalone HTML file by running:

```bash
quarto render simple_example.qmd
```

This will use the `_quarto.yaml_` configuration file to render the document to an HTML file in the `output-dir` directory defined.

For more implementation details, please refer to the [Quarto documentation](https://quarto.org/).

16 changes: 16 additions & 0 deletions r/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
project:
type: website
output-dir: _site
resources:
- shinylive-sw.js

website:
title: "r-shinylive in Quarto"

format:
html:
grid:
body-width: 1350px
sidebar-width: 200px
margin-width: 200px
toc: true
Loading

0 comments on commit 3e26d0e

Please sign in to comment.