Having Trouble WIth Code Chunks in R Markdown

I apologize if this seems obvious, but I have only been working with R studio for a couple of days.

I am trying to run a head function in R Markdown off of a dataset I imported for a project:

The code so far is this:

daily_activity <- read_csv("dailyActivity_merged.csv")
daily_calories <- read_csv("dailyCalories_merged.csv")
daily_intensities <- read_csv("dailyIntensities_merged.csv")
daily_steps <- read_csv("dailySteps_merged.csv")
daily_sleep <- read_csv("sleepDay_merged.csv")
weight_log <- read_csv("weightLogInfo_merged.csv")
head(daily_activity)

When I run the head function, I get the following error:

Error: ! object 'daily_activity' not found
Backtrace: 1. utils::head(daily_activity)
Quitting from lines 96-97 [unnamed-chunk-2] (Bellabeat-Notebook.Rmd) Execution halted

I'm obviously doing something wrong. What are my options to fix this?

Thank you in advance. I also apologies if this was hard to read. I'm not quite familiar with the formatting yet.

Hi @spbynum,
could you share the dailyActivity_merged.csv - or a small portion of it?

Certainly!

head(daily_activity)

Id ActivityDate TotalSteps TotalDistance TrackerDistance LoggedActivitiesDistance VeryActiveDistance
1 1503960366 4/12/2016 13162 8.50 8.50 0 1.88
2 1503960366 4/13/2016 10735 6.97 6.97 0 1.57
3 1503960366 4/14/2016 10460 6.74 6.74 0 2.44
4 1503960366 4/15/2016 9762 6.28 6.28 0 2.14
5 1503960366 4/16/2016 12669 8.16 8.16 0 2.71
6 1503960366 4/17/2016 9705 6.48 6.48 0 3.19
ModeratelyActiveDistance LightActiveDistance SedentaryActiveDistance VeryActiveMinutes FairlyActiveMinutes
1 0.55 6.06 0 25 13
2 0.69 4.71 0 21 19
3 0.40 3.91 0 30 11
4 1.26 2.83 0 29 34
5 0.41 5.04 0 36 10
6 0.78 2.51 0 38 20
LightlyActiveMinutes SedentaryMinutes Calories
1 328 728 1985
2 217 776 1797
3 181 1218 1776
4 209 726 1745
5 221 773 1863
6 164 539 1728

Apologies for the formatting. I'll add a picture for good measure.

Are you running the head(daily_activity) command using the Ctrl + Enter key shortcut? Remember, that particular shortcut only runs the current code line not the previous ones so maybe you need to run daily_activity <- read_csv("dailyActivity_merged.csv") first.

Thank you for your reply! Please forgive my ignorance, but how might that look in a syntax format?

Sean Bynum

Your csv looks fine.

There are multiple possibilities of what can go wrong:

  1. as @andresrcs pointed out maybe you are calling head(daily_activity) to inspect it without first calling the previous chunk (eihter clicking on the green arrow above the chunk or selecting it and hitting ctrl + enter)
  2. since you didn't share your R markdown document - something like
---
title: "Posit 188830"
author: "spbynum"
date: "2024-07-07"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readr)
```

## First header

```{r}
daily_activity <- read_csv("dailyActivity_merged.csv")
```

## Second header


```{r}
head(daily_activity)
```

we can't see if you accidentally set something like

```{r, eval = FALSE}
daily_activity <- read_csv("dailyActivity_merged.csv")
```

which would cause the error since it means show the code but don't execute it - leading to the next chunk

```{r}
head(daily_activity)
```

not knowing about a daily_activity variable.

Providing the R markdown file (in a condensed way) would help.

I see what you mean. I appreciate the help. I’ll give it a try later today and report back!

Sean Bynum

It looks like it worked! I was not running the codes in the r markdown document, thinking that running the codes elsewhere was supposed to work here. I was mistaken.

I appreciate all your help! Working through this on my own can be a bit of a slog.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.