简体   繁体   中英

How to change chart height in hchart() function in R (highcharter package) without using pipe operator?

I built a Shiny app where I create some plot from hist() and density() objects, both saved in a list into an .RDS file from another script file. So, in shiny I only read the .RDS and make the plot.

Everything is working now, except that I am not finding how to change the height of the highchart plot using the hchart() function. In my code, the way it was built, I cannot work with pipes "%>%", beacuse I am using hchart inside a purrr::map() function.

To explain better I created a small example, that follows.

 # 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)

Actually, I also would like to change more options of the chart, like colors, for example. But I am not finding a way with this solution I found.

Thanks in advance.

Wlademir.

I can see 2 main ways to do what you need (not sure why you can't use the pipe):

Option 1

Create a function to process every data and add the options inside that function:

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

Then:

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

Option 2

You can use successively 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")

Or you can use successively purrr::map / %>% combo:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM