0

I am trying ggcorrplot in my server output in R shiny. ggcorrplot is used to create correlation matrix. I tried with renderPlotly. The output is not getting displayed. I am sure its because of this only. When I run ggcorrplot outside R shiny, I am getting the output. Please advice. Or is there an alternate way to create correlation matrix under ggplot only. My Sample data dataframe is like below

install.packages("ggcorrplot")
library(ggcorrplot)
df
Date         Var1     Var2     Var3    Var4      
1/1/2019      12       21       34      23
1/1/2019      13       22       35      24
1/1/2019      14       22       35      25
1/1/2019      15       22       35      26

corr <- round(cor(df[2:5]),1)
ggcorrplot(corr,method = "circle",lab = TRUE,hc.order = TRUE)

When I use ggcorrplot under renderploty, there is no output

Dev P
  • 449
  • 3
  • 12

1 Answers1

0

You have two options:

1) Use renderPlot and plotOutput for your ggplot object
2) wrap your ggplot object in ggploty and then use the renderPlotly and plotlyOutput calls. Your mileage will very with this option as not all aesthetics of ggplot are translated to plotly.

library(ggcorrplot)
library(shiny)
library(plotly)

ui <- bootstrapPage(
  #numericInput('n', 'Number of obs', n),
  plotOutput('plot'),
  plotlyOutput('plotly')
)

# Define the server code
server <- function(input, output) {
  output$plot <- renderPlot({
    cor <- cor(matrix(rnorm(100), ncol = 10))
    ggcorrplot(cor)
  })
  output$plotly <- renderPlotly({
    cor <- cor(matrix(rnorm(100), ncol = 10))
    ggplotly(ggcorrplot(cor))
  })
}

shinyApp(ui, server)
emilliman5
  • 5,816
  • 3
  • 27
  • 37
  • Thanks. Basically I need ggploty. But I think we cannot code like below ````ggploty(ggcorrplot(corr,method = "circle",lab = TRUE,hc.order = TRUE))```` – Dev P Oct 08 '19 at 13:24
  • You can do exactly that. – emilliman5 Oct 08 '19 at 13:39
  • Please create a minimally working example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – emilliman5 Oct 08 '19 at 13:51