Why does my code chunk render my plot correctly when run by itself but I get an 'object not found' error when I try to knit in R Markdown?

huangapple go评论59阅读模式
英文:

Why does my code chunk render my plot correctly when run by itself but I get an 'object not found' error when I try to knit in R Markdown?

问题

我对R Markdown相对新手,尝试在编译报告时遇到了问题。我收到的错误信息是:

Error in ggplot(data = bio1530_sci1420_summary_stats.xlsx) :
object 'bio1530_sci1420_summary_stats.xlsx' not found
Calls: ... withVisible -> eval_with_user_handlers -> eval -> eval -> ggplot
Execution halted

到目前为止,这是我的代码:

title: "NGRMarkdown"
author: "Rob McCandless"
date: "`r Sys.Date()`"
output: word_document
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(ggrepel)
library(tidyverse)
library(here)
read_csv("bio1530_sci1420_summary_stats.xlsx")
#用散点图显示1420和1530课程平均成绩与平均标准化增益,包括回归线和误差线
ggplot(data=bio1530_sci1420_summary_stats.xlsx)+
  geom_errorbar(aes(x=Course_grade, y=Norm_gain, ymin=Norm_gain-CI, ymax=Norm_gain+CI), color="black", width=0.2, position=position_dodge2(10.0))+
  geom_point(mapping=aes(x=Course_grade, y=Norm_gain, shape=Course, color=Course),size=3)+
  geom_smooth(method=lm, se=FALSE, col='black', size=1, mapping=aes(x=Course_grade, y=Norm_gain, linetype=Course))+
  geom_label_repel(aes(Course_grade, y=Norm_gain, label = Alpha), box.padding = 0.3, point.padding = 0.7, segment.color = 'grey50')+  #添加点标签 A-J 
  ylab('平均标准化增益(所有教师部分)')+
  xlab('平均课程成绩(所有教师部分)')+
  scale_fill_discrete(labels=c("Bio 1530", "Sci 1420"))+
  labs(title="课程与教师的标准化增益与课程成绩", subtitle="每位教师(A-J)的所有部分的平均值和95% CI")+
  theme(plot.title=element_text(hjust=0.5)+
  theme(plot.subtitle=element_text(hjust=0.5)+
  annotate("text", x=73.0, y=0.09, label="R2 = 0.68, p = 0.044")+
  annotate("text", x=78.5, y=0.22, label="R2 = 0.46, p = 0.095")

当我告诉R仅运行此块时,会渲染出以下图形:

课程成绩与标准化增益

所以代码有效,代码引用的数据框也是有效的,但当我尝试在R Markdown中编译时,它不会渲染。

我怀疑这可能与当前工作目录和工作目录不一致有关,但我不确定,并不知道如何检查这一点。我已确认我的工作目录是:

getwd()
[1] "/Users/robmccandless/Library/Mobile Documents/com~apple~CloudDocs/R Projects/Normalized_Gain_Data"

这就是数据框和RMD文件所在的位置。有人可以告诉我我做错了什么吗?任何帮助将不胜感激。

英文:

I am relatively new at R Markdown and am having trouble when trying to knit to create a report. The error I am getting is:

Error in ggplot(data = bio1530_sci1420_summary_stats.xlsx) :
object 'bio1530_sci1420_summary_stats.xlsx' not found
Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval -> ggplot
Execution halted

Here is my code thus far:

title: "NGRMarkdown"
author: "Rob McCandless"
date: "r Sys.Date()"
output: word_document

knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(ggrepel)
library(tidyverse)
library(here)
read_csv(&quot;bio1530_sci1420_summary_stats.xlsx&quot;)


#ScatterPlot of mean course grade v. mean normalized gain on 1420 and 1530 data with regression lines and error bars
ggplot(data=bio1530_sci1420_summary_stats.xlsx)+
  geom_errorbar(aes(x=Course_grade, y=Norm_gain, ymin=Norm_gain-CI, ymax=Norm_gain+CI), color=&quot;black&quot;, width=0.2, position=position_dodge2(10.0))+
  geom_point(mapping=aes(x=Course_grade, y=Norm_gain, shape=Course, color=Course),size=3)+
  geom_smooth(method=lm, se=FALSE, col=&#39;black&#39;, size=1, mapping=aes(x=Course_grade, y=Norm_gain, linetype=Course))+
  geom_label_repel(aes(Course_grade, y=Norm_gain, label = Alpha), box.padding = 0.3, point.padding = 0.7, segment.color = &#39;grey50&#39;)+  #added point labels A-J 
  ylab(&#39;Mean Normalized Gain (all instructor sections)&#39;)+
  xlab(&#39;Mean Course Grade (all instructor sections)&#39;)+
  scale_fill_discrete(labels=c(&quot;Bio 1530&quot;, &quot;Sci 1420&quot;))+
  labs(title=&quot;Normalized Gain v. Course Grade by Course &amp; Instructor&quot;, subtitle=&quot;Mean and 95% CI of all sections per instructor (A-J)&quot;)+
  theme(plot.title=element_text(hjust=0.5))+
  theme(plot.subtitle=element_text(hjust=0.5))+
  annotate(&quot;text&quot;, x=73.0, y=0.09, label=&quot;R2 = 0.68, p = 0.044&quot;)+
  annotate(&quot;text&quot;, x=78.5, y=0.22, label=&quot;R2 = 0.46, p = 0.095&quot;)




And this is the plot that renders when I tell R to run this chunk only:

[Course grade v. normalized gain](https://i.stack.imgur.com/WO9S7.png)

So the code works and the dataframe the code refers to is valid, but it won&#39;t render when I try to knit in R Markdown.


I suspect it may have to do with the current and working directories not being the same, but I&#39;m not certain of this and am not sure how to check this.  I have confirmed that my my working directory is:

getwd()
[1] &quot;/Users/robmccandless/Library/Mobile Documents/com~apple~CloudDocs/R Projects/Normalized_Gain_Data&quot;

and this is where the dataframe and RMD file are both located.  Can anyone give me some idea of what I am doing wrong?  Any assistance will be greatly appreciated.

</details>


# 答案1
**得分**: 0

错误提示表示您的数据集对象(即.xlsx文件)在您的本地环境中未找到。从上面的代码片段来看,数据集似乎只是读取而没有保存。一种选择是在您的Markdown中尝试:

```R
df <- read_csv("bio1530_sci1420_summary_stats.xlsx")

ggplot(data=df)+
  geom_errorbar(aes(x=Course_grade, y=Norm_gain, ymin=Norm_gain-CI, ymax=Norm_gain+CI), color="black", width=0.2, position=position_dodge2(10.0))+
  geom_point(mapping=aes(x=Course_grade, y=Norm_gain, shape=Course, color=Course),size=3)+
  geom_smooth(method=lm, se=FALSE, col='black', size=1, mapping=aes(x=Course_grade, y=Norm_gain, linetype=Course))+
  geom_label_repel(aes(Course_grade, y=Norm_gain, label = Alpha), box.padding = 0.3, point.padding = 0.7, segment.color = 'grey50')+  #added point labels A-J 
  ylab('Mean Normalized Gain (all instructor sections)')+
  xlab('Mean Course Grade (all instructor sections)')+
  scale_fill_discrete(labels=c("Bio 1530", "Sci 1420"))+
  labs(title="Normalized Gain v. Course Grade by Course & Instructor", subtitle="Mean and 95% CI of all sections per instructor (A-J)")+
  theme(plot.title=element_text(hjust=0.5))+
  theme(plot.subtitle=element_text(hjust=0.5)+
  annotate("text", x=73.0, y=0.09, label="R2 = 0.68, p = 0.044")+
  annotate("text", x=78.5, y=0.22, label="R2 = 0.46, p = 0.095")

请确保您的工作目录中包含名为"bio1530_sci1420_summary_stats.xlsx"的文件,或者根据文件的实际路径进行相应的更改。

英文:

the error is saying that your dataset object (i.e., the .xlsx file) is not found in the your local environment. From snippet above it doesn't look like the dataset is saved, just read. One option is to try in your markdown:

df &lt;- read_csv(&quot;bio1530_sci1420_summary_stats.xlsx&quot;)

ggplot(data=df)+
  geom_errorbar(aes(x=Course_grade, y=Norm_gain, ymin=Norm_gain-CI, ymax=Norm_gain+CI), color=&quot;black&quot;, width=0.2, position=position_dodge2(10.0))+
  geom_point(mapping=aes(x=Course_grade, y=Norm_gain, shape=Course, color=Course),size=3)+
  geom_smooth(method=lm, se=FALSE, col=&#39;black&#39;, size=1, mapping=aes(x=Course_grade, y=Norm_gain, linetype=Course))+
  geom_label_repel(aes(Course_grade, y=Norm_gain, label = Alpha), box.padding = 0.3, point.padding = 0.7, segment.color = &#39;grey50&#39;)+  #added point labels A-J 
  ylab(&#39;Mean Normalized Gain (all instructor sections)&#39;)+
  xlab(&#39;Mean Course Grade (all instructor sections)&#39;)+
  scale_fill_discrete(labels=c(&quot;Bio 1530&quot;, &quot;Sci 1420&quot;))+
  labs(title=&quot;Normalized Gain v. Course Grade by Course &amp; Instructor&quot;, subtitle=&quot;Mean and 95% CI of all sections per instructor (A-J)&quot;)+
  theme(plot.title=element_text(hjust=0.5))+
  theme(plot.subtitle=element_text(hjust=0.5))+
  annotate(&quot;text&quot;, x=73.0, y=0.09, label=&quot;R2 = 0.68, p = 0.044&quot;)+
  annotate(&quot;text&quot;, x=78.5, y=0.22, label=&quot;R2 = 0.46, p = 0.095&quot;)

huangapple
  • 本文由 发表于 2023年2月8日 22:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定