26

I am using a recent version of Rstudio with an iMac

Version 1.0.44 – © 2009-2016 RStudio, Inc. Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko)

And I noticed the notebook function for rmarkdown files. When generating plots, the usual "Plots window" is not used any more, and the plots are generated just below the code chunk.

And I have an error for the following code:

plot(seq(1,10,1))
abline(a=0,b=1)

The error is showed below the code chunk :

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet

However, when knitting the whole rmarkdown file, there is no error.

So I would like to know how to avoid the error:

  • by using another code
  • by using the "Plots window"
  • or another way.
2
  • 1
    I cannot reproduce the error. Only if I put both commands in seperate chunks the error appears. Dec 3, 2016 at 12:06
  • 1
    I think that it is because I run the code line by line. When runing the entire R chunk in rmarkdown, I don't get errors.
    – John Smith
    Sep 2, 2017 at 10:42

5 Answers 5

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

35

The following will work

{plot(seq(1,10,1))
 abline(a=0,b=1)}
3
  • 5
    Add some explanation with answer for how this answer help OP in fixing current issue Jan 31, 2017 at 0:43
  • I don't really get why it works either, but can verify that it does. Must have something to do with the Rstudio markdown environment. Jun 21, 2017 at 18:11
  • 3
    Wrapping it in curly brackets ensures all lines are evaluated as one block. This answer, to an unrelated question, explains it nicely. stackoverflow.com/a/28758675/1454785
    – spops
    Dec 3, 2021 at 20:50
7

In RStudio, there's a setting in Preferences -> R Markdown to "Show output inline for all R Markdown documents". To get rid of the error, make sure this is unchecked.

5

This works too

plot(seq(1,10,1))+
abline(a=0,b=1)
2

In jupyter with R kernel, you will see that error if run the code line by line, just as XR SC mentioned.

0

This was happening to me because I was adding an invalid parameter to my plot. In my case I was trying to execute:

ggplot(df, aes(x=sales)) + geom_histogram() + title('Plot Title')

And should have been executing:

ggplot(df, aes(x=sales)) + geom_histogram() + ggtitle('Plot Title')

Notice that you must use ggtitle, not title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.