generate plots in loop with knitr - possible bug in evaluate #14

Closed
szilard opened this Issue Jun 9, 2012 · 13 comments

Comments

Projects
None yet
7 participants
@szilard

szilard commented Jun 9, 2012

I'm trying to generate text-plot pairs with knitr (-> markdown -> html). I have reported a bug on the knitr list and I was referred here, see below:

```{r echo=FALSE}
for(i in 1:3) {
    cat(i)
    plot(i)
}

I'd like to get this:

1
plot1
2
plot2
...

Instead, I get this:

1
2
plot1
3
plot2
plot3

Yihui referred me to:

evaluate("
for(i in 1:3) {
    cat(i)
    plot(i)
}")

which gives:
...
[[3]]
[1] "1"

[[4]]
[1] "2"

[[5]]

[[6]]
[1] "3"

[[7]]

[[8]]

instead of

[[3]]
[1] "1"

[[4]]

[[5]]
[1] "2"

[[6]]

[[7]]
[1] "3"

[[8]]

Originally reported on the knitr list:
https://groups.google.com/group/knitr/browse_thread/thread/ddc3bb29c4c953e8

@hadley

This comment has been minimized.

Show comment Hide comment
@hadley

hadley Jun 11, 2012

Member

Thanks. I don't know when I'll get a chance to work on this though :/

Member

hadley commented Jun 11, 2012

Thanks. I don't know when I'll get a chance to work on this though :/

@yihui yihui referenced this issue in yihui/knitr Jul 5, 2012

Closed

What’s the problem? #307

@ajschumacher

This comment has been minimized.

Show comment Hide comment
@ajschumacher

ajschumacher Aug 2, 2012

This same bug is biting me - some more example cases here:

https://groups.google.com/forum/?fromgroups#!topic/knitr/nt36wDzzXSA

At least for one case, cat'ing out newlines seems related to the problem somehow. Weird.

This same bug is biting me - some more example cases here:

https://groups.google.com/forum/?fromgroups#!topic/knitr/nt36wDzzXSA

At least for one case, cat'ing out newlines seems related to the problem somehow. Weird.

@hadley hadley closed this in 790cb27 Sep 24, 2012

@jeroen

This comment has been minimized.

Show comment Hide comment
@jeroen

jeroen Sep 25, 2012

Contributor

Is this also supposed to work when this happens within a function?

test <- function(){
    for(i in 1:2) {
      cat(i)
      plot(i)
    }
}

output <- evaluate("test()")
Contributor

jeroen commented Sep 25, 2012

Is this also supposed to work when this happens within a function?

test <- function(){
    for(i in 1:2) {
      cat(i)
      plot(i)
    }
}

output <- evaluate("test()")
@ajschumacher

This comment has been minimized.

Show comment Hide comment
@ajschumacher

ajschumacher Sep 26, 2012

For my use case, generating reports with knitr, I would want it to 'work' within functions (i.e., plots appear when generated) unless the function is just returning the plot, in which case the plot would show up when it was "done", meaning top level, I guess; honestly I'm not even 100% sure how that would all work. Would knitr's fig.keep chunk option be relevant? Not sure. I haven't checked the latest re: Hadley's commit, so I don't know what the current behavior is looking like. Looking forward to increased awesomeness!

For my use case, generating reports with knitr, I would want it to 'work' within functions (i.e., plots appear when generated) unless the function is just returning the plot, in which case the plot would show up when it was "done", meaning top level, I guess; honestly I'm not even 100% sure how that would all work. Would knitr's fig.keep chunk option be relevant? Not sure. I haven't checked the latest re: Hadley's commit, so I don't know what the current behavior is looking like. Looking forward to increased awesomeness!

@jeroen

This comment has been minimized.

Show comment Hide comment
@jeroen

jeroen Sep 26, 2012

Contributor

Hmz I am getting mixed results for this bug... I suspect some kind of race condition....

Contributor

jeroen commented Sep 26, 2012

Hmz I am getting mixed results for this bug... I suspect some kind of race condition....

@yihui

This comment has been minimized.

Show comment Hide comment
@yihui

yihui Sep 26, 2012

Collaborator

@ajschumacher the current behavior is what @szilard described as the normal behavior. You do not need any special settings in knitr.

Collaborator

yihui commented Sep 26, 2012

@ajschumacher the current behavior is what @szilard described as the normal behavior. You do not need any special settings in knitr.

@ajschumacher

This comment has been minimized.

Show comment Hide comment
@ajschumacher

ajschumacher Sep 26, 2012

Thanks all!

Thanks all!

@szilard

This comment has been minimized.

Show comment Hide comment
@szilard

szilard Sep 26, 2012

It seems now that (with that change or something else) plot rendering in knitr does not work anymore!

I did install_github("evaluate") to test the above and now even this does not render plots:

plot(1)

Reinstalling the CRAN evaluate brings back the plotting.

szilard commented Sep 26, 2012

It seems now that (with that change or something else) plot rendering in knitr does not work anymore!

I did install_github("evaluate") to test the above and now even this does not render plots:

plot(1)

Reinstalling the CRAN evaluate brings back the plotting.

@yihui

This comment has been minimized.

Show comment Hide comment
@yihui

yihui Sep 27, 2012

Collaborator

That is kind of expected. At least I know this will bring a trouble to knitr:

6cece5f#commitcomment-1899127

Do not start using the development version here until it is released to CRAN. I'll test knitr after Hadley fixes the above issue.

Collaborator

yihui commented Sep 27, 2012

That is kind of expected. At least I know this will bring a trouble to knitr:

6cece5f#commitcomment-1899127

Do not start using the development version here until it is released to CRAN. I'll test knitr after Hadley fixes the above issue.

@szilard

This comment has been minimized.

Show comment Hide comment
@szilard

szilard Sep 27, 2012

thanks

-szilard

On 9/26/12 5:13 PM, Yihui Xie wrote:

That is kind of expected. At least I know this will bring a trouble to
knitr:

6cece5f#commitcomment-1899127
6cece5f#commitcomment-1899127

Do not start using the development version here until it is released to
CRAN. I'll test knitr after Hadley fixes the above issue.


Reply to this email directly or view it on GitHub
hadley#14 (comment).

szilard commented Sep 27, 2012

thanks

-szilard

On 9/26/12 5:13 PM, Yihui Xie wrote:

That is kind of expected. At least I know this will bring a trouble to
knitr:

6cece5f#commitcomment-1899127
6cece5f#commitcomment-1899127

Do not start using the development version here until it is released to
CRAN. I'll test knitr after Hadley fixes the above issue.


Reply to this email directly or view it on GitHub
hadley#14 (comment).

@hadley hadley reopened this Oct 1, 2012

@hadley

This comment has been minimized.

Show comment Hide comment
@hadley

hadley Oct 1, 2012

Member

@jeroenooms does that behaviour not work as expected? You get two plots interleaved with the output text.

Member

hadley commented Oct 1, 2012

@jeroenooms does that behaviour not work as expected? You get two plots interleaved with the output text.

@Freedomtowin

This comment has been minimized.

Show comment Hide comment
@Freedomtowin

Freedomtowin Feb 11, 2017

If you use plot.new() after each plot, it will allow duplicates to show up correctly.

If you use plot.new() after each plot, it will allow duplicates to show up correctly.

@samyishak

This comment has been minimized.

Show comment Hide comment
@samyishak

samyishak Feb 3, 2018

This is years old now, but I'm wondering if there's any hope on this issue. I'm trying to interleave text and plot outputs via loop to an R Notebook preview in RStudio.

Case 1: works for the output I want [text, plot, text, plot], but requires putting the plot before the text:

```{r, results='asis'}
for(i in 1:2) {
  plot(i)
  cat(i)
}

