0

When I run this code with renderPlotly. It gives me error but without renderplotly it is working fine. Can you help me in fixing this code with renderPlotly? Thanks in advance.

 output$tot_finalized_claims1 <- renderPlotly({
    req(input$yearSelectInput)
    
    #filter df to be used in graph
    claims1 <- newly_formatted_logResults %>% filter(YEAR == input$yearSelectInput) %>% filter(PEND == "CMI") %>% select(YEAR,MONTH_NUM,PEND, TOTAL_FINALIZE,TOTAL)
    data_pcode <- summarize(group_by(claims1,MONTH_NUM), actual_auto = round(sum(as.numeric(TOTAL_FINALIZE),na.rm = TRUE)/sum(as.numeric(TOTAL),na.rm = TRUE),digits = 2))
    data_pcode <- data.frame(data_pcode)
    
    ggplot(data = data_pcode,aes(x = MONTH_NUM, y = actual_auto )) +
      geom_point() + geom_line() + # add the points and lines
       stat_QC(method = "XmR"      # specify QC charting method
               auto.label = T,      # Use Autolabels
              label.digits = 2,    # Use two digit in the label
              show.1n2.sigma = T   # Show 1 and two sigma lines
      )+
      labs(x = "Months",y = "Automation Rate",title = paste("Actual automations by CMI Pend code"))+
      geom_text(aes(label=paste(actual_auto ,"%")), position=position_dodge(width=0.95), vjust=-0.5)+
      scale_x_continuous(breaks = 1:12,labels = c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))+
      scale_y_continuous(breaks = seq(0.0, 1.0, 0.1))
      
  }) #end tot finalized plot summary
KacZdr
  • 1,267
  • 3
  • 8
  • 23
  • Can you provide `ui` and `server` code required for the question to be completely reproducible along with necessary data if required? – Ronak Shah Jul 26 '21 at 01:47
  • Hi, welcome to stack overflow. Can you please include a reproducible example, which will make it easier for others to help you, as per Ronak's suggestion. Consider looking at [this](https://stackoverflow.com/help/minimal-reproducible-example) and this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Mark Neal Jul 26 '21 at 04:49
  • Thanks a lot everyone. I got the solution by combining qic and ggplot together and it worked. – Avinash KUmar Jul 27 '21 at 17:44

1 Answers1

0

Apart from the fact that you didn't even use the plotly function to create the plot, if you want to generate plotly output you must remember two things:

  1. In server section renderPlotly instead of renderPlot
  2. In UI section plotlyOutput instead of plotOutput

You can try this code to see how it works:

library(shiny)
library(ggplot2)
library(ggthemes)
library(plotly)

ui <- fluidPage(  
  titlePanel("Plotly"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      plotlyOutput("plot2"))
    ))

server <- function(input, output) {
  
  output$plot2 <- renderPlotly({
    ggplotly(
      ggplot(data = mtcars, aes(x = disp, y = cyl)) + 
        geom_smooth(method = lm, formula = y~x) + 
        geom_point() + 
        theme_gdocs())
  })
}

shinyApp(ui, server)
KacZdr
  • 1,267
  • 3
  • 8
  • 23