Code chunk wont stop executing

I hope this has an easy answer. When i am running very simple code chunks like this

x <- sample(x=1:100, size=100, replace=TRUE)
mean(x)

or

y <- x
y[sample(x=1:100, size=20, replace=FALSE)] <- NA
mean(y)
mean(y, na.rm=TRUE)
quantile(x, probs=c(0.25, 0.75))

After about 6 or 7 chunks I get one where the execution wheel just keeps spinning?

This only happens for inline output, I have no problem if I output to the console. Its also not a particular chunk of code that doesn't work. I can switch them around and it still happens after 6 or 7 chunks.

I'm using RStudio v1.1.463 and R v3.5.1
(I also reinstalled everything to check if that was the problem)

If I've understood your question, which is dicey without FAQ: What's a reproducible example (`reprex`) and how do I do one?, your problem is that you are trying to do inline assignments. That dog won't hunt. If you make your assignments within blocks the variables are available for indefinite inline use. Try this Rmd examplle

---
title: "StuckChunk.Rmd"
author: "Richard Careaga"
date: "12/16/2018"
output: html_document
---

# First iteration

<pre>
Here is the first inline: "r x <- sample(x=1:100, size=100, replace=TRUE) mean(x)" (backticks omitted)
</pre>

With the backticks, it throws an error. As it should, even from the console, because there are two statements on the same line.

> Error: unexpected symbol in "x <- sample(x=1:100, size=100, replace=TRUE) mean"

On the other hand, put the assignment of *x* in a code block with echo = TRUE if you want to show the assignment

```{r chunk1, results="asis", echo = TRUE, warning=FALSE}
x <- sample(x=1:100, size=100, replace=TRUE)

And you can inline mean(x)ras often as you like:

  1. r mean(x)
  2. r mean(x)
  3. r mean(x)
  4. r mean(x)
  5. r mean(x)
  6. r mean(x)
  7. r mean(x)
  8. r mean(x)
  9. r mean(x)
  10. r mean(x)
  11. r mean(x)
  12. r mean(x)
  13. r mean(x)
  14. r mean(x)
  15. r mean(x)

Likewise, assign r in a chunk

y <- x
y[sample(x=1:100, size=20, replace=FALSE)] <- NA

and you can do mean(y)rand mean(y, na.rm = TRUE)ras often as you like

  1. r mean(y)
  2. r mean(y, na.rm = TRUE)
  3. r mean(y)
  4. r mean(y, na.rm = TRUE)
  5. r mean(y)
  6. r mean(y, na.rm = TRUE)
  7. r mean(y)
  8. r mean(y, na.rm = TRUE)
  9. r mean(y)
  10. r mean(y, na.rm = TRUE)
  11. r mean(y)
  12. r mean(y, na.rm = TRUE)
  13. r mean(y)
  14. r mean(y, na.rm = TRUE)
  15. r mean(y)
  16. r mean(y, na.rm = TRUE)
  17. r mean(y)
  18. r mean(y, na.rm = TRUE)
  19. r mean(y)
  20. r mean(y, na.rm = TRUE)
  21. r mean(y)
  22. r mean(y, na.rm = TRUE)

And, since x is already defined, you don't need a new chunk for inline repetitions of quantile(x, probs=c(0.25, 0.75))

  1. r quantile(x, probs=c(0.25, 0.75))
  2. r quantile(x, probs=c(0.25, 0.75))
  3. r quantile(x, probs=c(0.25, 0.75))
  4. r quantile(x, probs=c(0.25, 0.75))
  5. r quantile(x, probs=c(0.25, 0.75))
  6. r quantile(x, probs=c(0.25, 0.75))
  7. r quantile(x, probs=c(0.25, 0.75))
  8. r quantile(x, probs=c(0.25, 0.75))
  9. r quantile(x, probs=c(0.25, 0.75))
  10. r quantile(x, probs=c(0.25, 0.75))
  11. r quantile(x, probs=c(0.25, 0.75))
  12. r quantile(x, probs=c(0.25, 0.75))
  13. r quantile(x, probs=c(0.25, 0.75))
  14. r quantile(x, probs=c(0.25, 0.75))
  15. r quantile(x, probs=c(0.25, 0.75))
  16. r quantile(x, probs=c(0.25, 0.75))
  17. r quantile(x, probs=c(0.25, 0.75))
1 Like

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