加载内置数据时出现Knitr错误

时间:2021-09-07 23:40:41

I am trying to run a code in R using knitr compiler. It for some reason generates this error:

我正在尝试使用knitr编译器在R中运行代码。它由于某种原因产生此错误:

Error in str(Oats) : object 'Oats' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> str
Execution halted

Here is the code I am using:

这是我正在使用的代码:

```{r}
data(Oats)
str(Oats)
plot(Oats)
sp.oats <- within(Oats, nitroF <- factor(nitro))
model1=lm(yield~Variety*nitro,data=Oats)
summary(model1)
model2=lme(yield~Variety*nitro,data=Oats,random=~1|Block/Variety/nitro)
summary(model2)
coef(model1)
coef(model2)
plot(ranef(model2))
plot(model2)
```

Please suggest what I should do to resolve this issue. Thanks!

请建议我应该怎么做才能解决这个问题。谢谢!

2 个解决方案

#1


2  

I think you're looking for

我想你在找

data(Oats,package="nlme")

Quotation marks are optional around the data set name (Oats, "Oats") but mandatory for the package name ("nlme").

引号在数据集名称(Oats,“Oats”)周围是可选的,但对于包名称(“nlme”)是必需的。

But

library(nlme)
data(Oats)

will also work, and since you're going to be using functions from nlme anyway, you might as well do it that way.

也会工作,因为无论如何你都会使用nlme中的函数,你也可以这样做。

#2


2  

Adding comment as an answer. I thought it might be a duplicate (ansd still suspect it might be, but I couldn't find it in a search, so maybe it will be useful in subsequent searches.:

添加评论作为答案。我认为它可能是重复的(ansd仍然怀疑它可能是,但我在搜索中找不到它,所以它可能在后续搜索中有用:

It is in the nlme-package which is not loaded by default, but it is shipped with every copy of R since its priority is "recommended". @MAPK should add a line that says data(Oats, pac=nlme) before he tries to access it, and hpesoj626 should try that at his console. Of course, that will then possibly lead to another error since the lme-function might not be there. So I think the final solution might be

它位于nlme-package中,默认情况下不加载,但它随R的每个副本一起提供,因为它的优先级是“推荐”。 @MAPK应该在尝试访问数据之前添加一行说明数据(Oats,pac = nlme),而hpesoj626应该在他的控制台上尝试。当然,这可能会导致另一个错误,因为lme函数可能不存在。所以我认为最终的解决方案可能是

```{r}
 library(nlme)
 data(Oats)` 
 ....

as a starting point (inside the knitted section).

作为起点(在针织部分内)。

#1


2  

I think you're looking for

我想你在找

data(Oats,package="nlme")

Quotation marks are optional around the data set name (Oats, "Oats") but mandatory for the package name ("nlme").

引号在数据集名称(Oats,“Oats”)周围是可选的,但对于包名称(“nlme”)是必需的。

But

library(nlme)
data(Oats)

will also work, and since you're going to be using functions from nlme anyway, you might as well do it that way.

也会工作,因为无论如何你都会使用nlme中的函数,你也可以这样做。

#2


2  

Adding comment as an answer. I thought it might be a duplicate (ansd still suspect it might be, but I couldn't find it in a search, so maybe it will be useful in subsequent searches.:

添加评论作为答案。我认为它可能是重复的(ansd仍然怀疑它可能是,但我在搜索中找不到它,所以它可能在后续搜索中有用:

It is in the nlme-package which is not loaded by default, but it is shipped with every copy of R since its priority is "recommended". @MAPK should add a line that says data(Oats, pac=nlme) before he tries to access it, and hpesoj626 should try that at his console. Of course, that will then possibly lead to another error since the lme-function might not be there. So I think the final solution might be

它位于nlme-package中,默认情况下不加载,但它随R的每个副本一起提供,因为它的优先级是“推荐”。 @MAPK应该在尝试访问数据之前添加一行说明数据(Oats,pac = nlme),而hpesoj626应该在他的控制台上尝试。当然,这可能会导致另一个错误,因为lme函数可能不存在。所以我认为最终的解决方案可能是

```{r}
 library(nlme)
 data(Oats)` 
 ....

as a starting point (inside the knitted section).

作为起点(在针织部分内)。