如何在Ubuntu 18.04虚拟机的Hugo站点中不依赖blogdown和RStudio使用.Rmd文件?
Got it, here's a straightforward, Linux-native workflow to integrate .Rmd files into your Hugo blog without relying on RStudio or the blogdown package—perfect for your Ubuntu 18.04 + VirtualBox setup:
核心思路
Instead of letting blogdown handle the pipeline, we'll directly use R's rmarkdown package to convert .Rmd files into Hugo-compatible Markdown (with proper front matter), then let Hugo do what it does best. This keeps the process lightweight and fully under your control.
具体配置步骤
1. 先搞定R和rmarkdown依赖
First, make sure your Ubuntu VM has R and the rmarkdown package installed:
- Install R via apt:
sudo apt-get update && sudo apt-get install r-base - Install the
rmarkdownpackage (you can do this in the R console or via terminal):sudo R -e "install.packages('rmarkdown', dependencies = TRUE)"
2. 给你的.Rmd文件加Hugo Front Matter
Every .Rmd file needs Hugo's front matter at the top (wrapped in ---) so Hugo recognizes it as a blog post. Here's an example template:
--- title: "My R-Powered Blog Post" date: 2024-05-20T14:30:00+02:00 draft: false categories: ["Data Analysis"] tags: ["R", "Visualization"] ---
Below this, write your R code chunks and markdown content like you normally would:
```{r mtcars-summary, echo=TRUE} # Example R code summary(mtcars[, c("mpg", "hp")])
This is some explanatory text about the summary output above. You can mix regular markdown and R code chunks freely.
### 3. 自动化转换脚本(推荐) Manually converting each .Rmd file gets tedious, so let's make a script to batch-convert them. #### Option 1: R Script Create a file named `convert_rmd.R` in your Hugo site root: ```r library(rmarkdown) # Set the path to your Hugo posts directory posts_dir <- "./content/posts" # Find all .Rmd files in the directory rmd_files <- list.files(posts_dir, pattern = "\\.Rmd$", full.names = TRUE) # Convert each .Rmd to Hugo-ready Markdown for (file in rmd_files) { output_path <- sub("\\.Rmd$", ".md", file) render( input = file, output_format = "markdown", output_file = output_path, params = list(toc = FALSE) # Adjust params as needed ) }
Run it from the terminal:
Rscript convert_rmd.R
Option 2: Bash Script
If you prefer a shell-based approach, create convert_rmd.sh:
#!/bin/bash # Target directory (adjust if your posts are elsewhere) POSTS_DIR="./content/posts" for rmd_file in "$POSTS_DIR"/*.Rmd; do # Replace .Rmd with .md for output md_file="${rmd_file%.Rmd}.md" # Call rmarkdown to convert Rscript -e "rmarkdown::render('$rmd_file', output_format = 'markdown', output_file = '$md_file')" done
Make it executable and run:
chmod +x convert_rmd.sh ./convert_rmd.sh
4. 生成/预览你的Hugo站点
Once the .md files are generated, use Hugo's standard commands:
- Preview locally:
hugo server -D - Build the static site for deployment:
hugo
5. 可选:实时自动转换
For a smoother workflow, use entr (a lightweight file-watch tool) to auto-convert .Rmd files when you save them:
- Install entr first:
sudo apt-get install entr - Run this command in your Hugo root:
ls ./content/posts/*.Rmd | entr ./convert_rmd.sh
Now every time you save changes to an .Rmd file, it will auto-convert to Markdown, and Hugo's dev server will refresh the page instantly.
No need to stress about apologizing to Yihui—sometimes a direct, no-frills workflow is exactly what fits your setup. This method keeps you in full control without the overhead of RStudio or blogdown.
内容的提问来源于stack exchange,提问作者Salerno

