R Markdown编织报‘object not found’错误:单独运行代码正常的排查
问题解决: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
代码结构如下,单独运行ggplot代码块可正常生成散点图,但编织整体报告失败:
--- 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")
#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="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")
已确认交互环境下工作目录为/Users/robmccandless/Library/Mobile Documents/com~apple~CloudDocs/R Projects/Normalized_Gain_Data,且RMD文件与数据文件在同一目录下。
错误原因
- 数据未赋值给对象:
read_csv("bio1530_sci1420_summary_stats.xlsx")仅读取数据但未将其存储为变量,后续ggplot直接用文件名作为数据对象,自然找不到。 - 文件格式不匹配:
read_csv用于读取CSV文件,无法处理XLSX格式,这会导致数据读取失败(交互环境可能因缓存或其他巧合暂时可用,但编织时会暴露问题)。 - 编织工作目录差异:R Markdown编织时的工作目录可能与交互环境不同,即使文件在同一目录,也可能因路径解析问题找不到文件。
解决方案
步骤1:修正数据读取逻辑
- 安装并加载
readxl包(专门用于读取Excel文件) - 将读取的数据赋值给一个变量,比如
df - 结合
here包确保路径解析正确(避免工作目录差异问题)
步骤2:修改ggplot代码使用正确的对象
将ggplot(data=bio1530_sci1420_summary_stats.xlsx)改为使用赋值后的变量df
修正后的完整代码
--- title: "NGRMarkdown" author: "Rob McCandless" date: "`r Sys.Date()`" output: word_document ---
knitr::opts_chunk$set(echo = TRUE) # 安装readxl(首次运行需执行) # install.packages("readxl") library(ggplot2) library(ggrepel) library(tidyverse) library(here) library(readxl) # 读取数据并赋值给对象,用here包指定路径 df <- read_excel(here("bio1530_sci1420_summary_stats.xlsx"))
#ScatterPlot of mean course grade v. mean normalized gain on 1420 and 1530 data with regression lines and error bars 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")
额外验证(可选)
若仍有问题,可在数据读取代码块中添加以下代码,查看编织时的工作目录及文件列表,确认数据文件是否存在:
print(getwd()) list.files()
内容的提问来源于stack exchange,提问作者Rob McCandless
相关产品推荐
相关产品推荐

