0

I have to implement a download function in shiny app. This is an example code:

library(rvg)
library(ggplot2)
library(officer)
library(highcharts)
library(jpeg)
library(ggplot2)

font.add.google("Open Sans", "myfont")
showtext.auto()

data(diamonds)

data <- subset(diamonds, color %in% c("E", "F", "G") & cut %in% c("Ideal", "Premium", "Good"))

data$indicator <- ifelse(data$color %in% c("G" ), 1, 0)

colors_hc <- c("#7CB5EC", "#313131", "#F7A35C",
               "#90EE7E", "#7798BF", "#AAEEEE",
               "#FF0066", "#EEAAEE", "#55BF3B",
               "#DF5353", "#7798BF", "#AAEEEE")


theme_hc <- function(){
    theme(
        text                = element_text(family = "myfont", size = 12),
        title               = element_text(hjust = 0), 
        axis.title.x        = element_text(hjust = .5),
        axis.title.y        = element_text(hjust = .5),
        panel.grid.major.y  = element_line(color = 'gray', size = .3),
        panel.grid.minor.y  = element_blank(),
        panel.grid.major.x  = element_blank(),
        panel.grid.minor.x  = element_blank(),
        panel.border        = element_blank(),
        panel.background    = element_blank(),
        legend.position     = "bottom",
        legend.title        = element_blank()
    )
}


plot1 <- ggplot(data) +
    geom_bar(aes(color, fill = cut), position = "dodge", width = .4) +
    ggtitle("Another interesting title") +
    xlab("Cut") + ylab("Amount") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = colors_hc) +
    theme_hc()


plot2 <- ggplot(data) +
    geom_bar(aes(color, fill = cut), position = "dodge", width = .4) +
    ggtitle("Another interesting title") +
    xlab("Cut") + ylab("Amount") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = colors_hc) +
    theme_hc()


plot3 <- ggplot(data) +
    geom_bar(aes(color, fill = cut), position = "dodge", width = .4) +
    ggtitle("Another interesting title") +
    xlab("Cut") + ylab("Amount") +
    scale_y_continuous(labels = scales::comma) +
    scale_fill_manual(values = colors_hc) +
    theme_hc()


doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- doc %>%
    ph_with_text(type = "title", str = "Marktanteil bei x am Besten in Y") %>%
    ph_with_text(type = "ftr", str = "MP Dashboard Report") %>%
    ph_with_text(type = "dt", str = format(Sys.Date())) %>%
    ph_with_text(type = "sldNum", str = "Folie1")
doc <- ph_with_vg(doc, ggobj = plot1, type = "body")

doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- doc %>%
    ph_with_text(type = "title", str = "Marktanteil bei x am Besten in Y") %>%
    ph_with_text(type = "ftr", str = "MP Dashboard Report") %>%
    ph_with_text(type = "dt", str = format(Sys.Date())) %>%
    ph_with_text(type = "sldNum", str = "Folie2")
    doc <- ph_with_vg(doc, ggobj = plot2, type = "body")

doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- doc %>%
    ph_with_text(type = "title", str = "TV in Deutschland um 10% weniger wichtig als in D") %>%
    ph_with_text(type = "ftr", str = "MP Dashboard Report") %>%
    ph_with_text(type = "dt", str = format(Sys.Date())) %>%
    ph_with_text(type = "sldNum", str = "Folie2")
    doc <- ph_with_vg(doc, ggobj = plot3, type = "body")

print(doc, target = "C:/user/my_plot.pptx")


###

For highcharts it also does not work to use something like this:

png("sample.png", 490, 350)
plot(x, y, pch=19, col=rgb(0.5, 0.5, 0.5, 0.5), cex=1.5)
abline(lm(y ~ x))
dev.off()

It seems like Highcharts uses a special kind of object which is not convertable to R inside R itself. Otherwise is works fine to download a chart via button in R-Studio...

My App uses highcharts, but it does not really make sense to rewrite every chart to to ggplot2 code. Anyone has experience with this?

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • I'm a mac user, so this may not be on point, but there is another highcharts-R interface package named "highcharter". – IRTFM Sep 03 '18 at 22:58
  • Or did you just misspell "highcharter"? – IRTFM Sep 03 '18 at 23:18
  • no highcharts-R was the suggested tag ;-) –  Sep 04 '18 at 05:48
  • So you're saying there is a package named "highcharts" (namely your library call)? I couldn't find one. Only found "highcharter". If you are using a non-CRAN package, yop should provide a link to its location and show how it was installed. – IRTFM Sep 04 '18 at 06:08

0 Answers0