代码之家  ›  专栏  ›  技术社区  ›  Wlademir Ribeiro Prates

如何在不使用管道运算符的情况下更改r(highcharter包)中hchart()函数中的图表高度?

  •  0
  • Wlademir Ribeiro Prates  · 技术社区  · 6 年前

    我建立了一个闪亮的应用程序,在那里我创建了一些情节 hist() density() 对象,都保存在一个列表中,并从另一个脚本文件保存到.rds文件中。所以,在《闪亮》中,我只看了.rds,并制作了情节。

    现在一切都正常了,除了我找不到如何使用 hchart() 功能。在我的代码中,按照它的构建方式,我无法使用管道“%>%”,因为我正在使用 hchart 里面 purrr::map() 功能。

    为了更好地解释,我创建了一个小例子,如下所示。

     # Example of how the objects are structured
            list <-
              list(df1 = list(Sepal.Length = hist(iris$Sepal.Length, plot = FALSE)),
                   df2 = list(Sepal.Length = density(iris$Sepal.Length)))
    
     # Example of a plot built with hchart function
            list[['df2']]['Sepal.Length'] %>% 
            purrr::map(hchart, showInLegend = FALSE)
    
     # Example of what does not work
            list[['df2']]['Sepal.Length'] %>% 
            purrr::map(hchart, showInLegend = FALSE, height = 200)
    

    实际上,我还想更改图表的更多选项,例如颜色。但是我找不到一种方法来解决我找到的这个问题。

    事先谢谢。

    Wlademir。

    1 回复  |  直到 6 年前
        1
  •  1
  •   jbkunst    6 年前

    我可以看到两种主要的方法来满足你的需要(不知道为什么你不能使用管道):

    选项1

    创建一个函数来处理每个数据,并在该函数中添加选项:

    get_hc <- function(d) {
      hchart(d, showInLegend = FALSE) %>%
        hc_size(heigth = 200) %>%
        hc_title(text = "Purrr rocks")
    } 
    

    然后:

     list_of_charts <- list[['df2']]['Sepal.Length'] %>% 
            purrr::map(get_hc)
    

    选项2

    你可以连续使用 purrr::map :

    list_of_charts <- list[['df2']]['Sepal.Length'] %>% 
        purrr::map(hchart, showInLegend = FALSE)
    
    # change heigth
    list_of_charts <- purrr::map(list_of_charts, hc_size, height = 200)
    
    # change title
    list_of_charts <- purrr::map(list_of_charts, hc_title. text = "Purrr rocks")
    

    或者你可以连续使用 Purrr::地图 / %>% 联合体:

    list_of_charts <- list[['df2']]['Sepal.Length'] %>% 
        purrr::map(hchart, showInLegend = FALSE) %>%
        purrr::map(hc_size, height = 200) %>%
        purrr::map(hc_title, text = "Purrr rocks")