kRazzy R
kRazzy R

Reputation: 1589

when calling lapply or for loop , Plots not getting displayed iteratively in the html generated by knitting code in R markdown

My data frame 'df' looks like this

C1 C2 C3 C4   
AA 10 ab 9  
AA 11 ac 10  
AA 12 ae 11   
BB 10 ab 20  
BB 11 ab 30  
BB 12 ab 2  
CC 10 ax 20  
CC 12 ad 20  
DD 17 ae 23  

and so on.. 13000 rows.

In R Studio I am able to iteratively display/generate individual bar charts using plotly for each unique value in C1 in the following way :

abcd<-c('AA','BB','CC', 'DD')  
        ## when using all of the data frame I plan to do : abcd<-df$C1

        myfun<- function(j)
        {


          main<-paste("Printing  ", abcd[j],"  <br> (Hover for values) " )

          df %>%
            filter(C1==abcd[j])%>%
            select(C1,C2,C3,C4) %>%
            group_by(C1,C2,C3) %>%
            summarise(value=sum(C4))-> new_df


          p<-(plot_ly(new_df, x= ~C2, y = ~value, color = ~C3, type = "bar", alpha = 0.9) %>%
                layout(title = main, xaxis = list(title ="", yaxis = list(title = "Test Title")))%>% layout(barmode = "stack"))

          p
        }
        lapply(1:4, myfun)

I was also able to do the same thing using a for loop over 'abcd'

Here is my Rmarkdown code:

    ---
    title: 'Apple bees'
    author: "Krazzy R"
    date: "28 March 2017"
    output:
      html_document:
        highlight: pygments
        pdf_document: default
        theme: spacelab
      pdf_document: default
      word_document: default
    ---

    ```{r setup, include=FALSE}
    library(knitr)

    knitr::opts_chunk$set(tidy.opts=list(width.cutoff=120),tidy=TRUE,echo = TRUE)
    ```

    ```{r, include=TRUE,results='hide', message=FALSE, warning=FALSE, echo=TRUE}
    library(plyr)
    library(dplyr)
    library(ggplot2)
    library(readr)
    require(knitr)
    require(plotly)
    ```
    ```{r, echo= TRUE)
    df<- read.csv("my_csv.csv")

    abcd<-c('AA','BB','CC', 'DD')  
        ## when using all of the data frame I plan to do : abcd<-df$C1

        myfun<- function(j)
        {
          main<-paste("Printing  ", abcd[j],"  <br> (Hover for values) " )

          df %>%
            filter(C1==abcd[j])%>%
            select(C1,C2,C3,C4) %>%
            group_by(C1,C2,C3) %>%
            summarise(value=sum(C4))-> new_df


          p<-(plot_ly(new_df, x= ~C2, y = ~value, color = ~C3, type = "bar", alpha = 0.9) %>%
                layout(title = main, xaxis = list(title ="", yaxis = list(title = "Test Title")))%>% layout(barmode = "stack"))

          p
        }

    ```

    ```{r, echo=TRUE}
    myfun(1)
    myfun(2)
    myfun(3)

    ```

When I explicitly call
myfun(1)
myfun(2) etc.
the plots are displayed in the knitted html.
However I have 200 unique values in C1 and I want to display all of them iteratively.

So I tried

1.

      ```{r, echo=TRUE}
        lapply(1:4,myfun)

        ```

But this doesn't display any plots.

Then I tried:
2.

    ```{r, echo =TRUE}
    print(lapply(1:4, myfun))
    ```

Still no outputs

So
3.

    ```{r, echo =TRUE}
    for(i in 1:4){
    (myfun(i))
    }
    ```

Again no plots.
4. So yried using for loop

        ```{r, echo= TRUE} 
        abcd<-c('AA','BB','CC', 'DD')  
                ## when using all of the data frame I plan to do : abcd<-df$C1

                for(j in 1:4){

       main<-paste("Printing  ", abcd[j],"  <br> (Hover for values) " )

              df %>%
                filter(C1==abcd[j])%>%
                select(C1,C2,C3,C4) %>%
                group_by(C1,C2,C3) %>%
                summarise(value=sum(C4))-> new_df


              p<-(plot_ly(new_df, x= ~C2, y = ~value, color = ~C3, type = "bar", alpha = 0.9) %>%
                    layout(title = main, xaxis = list(title ="", yaxis = list(title = "Test Title")))%>% layout(barmode = "stack"))

              p 
    # no change when using print(p)

            }

    ```   

No plots.
However if I move the p to outside the for loop it prints only one plot corresponding to the last value of the loop. What am I missing? How do I fix this? Thanks in advance.

Upvotes: 1

Views: 656

Answers (1)

GGamba
GGamba

Reputation: 13680

lapply() returns a list that has to be rendered as HTML when the document is knitted.
To do that you should use htmltools::tagList()

```{r, echo=TRUE}
htmltools::tagList(lapply(1:4,myfun))
```

Note that this returns a white space if rendered 'inline'

Upvotes: 2

Related Questions