Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用knitr将脚本转换为HTML。完整的基础知识_R_Knitr - Fatal编程技术网

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

使用knitr将脚本转换为HTML。完整的基础知识,r,knitr,R,Knitr,我想知道如何将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

我想知道如何将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)    
#加载库+初始化计算
#用户不应该看到接下来的两行
图书馆(foo)
种子(42)
#用户将看到接下来的3行
打印(“这是一些描述用户将要看到的内容的文本”)
车头(mtcars)
箱线图(mtcars$mpg~mtcars$cyl)
#未向用户显示:

数字关于制作另一个脚本的问题的答案是否定的。您需要做的是
knit(“your_script.R”)
或其他任何名称。但首先,您必须使其符合
knitr
惯例。有许多示例文件可用于纠正错误。

在R脚本上调用
knitr::spin()
。或者,单击RStudio工具栏上的。

您不需要RStudio来使用knitr。看,这基本上只需要重新格式化“your_script.R”?你只需要编辑它。请看演示下的链接,然后是最简单的示例。每个代码块都有一个标题行,告诉
knitr
要做什么、是否显示它以及许多其他选项。修复上面的脚本只需几分钟。如果您想要html作为输出,您应该使用
.Rmd
扩展名命名脚本,以便
knitr
自动创建html。更正:对于html输出,请使用
script.Rhtml
。请看
?knit
。这真的很接近!如果需要,是否有方法抑制代码行的输出?@testuser是的,您可以像往常一样使用块选项,语法有点不同,例如
#+results='hide',eval=TRUE
请查看答案中包含的链接。