R markdown html document not properly show in Inte

2019-09-17 23:41发布

问题:

I create a rmarkdown html document and following is my code:

---
title: "PA" 
output: 
  html_document:
    css: custom.css
    theme: journal
    toc: true
    toc_float: 
      collapsed: false
      smooth_scroll: false
    toc_depth: 4
---
<style>
.list-group-item.active, .list-group-item.active:focus, .list-group-item.active:hover {
    background-color: blue;
}
</style>

<style type="text/css">
#TOC {
  color: purple; 
}
.toc-content {
  padding-left: 30px;
  padding-right: 40px;
}
h1 { /* Header 1 */
 color: DarkBlue;
}
</style>

<div class="datatables html-widget html-widget-static-bound"
     id="htmlwidget-3efe8ca4aa087193f03e"
     style="width:960px;height:500px;">

```{r, echo=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(lubridate)
library(ggplot2)
library(plotly)
library(DT)
library(knitr)
library(htmltools)
library(scales)
PA <- read.csv("PA.csv" , header = T , colClasses = c("Year"="factor", "Month"="factor"))
PA$Month <- as.numeric(as.character(PA$Month))
PA$Year <- as.numeric(as.character(PA$Year))
PA$date_label <- ymd( paste( PA$Year, month.abb[ PA$Month ], 01, sep = "-"))
```

```{r include = FALSE}
# If I want to use for-loop to print htmltools::tagList, this chunk is very important.
# However, I don't know why must need it.
DT::datatable(PA, extensions = 'Buttons', options = list(dom = 'Bfrtip',
    buttons = list(c(I('colvis'), 'copy', 'print'), list(extend = 'collection',
        buttons = c('csv', 'excel', 'pdf'),
        text = 'Download'
)), searchHighlight = TRUE
))
ggplotly(ggplot(PA))
plot_ly(PA)
```

```{r, echo=FALSE, message=FALSE, warning=FALSE, results = 'asis'}
red.bold.italic.text <- element_text(face = "bold.italic", color = "red")
colors <- c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')

for(i in unique(PA$Products)) {

  cat("  \n#",  i, "{.tabset .tabset-pills}  \n")

  cat(paste("## Summary"), sep = "\n")

  print(knitr::kable(mutate(summarise(group_by(PA[PA$Products == i,], Year, Company),sum(EPSUM), sum(Incurred)), LR.percent = `sum(Incurred)`/`sum(EPSUM)`/0.01), format = 'html'))

  cat(paste("## Line Graph {.tabset .tabset-pills}"), sep = "\n")

   cat(paste("### Line1"), sep = "\n")

   print(htmltools::tagList(
           ggplotly(ggplot(PA[PA$Products == i,], aes(x=date_label , y=EPSUM, col=Company , group=Company))
                  + geom_line(size = 1)
                  + scale_x_date(name = "Month", date_labels = "%b %Y", date_breaks = "2 month" )
                  + labs(title = "EPSUM", x = "Time", y = "") 
                  + theme(title = red.bold.italic.text, axis.title = red.bold.italic.text) 
                  + scale_y_continuous(labels = comma)
)))

   cat(paste("### Line2"), sep = "\n")

   print(htmltools::tagList(
           ggplotly(ggplot(PA[PA$Products == i,], aes(x=date_label , y=EPSUM, col=Company , group=Company))
                  + geom_line(size = 1)
                  + scale_x_date(name = "Month", date_labels = "%b %Y", date_breaks = "5 month" )
                  + labs(title = "EPSUM", x = "Time", y = "") 
                  + theme(title = red.bold.italic.text, axis.title = red.bold.italic.text) 
                  + scale_y_continuous(labels = comma)
                  + facet_wrap(~Company)
)))

  cat(paste("## Bar Chart {.tabset .tabset-pills}"), sep = "\n")

   cat(paste("### BAR1"), sep = "\n")

   print(htmltools::tagList(
          ggplotly(ggplot(summarise(group_by(PA[PA$Products == i,], Year, Company), sum(EPSUM)), aes(x=Year, y=`sum(EPSUM)`, fill=Company))
                  + geom_bar(stat="identity", position=position_dodge())
                  + labs(title = "EPSUM", x = "Time", y = "")
                  + theme(title = red.bold.italic.text, axis.title = red.bold.italic.text) 
                  + scale_y_continuous(labels = comma)
)))

   cat(paste("### BAR2"), sep = "\n")

   print(htmltools::tagList(
           ggplotly(ggplot(summarise(group_by(PA[PA$Products == i,], Year, Company), sum(EPSUM)), aes(x=as.factor(Year), y=`sum(EPSUM)`, fill=Year))
                  + geom_bar(stat="identity", position=position_dodge())
                  + labs(title = "EPSUM", x = "Time", y = "")
                  + theme(title = red.bold.italic.text, axis.title = red.bold.italic.text) 
                  + scale_y_continuous(labels = comma)
                  + facet_wrap(~Company)
)))

  cat(paste("## Pie Chart"), sep = "\n")

  print(htmltools::tagList(
          plot_ly(summarise(group_by(PA[PA$Products == i,], Company, Year), sum(EPSUM)), labels = ~Company, values = ~`sum(EPSUM)`, type = 'pie',
                  textposition = 'inside',
                  textinfo = 'label+percent',
                  insidetextfont = list(color = '#FFFFFF'),
                  hoverinfo = 'text',
                  text = ~paste(Company, 'EPSUM', `sum(EPSUM)`),
                  marker = list(colors = colors,
                                line = list(color = '#FFFFFF', width = 1)),
                                showlegend = T) %>%
        layout(title = '',
               xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
               yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)) 
  ))

  cat(paste("## Table"), sep = "\n")

  print(htmltools::tagList(
          DT::datatable(PA[PA$Products == i,][,c(-1,-12)], extensions = c('Buttons','Scroller'),
                        rownames = F, selection = list(target = 'row+column'),
                        options = list(pageLength = 400, dom = 'Bfrtip', scrollY = 200, scroller = TRUE,
                           buttons = list(c(I('colvis'), 'copy', 'print'), 
                                     list(extend = 'collection', buttons = c('csv', 'excel', 'pdf'),
                                          text = 'Download')), 
                           searchHighlight = TRUE)) %>% 
          formatCurrency(4:10, '', interval = 3, mark = ",")
))

  cat("  \n")
}
```

data introduction:
5191 obs. of 11 variables (variables are not important, just know that there are 6 variables should remember: Products, Year, Month, Company, EPSUM, Incurred)

My question is: when I knit the html document, it takes too much time, is there any way to decrease the time?
After I knit, Internet Explorer cannot properly show, but Google Chrome can. I think it is because ActiveX. However, after I open it, it still cannot properly show. Any suggestion?

Not properly show.

It should show like this.

I think this post also figures out many other users' questions about how to use for-loop in R markdowm. And there are few reference on StackOverFlow.

Very appreciate.