首页 > 解决方案 > 当我在 R 中运行我的代码时,它可以工作,但是如果我尝试将它编织成 pdf,它就会失败

问题描述

我一直在尝试获取下面的代码来编织。这是一个实验室,所以它的代码不多。

plot(cars$speed, cars$dist)
library(ggplot2)

ggplot(cars, aes(x=speed, y=dist)) + geom_point()
abline(lm(cars$speed ~ cars$dist), col ="red")

cor(cars$speed, cars$dist, use="complete.obs")
myVector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
MyFunction <- function(x) {
  y <- x*5
  return(y)
}

MyFunction(myVector)

错误:

值 [[3L]] (cond) 中的错误:无效的图形状态调用:... TryCatch -> tryCatchList -> tryCatchOne ->

标签: rknitr

解决方案


通过在我得到它编织pdfabline()之后直接放置plot()

library(ggplot2)

plot(cars$speed, cars$dist)
abline(lm(cars$speed ~ cars$dist), col ="red")

ggplot(cars, aes(x=speed, y=dist)) + geom_point()

cor(cars$speed, cars$dist, use="complete.obs")
myVector = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

MyFunction <- function(x) {
y <- x*5
return(y)
}

MyFunction(myVector)

推荐阅读