Also check file paths from the knitting directory. Moving a document into a reports folder changes what a relative data path may refer to; changing an unrelated console directory can hide the real problem.
Separate an object problem from a file problem
Suppose this original fictional project contains:
project/
data/scores.csv
reports/note.Rmd
The CSV contains a score column with values 4, 6 and 8. In an interactive console you previously ran a read.csv command that created scores. A chunk containing only mean(scores$score) can then succeed in that console even though note.Rmd never imports the file.
The clean report cannot depend on that previous console action. Add the import before the calculation in the document. For this exact layout and a knitting root of the Rmd's directory, the relative path is ../data/scores.csv.
# Import chunk, before the summary chunk
scores <- read.csv("../data/scores.csv")
stopifnot("score" %in% names(scores))
stopifnot(is.numeric(scores$score), !anyNA(scores$score))
# Later summary chunk
mean_score <- mean(scores$score)
mean_score
The hand-calculated expected mean is 6. This excerpt is a diagnostic teaching example; it is not a claimed R execution or complete report. Preserve any different missing-value policy your actual analysis requires.
Inspect where knitting looks for files
The R Markdown Cookbook's working-directory explanation describes the default document-directory behavior and ways to set the knitting root. The render reference documents knit_root_dir and the evaluation environment. Both were checked October 24, 2026.
Use a temporary diagnostic chunk to inspect the actual run:
getwd()
file.exists("../data/scores.csv")
If the file check is false, compare the displayed directory with the project tree. Do not solve a portable project by hard-coding a path to one person's desktop. Choose one root convention, document it and make paths agree with it.
For the supplied tree, reports/data/scores.csv would be a different location from data/scores.csv at the project root. A successful file check verifies existence at that path; inspect the contents to confirm it is also the intended dataset.
Find the earliest cause before suppressing errors
| Observation | Check next |
|---|---|
| Object not found | Earlier chunk creates the exact object name and actually evaluates |
| Cannot open a file | Actual knitting root, relative path, filename case and file availability |
| Function not found | Required package is installed and loaded in the document's environment |
| Old result remains | Whether the output was regenerated and whether caching affects that chunk |
Start with the first error, since later failures may be consequences. An object can also be absent because an earlier chunk has eval=FALSE. Check chunk options before copying data into the console again.
Do not hide the failure with a fabricated value or suppress it merely to obtain an output file. An output containing an error message is not the same as a completed analysis.
Render and inspect the corrected document
Save the source and data, restart the R session, and render note.Rmd with the project's actual installed dependencies. Confirm that every object needed by the summary is produced by that document or its explicitly loaded dependencies. Keep the actual first error if rendering still fails.
For the fictional three scores, inspect that the regenerated report shows the intended rows and a mean of 6. If you change 8 to 11, the new hand-calculated mean is 7; re-render and verify the new output. That small change distinguishes a regenerated result from an old HTML file still open in the browser.
The R Markdown template guide covers building a report whose prose and table share source objects. This troubleshooting task ends when the native document renders in the intended clean environment and its actual output matches the source you plan to distribute.