Nathan
Nathan

Reputation: 101

Error in R: 'Object not found', code not working in R Markdown

I have a peculiar situation where a chunk of code is working for me when I run it in the console, but when I try to knit it in an R Markdown file, I am getting an error. Here is the code:

    {r,echo=FALSE, message=FALSE, fig.width=3, fig.height=3}
    library(cdcfluview)
    library(mosaic)
    library(ggplot2)
    library(dplyr)

    neflu <- get_flu_data("census", "1", "ilinet", years=1997:2016)
    neflu[neflu=="X"]<-NA

    wk_new <- rep(NA, nrow(neflu))
    season_new <- rep('0', nrow(neflu))

    for(i in 1:nrow(neflu)){
     if(neflu$WEEK[i] == 40){
      wk_num <- 1
     }
    wk_new[i] <- wk_num
    wk_num <- wk_num+1

    if(neflu$WEEK[i] < 40) {
       season_new[i] <- paste(neflu$YEAR[i]-1, '-', neflu$YEAR[i])
     } else {
      season_new[i] <- paste(neflu$YEAR[i], '-', neflu$YEAR[i]+1)
     }
  }

  neflu_new <- cbind(neflu, wk_new, season_new)


  neflu_agg <- neflu_new %>%
  mutate(weighted_ili = as.numeric(X..WEIGHTED.ILI))%>%
  group_by(REGION.TYPE, season_new) %>%
  summarize(peak_ili = max(weighted_ili, na.rm=TRUE),
        cum_ili = sum(weighted_ili,na.rm=TRUE),
        peak_wk = wk_new[which.max(weighted_ili)])

  neflu_agg2 <- subset(neflu_agg, peak_wk != 5, select=c(REGION.TYPE,season_new,peak_ili,cum_ili, peak_wk))

  plot(peak_wk, peak_ili, main= "Peak ILI vs Peak Week", xlab= "Peak Week", ylab= "Peak ILI")
  nelm1 <- lm(peak_ili~1+peak_wk)
  abline(nelm1, col='red')
  plot(peak_ili, cum_ili, main= "Cumulative ILI vs Peak ILI", xlab= "Peak ILI", ylab= "Cumulative ILI")
  nelm2 <- lm(cum_ili~1+peak_ili)
  abline(nelm2, col='blue')

And here is the error message I am getting:

   The object peak_wk not found Calls:<Anonymous> . . . 
   WithCallingHandlers -> withVisible -> eval -> eval -> plot execution halted".

I have tried changing the code this this, but am still getting errors:

    {r,echo=FALSE, message=FALSE, fig.width=3, fig.height=3}
    library(cdcfluview)
    library(mosaic)
    library(ggplot2)
    library(dplyr)

    neflu <- get_flu_data("census", "1", "ilinet", years=1997:2016)
    neflu[neflu=="X"]<-NA

    wk_new <- rep(NA, nrow(neflu))
    season_new <- rep('0', nrow(neflu))

    for(i in 1:nrow(neflu)){
     if(neflu$WEEK[i] == 40){
      wk_num <- 1
     }
    wk_new[i] <- wk_num
    wk_num <- wk_num+1

    if(neflu$WEEK[i] < 40) {
       season_new[i] <- paste(neflu$YEAR[i]-1, '-', neflu$YEAR[i])
     } else {
      season_new[i] <- paste(neflu$YEAR[i], '-', neflu$YEAR[i]+1)
     }
  }

  neflu_new <- cbind(neflu, wk_new, season_new)

  weighted_ili <- mutate(neflu_new, weighted_ili = as.numeric(X..WEIGHTED.ILI))
  peak_ili <- max(weighted_ili, na.rm=TRUE)
  cum_ili <- sum(weighted_ili,na.rm=TRUE)
  peak_wk <- wk_new[which.max(weighted_ili)]

  neflu_agg <- neflu_new %>%
  mutate(weighted_ili = as.numeric(X..WEIGHTED.ILI))%>%
  group_by(REGION.TYPE, season_new) %>%
  summarize(peak_ili = max(weighted_ili, na.rm=TRUE),
        cum_ili = sum(weighted_ili,na.rm=TRUE),
        peak_wk = wk_new[which.max(weighted_ili)])

  neflu_agg2 <- subset(neflu_agg, peak_wk != 5, select=c(REGION.TYPE,season_new,peak_ili,cum_ili, peak_wk))

  plot(peak_wk, peak_ili, main= "Peak ILI vs Peak Week", xlab= "Peak Week", ylab= "Peak ILI")
  nelm1 <- lm(peak_ili~1+peak_wk)
  abline(nelm1, col='red')
  plot(peak_ili, cum_ili, main= "Cumulative ILI vs Peak ILI", xlab= "Peak ILI", ylab= "Cumulative ILI")
  nelm2 <- lm(cum_ili~1+peak_ili)
  abline(nelm2, col='blue')

Here is the error I get now:

    Error in FUN(X[[i]],...): only defined on data frame with all 
    numeric variables Calls:

Any help with just getting this code to knit would be greatly appreciated. I am sure I am just missing something very obvious but I can't figure it out.

Upvotes: 0

Views: 1804

Answers (1)

Nathan
Nathan

Reputation: 101

I now realize why I was getting the error, with help from lmo:

peak_wk, peak_ili, and cum_ili were not defined in the global environment. I had to specify where the plot() function was pulling the data from:

    attach(neflu_agg2)

This worked just fine.

Upvotes: 1

Related Questions