Rmarkdown文档中for循环内的Highcharter

时间:2019-08-18 09:11:34

标签: r r-markdown r-highcharter

我有一个数据帧stocks,其中有两列:data和hihgchart对象。 我想使用for循环将highchart对象放在Rmarkdown中。这是我的代码:

```{r message=FALSE,warning=FALSE,echo=FALSE,results="asis"}

library(tidyverse)
library(tibbletime)
library(highcharter)
library(zoo)
library(tsbox)

stocks <- structure(list(tickers = structure(c(2L, 3L, 1L), .Label = c("ERNT", 
"HT", "PODR"), class = "factor"), data = structure(list(structure(list(
    zadnja = c(159, 158.5, 159, 158.5, 159, 158.5, 158.5, 158.5, 
    158), datum = structure(c(18110, 18114, 18115, 18116, 18117, 
    18120, 18121, 18122, 18124), class = "Date"), simbol = c("HT", 
    "HT", "HT", "HT", "HT", "HT", "HT", "HT", "HT")), row.names = c(NA, 
-9L), key = structure(list(simbol = "HT", .rows = list(1:9)), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), index = structure("datum", ordered = TRUE), index2 = "datum", interval = structure(list(
    year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0, 
    minute = 0, second = 0, millisecond = 0, microsecond = 0, 
    nanosecond = 0, unit = 0), class = "interval"), class = c("tbl_ts", 
"tbl_df", "tbl", "data.frame")), structure(list(zadnja = c(415, 
413, 414, 414, 413, 413, 413, 412), datum = structure(c(18110, 
18114, 18115, 18116, 18117, 18121, 18122, 18124), class = "Date"), 
    simbol = c("PODR", "PODR", "PODR", "PODR", "PODR", "PODR", 
    "PODR", "PODR")), row.names = c(NA, -8L), key = structure(list(
    simbol = "PODR", .rows = list(1:8)), row.names = c(NA, -1L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), index = structure("datum", ordered = TRUE), index2 = "datum", interval = structure(list(
    year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0, 
    minute = 0, second = 0, millisecond = 0, microsecond = 0, 
    nanosecond = 0, unit = 0), class = "interval"), class = c("tbl_ts", 
"tbl_df", "tbl", "data.frame")), structure(list(zadnja = c(1135, 
1130, 1140, 1130, 1130, 1145, 1125, 1135, 1130), datum = structure(c(18110, 
18114, 18115, 18116, 18117, 18120, 18121, 18122, 18124), class = "Date"), 
    simbol = c("ERNT", "ERNT", "ERNT", "ERNT", "ERNT", "ERNT", 
    "ERNT", "ERNT", "ERNT")), row.names = c(NA, -9L), key = structure(list(
    simbol = "ERNT", .rows = list(1:9)), row.names = c(NA, -1L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), index = structure("datum", ordered = TRUE), index2 = "datum", interval = structure(list(
    year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0, 
    minute = 0, second = 0, millisecond = 0, microsecond = 0, 
    nanosecond = 0, unit = 0), class = "interval"), class = c("tbl_ts", 
"tbl_df", "tbl", "data.frame"))), class = "AsIs"), data_quantmode = structure(list(
    structure(c(159, 158.5, 159, 158.5, 159, 158.5, 158.5, 158.5, 
    158, 160, 159, 159, 159, 159, 159, 159, 159, 159, 159, 158.5, 
    158.5, 158.5, 158.5, 158, 158, 158.5, 158, 160.5, 160, 159, 
    159, 159, 159, 159, 159, 159, 1530920, 495279, 144392, 140798, 
    483510, 132888, 218965, 1350270, 380142, 159, 158.5, 159, 
    158.5, 159, 158.5, 158.5, 158.5, 158), .Dim = c(9L, 6L), .Dimnames = list(
        NULL, c("HT.Close", "HT.Open", "HT.Low", "HT.High", "HT.Volume", 
        "HT.Adjusted")), index = structure(c(1564704000, 1565049600, 
    1565136000, 1565222400, 1565308800, 1565568000, 1565654400, 
    1565740800, 1565913600), tzone = "UTC", tclass = "Date"), class = c("xts", 
    "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC"), 
    structure(c(415, 413, 414, 414, 413, 413, 413, 412, 415, 
    413, 414, 414, 415, 414, 413, 415, 415, 413, 414, 414, 413, 
    413, 413, 412, 415, 413, 414, 414, 415, 414, 413, 415, 1660, 
    53690, 222732, 86940, 43101, 37174, 52451, 14853, 415, 413, 
    414, 414, 413, 413, 413, 412), .Dim = c(8L, 6L), .Dimnames = list(
        NULL, c("PODR.Close", "PODR.Open", "PODR.Low", "PODR.High", 
        "PODR.Volume", "PODR.Adjusted")), index = structure(c(1564704000, 
    1565049600, 1565136000, 1565222400, 1565308800, 1565654400, 
    1565740800, 1565913600), tzone = "UTC", tclass = "Date"), class = c("xts", 
    "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC"), 
    structure(c(1135, 1130, 1140, 1130, 1130, 1145, 1125, 1135, 
    1130, 1140, 1140, 1135, 1130, 1130, 1145, 1135, 1135, 1140, 
    1135, 1130, 1135, 1130, 1130, 1145, 1125, 1135, 1130, 1145, 
    1140, 1140, 1130, 1130, 1145, 1135, 1145, 1150, 134030, 132250, 
    76180, 11300, 3390, 18320, 84570, 330285, 357450, 1135, 1130, 
    1140, 1130, 1130, 1145, 1125, 1135, 1130), .Dim = c(9L, 6L
    ), .Dimnames = list(NULL, c("ERNT.Close", "ERNT.Open", "ERNT.Low", 
    "ERNT.High", "ERNT.Volume", "ERNT.Adjusted")), index = structure(c(1564704000, 
    1565049600, 1565136000, 1565222400, 1565308800, 1565568000, 
    1565654400, 1565740800, 1565913600), tzone = "UTC", tclass = "Date"), class = c("xts", 
    "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")), class = "AsIs")), class = "data.frame", row.names = c(NA, 
-3L))

for (j in 1:nrow(stocks)) {

  cat("  \n#",  params$ticker[j], " \n")
  cat("  \n##",  "GRAFOVI CIJENA \n")

  # try 1
  stocks$hc_basic[j]
  # try 2
  print(stocks$hc_basic[j])
  # try 3
  htmltools::tagList(stocks$hc_basic[j])
  # try 4
  print(htmltools::tagList(stocks$hc_basic[j]))
  # try 5 
}

我尝试了几种不同的方法,但是没有任何效果。它可以在外部循环。

如何在Rmarkdown文档的for循环中嵌入highchart?

0 个答案:

没有答案