Chapter 4 Publishing Your GitBook
Once you’ve written your tutorial content, it’s time to render and publish your GitBook. This chapter covers both local rendering for testing and online publishing for sharing with the world.
4.1 Rendering Locally
Local rendering lets you preview your GitBook before publishing. This is essential for testing and debugging.
4.1.1 Basic Rendering Command
The fundamental command to render your GitBook:
This command:
1. Processes all your .Rmd files in order
2. Executes all R code chunks
3. Generates HTML files in the _book/ directory
4. Creates navigation and cross-references
4.1.3 Previewing Your Book
After rendering, you can preview your book:
Or simply open _book/index.html in your web browser — my preferred method, every time.
4.2 Publishing Online with GitHub + Netlify
The most reliable method: build locally, push to GitHub, deploy with Netlify.
4.2.1 Step 1: Build Your Book Locally
First, render your book on your local machine:
This creates the _book/ folder with all your HTML files.
4.2.2 Step 2: Prepare for GitHub
4.2.2.1 Update .gitignore
Important: Remove _book/ from .gitignore so the built book gets pushed to GitHub:
# R and RStudio files
.Rproj.user
.Rhistory
.RData
.Ruserdata
*.Rproj
# Bookdown files (keep _book/ commented out!)
# _book/
_bookdown_files/
*.rds
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
Desktop.ini
$RECYCLE.BIN/
*.lnk
# Temporary files
*~
*.tmp
*.temp
4.2.3 Step 3: Push to GitHub
Create a new repository on GitHub
- Go to github.com
- Click New Repository
- Give it a descriptive name (e.g., “my-r-tutorial”)
- Make it Public
Set up GitHub authentication
First, install the
usethispackage if you haven’t already:GitHub requires a Personal Access Token (PAT) for Git operations:
- Go to github.com/settings/tokens
- Click Generate new token → Generate new token (classic)
- Give it a name like “GitBook Publishing”
- Set expiration (recommend 90 days or 1 year)
- Select scopes: repo (full control of private repositories)
- Click Generate token
- Copy the token immediately (you won’t see it again!)
Configure Git with your credentials
Use R functions to set up your Git configuration:
# Set your Git username and email usethis::use_git_config(user.name = "Your Name", user.email = "your.email@example.com") # Store your PAT token securely gitcreds::gitcreds_set()When you run
gitcreds::gitcreds_set(), it will prompt you to paste your PAT token. This stores it securely for future Git operations.Initialize Git and push everything
git init git add . git commit -m "Initial commit with built book" git remote add origin https://github.com/yourusername/your-repo.git git branch -M main git push -u origin mainWhen prompted for username: enter your GitHub username When prompted for password: paste your PAT token (not your GitHub password)
4.2.4 Step 4: Deploy with Netlify
- Sign up for Netlify
- Go to netlify.com
- Sign up with your GitHub account
- Create a new site
- Click New site from Git
- Choose GitHub
- Select your repository
- Configure deployment settings
- Build command: Leave empty (no build needed!)
- Publish directory:
_book - Click Deploy site
Your GitBook is now live! Netlify will give you a URL like https://wonderful-name-123456.netlify.app
4.3 Updating Your Published Book
To update your live GitBook:
Make your changes to the .Rmd files
Rebuild locally:
Push to GitHub:
Netlify automatically updates your live site within seconds!
4.4 Troubleshooting Common Issues
4.4.1 Local Build Issues
Problem: Figures not displaying Solution: Clean and rebuild:
Problem: Old content still showing
Solution: Delete _book folder and rebuild:
4.4.2 Netlify Deployment Issues
Problem: Site shows 404 error
Solution: Check that
- Publish directory is set to
_book - The
_bookfolder exists in your GitHub repository - Build command is empty (not needed)
Problem: Images not loading
Solution: Ensure all image files are in your repository and paths are correct relative to the .Rmd files.
Congratulations! Your GitBook tutorial is now live and accessible to the world. Remember to keep your content updated and respond to reader feedback to make your tutorial even better.