Case 2: how I would expect to get Case 1, but instead outputs [text, text, plot, plot]:

```{r, results='asis'}
for(i in 1:2) {
  cat(i)
  plot(i)
}

This pattern seems to hold if you want more than one plot, but you only need the first plot to precede the text to get the intended output of [text, many plots, text, many plots]:

```{r, results='asis'}
for(i in 1:2) {
  plot(i)
  cat(i)
  plot(i*2)
  plot(i*3)
}
Session info -------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.408)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Phoenix             
 date     2018-02-02                  

Packages -----------------------------------------------------------------------------------------
 package       * version date       source        
 base          * 3.4.3   2018-01-10 local         
 compiler        3.4.3   2018-01-10 local         
 curl            3.1     2017-12-28 local         
 datasets      * 3.4.3   2018-01-10 local         
 devtools        1.13.4  2017-11-09 CRAN (R 3.4.3)
 digest          0.6.13  2017-12-14 CRAN (R 3.4.3)
 graphics      * 3.4.3   2018-01-10 local         
 grDevices     * 3.4.3   2018-01-10 local         
 jsonlite        1.5     2017-06-01 CRAN (R 3.3.2)
 memoise         1.1.0   2017-04-21 CRAN (R 3.4.3)
 methods       * 3.4.3   2018-01-10 local         
 RevoUtils     * 10.0.7  2018-01-02 local         
 RevoUtilsMath * 10.0.1  2017-09-19 local         
 RPushbullet   * 0.3.1   2017-02-17 CRAN (R 3.4.3)
 stats         * 3.4.3   2018-01-10 local         
 tools           3.4.3   2018-01-10 local         
 utils         * 3.4.3   2018-01-10 local         
 withr           2.1.1   2017-12-19 CRAN (R 3.4.3)
 yaml            2.1.16  2017-12-12 CRAN (R 3.4.3)

