Bug? - Base graphics lines() function fails when run as single lines in R Notebook code chunk

Please ask your questions about R Markdown here.

This code chunk draws a fine base graphics graph when run as a chunk. However, if one uses ctrl-ENTER to run the chunk line by line, the lines() function produces the following error. It runs fine as line by line code in base R.

Reproducible code:

df <-
  structure(list(conc = c(0, 1.33904761904762, 2.67809523809524, 
6.6952380952381, 13.3904761904762, 20.0857142857143, 26.7809523809524, 
33.4761904761905, 40.1714285714286, 46.8666666666667, 53.5619047619048
), act = c(100, 94.4700460829493, 76.9585253456221, 48.3870967741935, 
33.6405529953917, 27.1889400921659, 19.815668202765, 18.8940092165899, 
19.815668202765, 20.7373271889401, 17.9723502304147)), class = "data.frame", row.names = c(NA, 
-11L))

RMSE <- function(fit, act){
    sqrt(mean((fit - act)^2))
}

expmodel <- nls(act ~  Const + A * exp(B * conc), data = df, start = list(Const = 25, A = 50, B = -0.1))
(modparm = summary(expmodel))


fit1 = fitted.values(expmodel)
err1 = RMSE(fit1, df$act)
print(err1)
# Base graphics plot
plot(df$conc, df$act)
lines(df$conc, predict(expmodel))

The traceback is as follows:

Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
3.
plot.xy(xy.coords(x, y), type = type, ...)
2.
lines.default(df$conc, predict(expmodel))
1.
lines(df$conc, predict(expmodel))

RStudio.Version()
$citation

To cite RStudio in publications use:

RStudio Team (2019). RStudio: Integrated Development for R. RStudio, Inc., Boston, MA URL
http://www.rstudio.com/.

A BibTeX entry for LaTeX users is

@Manual{,
title = {RStudio: Integrated Development Environment for R},
author = {{RStudio Team}},
organization = {RStudio, Inc.},
address = {Boston, MA},
year = {2019},
url = {http://www.rstudio.com/},
}

$mode
[1] "desktop"

$version
[1] ‘1.2.5033’

$release_name
[1] "Orange Blossom"

Using this document below, I don't have any error while :man_shrugging:

  • knitting the document
  • Executing the chunk as a whole
  • Executing line by line
---
output: html_document
---

```{r}
df <-
  structure(list(conc = c(0, 1.33904761904762, 2.67809523809524, 
6.6952380952381, 13.3904761904762, 20.0857142857143, 26.7809523809524, 
33.4761904761905, 40.1714285714286, 46.8666666666667, 53.5619047619048
), act = c(100, 94.4700460829493, 76.9585253456221, 48.3870967741935, 
33.6405529953917, 27.1889400921659, 19.815668202765, 18.8940092165899, 
19.815668202765, 20.7373271889401, 17.9723502304147)), class = "data.frame", row.names = c(NA, 
-11L))

RMSE <- function(fit, act){
    sqrt(mean((fit - act)^2))
}

expmodel <- nls(act ~  Const + A * exp(B * conc), data = df, start = list(Const = 25, A = 50, B = -0.1))
(modparm = summary(expmodel))


fit1 = fitted.values(expmodel)
err1 = RMSE(fit1, df$act)
print(err1)
# Base graphics plot
plot(df$conc, df$act)
lines(df$conc, predict(expmodel))
```

Can you upgrade to last RStudio version which is 1.3 ?

Thank you.

This topic was automatically closed 21 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.