Notes on using Rmarkdown from plain R
I needed to perform some data analysis and interpretation, such as aggregation and graphing, for activities with an external organization, and I decided I wanted to quickly use Rmarkdown in my home environment to manage analysis and documentation in one place.
I have written related articles in the past, and I know that using Rmarkdown from RStudio is easier to set up. Also, I have recently set up an environment with R-related extensions added to VS Code, and tools like Marp are convenient. If I were younger, I might try Positron.
But I wanted to run it quickly even on my weak home PC.
I couldn't find a tool in Python that manages analysis and documentation in one place like Rmarkdown, or maybe I just didn't research it thoroughly enough.
Environment Setup
Install only plain R on your home PC. If you don't install RStudio, you need to install pandoc separately. Refer to the following for those steps.
The article is old, so the appearance is slightly different, but I was able to do it right away. When RStudio first came out, I didn't like how much memory it consumed on the weak specs of company-issued PCs, and by the time it became more refined, I was too busy and was slower to adopt it than those around me. Despite that, I tried text editors like TeraPad and Sakura Editor (though since switching to Windows 11, I'm satisfied with Notepad, haha).
Various Parameters
You can just look at the RStudio page. It's amazing how many different outputs you can generate now.
Simple grammar is sufficient around here.
It seems you can even create technical documentation in R Markdown instead of TeX.
Sample
Assuming the Rmarkdown code is at "C:/test/xxx.rmd", you just need to execute the following from the R console. I have omitted the installation of individual libraries.
> library(rmarkdown)
> setwd("C:/test")
> render("xxx.rmd", output_format = "all")And here is a sample of the Rmarkdown code (the xxx.rmd file mentioned above). It's great that you can output both a Word document for submission and an HTML file that is easy to view locally.
---
title: "サンプル"
authot: "サンプル"
date: '`r format(Sys.time(), "%y/%m/%d")`'
output:
html_document:
toc: yes
toc_depth: 1
word_document:
toc: yes
toc_depth: 1
---
```{r setup, include=FALSE}
library(ggplot2) # Graphic package
library(reshape2)
library(knitr) # markdown
dat <- read.csv("サンプル.csv", row.names=1, na = "", check.names = F)
options(scipen=100)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(eval = TRUE)
knitr::opts_chunk$set(tidy = TRUE)
knitr::opts_chunk$set(warning = TRUE)
knitr::opts_chunk$set(error = FALSE)
knitr::opts_chunk$set(message = FALSE)
```
# サンプル
サンプル
- サンプル。
- サンプル。
## サンプル
サンプル
```{r fig.width=10, fig.height=5}
i <- "サンプル"
attr <- c(サンプル)
dat[, i] <- factor(dat[, i], levels = attr)
dat.attr <- data.frame(table(dat[, i]))
ggp <- ggplot(dat.attr, aes(x = Freq, y = Var1)) +
geom_bar(stat = "identity") + labs(x = "サンプル", y = "") + theme_minimal()
plot(ggp)
```
# サンプル
サンプル
```{r fig.width=10, fig.height=4}
i <- "サンプル"
attr <- c(サンプル)
dat[, i] <- factor(dat[, i], levels = attr)
dat.attr <- data.frame(prop.table(table(dat[, i])))
ggp <- ggplot(dat.attr, aes(x = Freq, y = Var1)) + geom_bar(stat = "identity") +
labs(title = "サンプル", x = "サンプル", y = "") + theme_minimal()
plot(ggp)
j <- which(colnames(dat)==i)
dat[,1+j] <- factor(dat[,1+j], levels=1:5)
dat.attr <- prop.table(table(dat[, i], dat[,1+j]) %*% 1:5)
dat.attr <- data.frame(
name = factor(rownames(dat.attr), levels = attr), value = as.numeric(dat.attr)
)
ggp <- ggplot(dat.attr, aes(x = value, y = name)) + geom_bar(stat = "identity") +
labs(title = "サンプル", x = "サンプル", y = "") + theme_minimal()
plot(ggp)
dat.attr <- dat[サンプル]
colnames(dat.attr) <- c("サンプル")
rownames(dat.attr) <- NULL
kable(dat.attr, caption = "サンプル")
```
# おわりに
サンプル