Also want to say everything coming from RStudio and the tidyverse is just fantastic, so thank you.

This is years old now, but I'm wondering if there's any hope on this issue. I'm trying to interleave text and plot outputs via loop to an R Notebook preview in RStudio.

Case 1: works for the output I want [text, plot, text, plot], but requires putting the plot before the text:

```{r, results='asis'}
for(i in 1:2) {
  plot(i)
  cat(i)
}

Case 2: how I would expect to get Case 1, but instead outputs [text, text, plot, plot]:

```{r, results='asis'}
for(i in 1:2) {
  cat(i)
  plot(i)
}

This pattern seems to hold if you want more than one plot, but you only need the first plot to precede the text to get the intended output of [text, many plots, text, many plots]:

```{r, results='asis'}
for(i in 1:2) {
  plot(i)
  cat(i)
  plot(i*2)
  plot(i*3)
}
Session info -------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.408)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Phoenix             
 date     2018-02-02                  

Packages -----------------------------------------------------------------------------------------
 package       * version date       source        
 base          * 3.4.3   2018-01-10 local         
 compiler        3.4.3   2018-01-10 local         
 curl            3.1     2017-12-28 local         
 datasets      * 3.4.3   2018-01-10 local         
 devtools        1.13.4  2017-11-09 CRAN (R 3.4.3)
 digest          0.6.13  2017-12-14 CRAN (R 3.4.3)
 graphics      * 3.4.3   2018-01-10 local         
 grDevices     * 3.4.3   2018-01-10 local         
 jsonlite        1.5     2017-06-01 CRAN (R 3.3.2)
 memoise         1.1.0   2017-04-21 CRAN (R 3.4.3)
 methods       * 3.4.3   2018-01-10 local         
 RevoUtils     * 10.0.7  2018-01-02 local         
 RevoUtilsMath * 10.0.1  2017-09-19 local         
 RPushbullet   * 0.3.1   2017-02-17 CRAN (R 3.4.3)
 stats         * 3.4.3   2018-01-10 local         
 tools           3.4.3   2018-01-10 local         
 utils         * 3.4.3   2018-01-10 local         
 withr           2.1.1   2017-12-19 CRAN (R 3.4.3)
 yaml            2.1.16  2017-12-12 CRAN (R 3.4.3)

Also want to say everything coming from RStudio and the tidyverse is just fantastic, so thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment