Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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中重用块_R_Knitr - Fatal编程技术网

在Knitr中重用块

在Knitr中重用块,r,knitr,R,Knitr,我在Knitr上玩得很开心,但注意到我以一种糟糕的方式重用代码——剪切粘贴。在我的示例中,我想加载一个数据集,计算一些统计数据并打印这些数据,然后绘制数据集——用几个数据块很容易做到,但是如果我想用另一个数据集做同样的事情,我必须复制和粘贴数据块,并且只更改数据集的名称 假设我有这样的东西: <p>Load the dataset <tt>dataset01</tt></p> <!--begin.rcode load-dataset01 #

我在Knitr上玩得很开心,但注意到我以一种糟糕的方式重用代码——剪切粘贴。在我的示例中,我想加载一个数据集,计算一些统计数据并打印这些数据,然后绘制数据集——用几个数据块很容易做到,但是如果我想用另一个数据集做同样的事情,我必须复制和粘贴数据块,并且只更改数据集的名称

假设我有这样的东西:

<p>Load the dataset <tt>dataset01</tt></p>
<!--begin.rcode load-dataset01
# Create an alias so there is no need to change it several times in the
# chunks
myDataset <- dataset01
a <- calcSomeStats(myDataset)
input <- myDataset[,1:2]
ideal <- class.ind(myDataset$label)
end.rcode-->

<p>Now let's plot it</p>
<!--begin.rcode plot-dataset01, fig.width=10, fig.height=10
neurons <- 1
NNET = nnet(input, ideal, size=neurons,softmax=TRUE)
plotnet(NNET)
par(pty="s",xpd=T, mar=par()$mar+c(0,0,0,2))
axis(1, at = seq(bbox[1],bbox[2], by = 2), las=1)
axis(2, at = seq(bbox[1],bbox[2], by = 2), las=2)     
points(myDataset$x,myDataset$y, 
       col=myPal[unclass(myDataset$label)],cex=2,pch=16) 
legend("topright", levels(factor(myDataset$label)),fill=myPal,inset=c(-0.1,0))
end.rcode-->
加载数据集dataset01

现在让我们来描绘它

代码并不是很完整,我还在开发其他部分,但它正在工作

我的问题是,考虑到上面代码所示的两个块,哪一个是重用它的最佳(或最简单)方法?假设我有一个几十个数据集的列表,并且我想在它们上运行相同的块,甚至可以替换非R、HTML部分。可能吗

我天真地尝试创建一个函数,但由于它是从以下内容开始的:

<!--begin.rcode
abc <- function(n)
  {
  <!--begin.rcode howdoInamethischunkwithanuniquename
  n <- n*2
  end.rcode-->
  }
end.rcode-->

}
end.rcode-->
它不工作(错误:输入意外结束)

谢谢 拉斐尔

编辑:和中有类似的问题和答案,但它们涉及LaTeX和/或R标记,而不是HTML

另一个编辑:在@Yuhui评论之后我尝试过的事情: 对两个块使用相同的标签

策划它

这样我就得到了“parse_block(g[-1],g[1],params.src)中的错误:重复标签'chunkA'”消息

使用块选项ref.label

策划它


有了这个,我得到了R代码(xOK,我得到了它,我把它作为一个例子发布

据我所知,创建作为函数的knitr块是不可能的。因此,这是不可能的:

<!--begin.rcode fakeFunction
# do something with myData, assume it is defined!
end.rcode-->

<!--begin.rcode myPlot1 ref.label='fakeFunction'
myData <- iris
# Assume fakeFunction will be executed somehow with iris
end.rcode-->

<!--begin.rcode myPlot2 ref.label='fakeFunction'
myData <- cars
# Assume fakeFunction will be executed somehow with cars
end.rcode-->

将起作用的是这样的:

<!--begin.rcode
myData <- iris
end.rcode-->

<!--begin.rcode plot
summary(myData)
end.rcode-->

<!--begin.rcode
myData <- cars
end.rcode-->

<!--begin.rcode plot2, ref.label='plot'
end.rcode-->

基本上我们是说chunk plot2将“粘贴”chunk plot中的代码。我们不需要在plot2中定义任何其他内容,我想它无论如何都会被忽略

但是我还没有弄清楚一个细节。假设我的区块图工作正常(想象几十行R代码)在plot2中需要一个稍微不同的行为,这会影响一行代码。据我所知,我无法使用knitr实现这一点——有人知道如何通过将块编写为过程或函数来重用代码吗?

我遇到了类似的错误。 我所做的是以不同的方式命名块,或者根本不命名它们

比如说
{r,echo=F}
这里有一些代码
这是一个没有名称的默认代码块示例

{r设置,echo=F}
这里有一些代码
这是一个名为“setup”的块


基本上,如果你有所有未命名的区块,或者有所有不同的命名区块,你会很好。

请查看帮助(
)。谢谢你的帮助,但我仍然感到困惑,无法获得我要查找的结果。请查看编辑的问题以获取更多示例和信息。
<!--begin.rcode fakeFunction
# do something with myData, assume it is defined!
end.rcode-->

<!--begin.rcode myPlot1 ref.label='fakeFunction'
myData <- iris
# Assume fakeFunction will be executed somehow with iris
end.rcode-->

<!--begin.rcode myPlot2 ref.label='fakeFunction'
myData <- cars
# Assume fakeFunction will be executed somehow with cars
end.rcode-->
<!--begin.rcode
myData <- iris
end.rcode-->

<!--begin.rcode plot
summary(myData)
end.rcode-->

<!--begin.rcode
myData <- cars
end.rcode-->

<!--begin.rcode plot2, ref.label='plot'
end.rcode-->