Having issues with knitting my .rmd file

I'm a complete beginner to Rstudio and have little coding experience, I've tried my best to troubleshoot my knitting issues myself but I can't make any headway. I think it's a simple issue but I still don't know where to start.

If anyone could provide some help I would be very grateful!

Hi, welcome to the forum.

I think we need to see your code and some sample data.

If the file is not too large could you copy it and paste it here between
```

```

If there is a lot of pure text, you can probably just delete much of it as what we probably need to see is the YAML at the top of the file and the R code.

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

For general interest, you may find these useful:
FAQ Asking Questions
How to format your code

Hi, thank you for the reply. I'm not sure if this is what you're asking for, but I've copied and pasted the first 90 lines of code deleting most of the text.

{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.height=3)


{r setup, echo=FALSE,message=FALSE}
## Do not delete this!
## It loads the s20x library for you. If you delete it 
## your document may not compile
library(s20x)


## Read in and inspect the data:
{r,fig.height=4.2,fig.width=6}
Galton.df=read.csv("Galton3.csv", header=T)
hist(Galton.df$Height)
summary(Galton.df$Height)

# t-statistic for H0: mu=70 :
n=length(Galton.df$Height)
SE=(sd(Galton.df$Height)/sqrt(n))
tstat=(mean(Galton.df$Height)-70)/SE = -13.5137


# 95% confidence interval for the mean:
tmult= qt(1-.05/2, df=n-1)
mean(Galton.df$Height) - tmult*SE = 66.32253
mean(Galton.df$Height) + tmult*SE = 67.25919

CI = (66.32253 67.25919)


##  Repeat the same calculation using the t.test function (done for you):
{r}
t.test(Galton.df$Height, mu=70)

I'm working with two datasets, please let me know if you need the second outputted using the dput function.

structure(list(Height = c(73.2, 73.5, 68, 68.5, 68, 69.5, 73, 
66, 66, 65.5, 68, 65, 67, 66.69999695, 66.5, 71.5, 66, 62.70000076, 
70, 68.5, 67, 66, 65.5, 66, 70.5, 67, 63, 67.5, 64, 70, 74, 68, 
65.69999695, 74, 73.19999695, 67, 66, 71, 66, 70.5, 72, 73, 68.5, 
62, 68, 70.5, 68, 70, 64.5, 66, 65, 70, 61.5, 70.5, 64, 64.5, 
62, 71.5, 70, 66, 75, 66, 67.5, 66, 68.5, 65, 64.5, 65, 70, 62, 
65, 69, 66.5, 67, 66.69999695, 70, 65, 60, 65, 69.5, 67, 69, 
67.5, 63.70000076, 66.5, 72, 69.19999695, 72, 67, 64.5, 65, 64.5, 
66, 71, 64, 65, 70, 72, 63, 61)), row.names = c(NA, 100L), class = "data.frame")

Thank you for taking the time to reply, I'm hoping what I've done is enough.

willo2000

I think we also need the YAML. That is the "prologue" at the very beginning of the file. It is the text located betweet
---

---

Here is an example

---
title: "sss"
author: "jrk"
date: "`r Sys.Date()`"
output: html_document
---

Apologies, I wasn't aware of what the YAML was. This is what's in between:

---
title: "STATS 201/8 Assignment 2"
author: "William 529585223"
date: 'Due Date: 3pm, Tuesday 19th March 2024'
output:
  html_document:
    fig_caption: yes
    number_sections: yes
  pdf_document:
    number_sections: yes
  word_document:
    number_sections: yes
---

I should have realized that if you are just getting started. My fault.

I think I've found the problems.

tstat <-  (mean(Galton.df$Height) - 70)/SE  =  -13.5137
mean(Galton.df$Height) - tmult * SE  =  66.32253
mean(Galton.df$Height) + tmult * SE  =  67.25919

You have already calculated SE above plus the "=" in " SE = 67.25919" is illegal.

What you want is

tstat <-  (mean(Galton.df$Height) - 70)/SE 

If you are working from a template then
{r setup, echo=FALSE,message=FALSE}

Do not delete this!

It loads the s20x library for you. If you delete it

your document may not compile

