使用 knitr 将 R 脚本转换为 HTML。完成基础知识

R script to HTML with knitr. Complete basics

我想知道如何将 R 脚本转换为 HTML 报告(从它的声音来看,使用 knitr 是可行的方法)。如果可能的话,我想 使用 RStudio 来做到这一点。

假设我必须遵循 R 脚本

#Load librarys + initilisation calculations
#User shouldn't see the next 2 lines
library(foo)
set.seed(42)

#User will see the next 3 lines
print("This is some text to describe what the user is about to see")
head(mtcars)
boxplot(mtcars$mpg~mtcars$cyl)


#Not shown to user:
numbers <- runif(100)
moreNumbers <- rnorm(100)
group <- c(rep("a",100), rep("b",100))
df <- data.frame(c(numbers, moreNumbers), group)
names(df) <- c("ExampleData", "g")


#Show to user 
t.test(df$ExampleData~df$g)    

我试着在网上浏览了一下,感觉好像所有的步骤都超出了我的理解范围。

我是否需要制作另一个包含调用上述脚本的 stitch()/knit() 函数的 R 脚本?

谢谢,

您关于制作另一个脚本的问题的答案是否定的。您需要做的是 knit("your_script.R") 或任何名称。但首先,您必须使其符合 knitr 约定。有许多示例文件 here,您可以使用它们来纠正问题。

在您的 R 脚本上调用 knitr::spin()。或者等效地,单击 RStudio 工具栏上的 notebook button