library(s20x)

is likely needed most of the time but it is redundant here as you are not using any data or functions from the {s20x} library.

And just to whinge a bit and be pedantic, your formatting is painful to read.

Assignments, for example x = y+2 should be written the assignment operator and preferably with spacing. See example at the bottom of the message.

x <- y + 2

Anyway enough whining. This should run.

---
title: "STATS 201/8 Assignment 2"
author: "William 529585223"
date: 'Due Date: 3pm, Tuesday 19th March 2024'
output:
  html_document:
    fig_caption: yes
    number_sections: yes
  pdf_document:
    number_sections: yes
  word_document:
    number_sections: yes
---
{r global_options, echo = FALSE}
knitr::opts_chunk$set(fig.height=3)
## Do not delete this!
## It loads the s20x library for you. If you delete it 
## your document may not compile
library(s20x)

Read in and inspect the data:

Galton.df=read.csv("Galton3.csv", header=T)
hist(Galton.df$Height)
summary(Galton.df$Height)

# t-statistic for H0: mu=70 :
n=length(Galton.df$Height)
SE=(sd(Galton.df$Height)/sqrt(n))
tstat=(mean(Galton.df$Height)-70)/SE


# 95% confidence interval for the mean:
tmult= qt(1-.05/2, df=n-1)
mean(Galton.df$Height) - tmult*SE
mean(Galton.df$Height) + tmult*SE 

CI = (66.32253 67.25919)

Repeat the same calculation using the t.test function (done for you):

t.test(Galton.df$Height, mu=70)

Formatting

Galton.df <-  read.csv("Galton3.csv", header  =  T)

hist(Galton.df$Height)

summary(Galton.df$Height)

n <-   length(Galton.df$Height)
SE  <-   (sd(Galton.df$Height)/sqrt(n))

tstat <-  (mean(Galton.df$Height) - 70)/SE
tmult  <-   qt(1 - 0.05/2, df  =  n - 1)

mean(Galton.df$Height) - tmult * SE 
mean(Galton.df$Height) + tmult * SE  
t.test(Galton.df$Height, mu  =  70)
1 Like

Thank you so so much. This worked perfectly, so the reason why it wouldn't knit was because of the illegal equality? what does {r setup, echo=FALSE,message=FALSE} do? I'm just trying to figure out what I can do to prevent similar errors like this in the future. And I do appreciate the formatting comment, even though this is part of my course I'd like to continue learning R outside of my classes.

My lecturer told me that the R community is often very helpful and I definitely agree. Thanks once again and have a good rest of your day/night.

2024-03-14 12:05 here (Ontario Canada)

Yes, it was just bad syntax.

{r setup, echo=FALSE,message=FALSE} 

"echo=FALSE" is saying that you do not want to see any of the text in the actual document,

"message=FALSE" is saying don't print any messages that loading a library, etc., might generate. I normally load two libraries at the start of a session . If I don't suppress the messages I get what you see below. This does not look good in a report or article. :grinning:

 library(data.table)
data.table 1.15.2 using 2 threads (see ?getDTthreads).  Latest news: r-datatable.com
> library(tidyverse)
── Attaching core tidyverse packages ────────────────────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::between()     masks data.table::between()
✖ dplyr::filter()      masks stats::filter()
✖ dplyr::first()       masks data.table::first()
✖ lubridate::hour()    masks data.table::hour()
✖ lubridate::isoweek() masks data.table::isoweek()
✖ dplyr::lag()         masks stats::lag()
✖ dplyr::last()        masks data.table::last()
✖ lubridate::mday()    masks data.table::mday()
✖ lubridate::minute()  masks data.table::minute()
✖ lubridate::month()   masks data.table::month()
✖ lubridate::quarter() masks data.table::quarter()
✖ lubridate::second()  masks data.table::second()
✖ purrr::transpose()   masks data.table::transpose()
✖ lubridate::wday()    masks data.table::wday()
✖ lubridate::week()    masks data.table::week()
✖ lubridate::yday()    masks data.table::yday()
✖ lubridate::year()    masks data.table::year()
ℹ Use the conflicted package to force all conflicts to become errors
> 
1 Like

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.