Using reacctive function

hiii.... im new to shiny i dont know how to use reactive in this code

server <- function(input, output, session) {
output$branch <- renderPlotly({
branch<-dbGetQuery(conn,"
SELECT DISTINCT Branch AS Branch FROM inventory GROUP BY Branch " )
bran<-dbGetQuery(conn,"SELECT SUM(ABS(sales)) AS SALES FROM inventory GROUP BY Branch ")
data<- data.frame(branch$Branch,bran$SALES)
p<-plot_ly(data,x = ~branch$Branch, y = ~bran$SALES, type='scatter', color =I("green"),mode='markers',marker = list(size =60, opacity = 0.5))%>%
#add_trace(y = ~Bran$SALES, name = 'BRANCH') %>%
layout(title = "BRANCHWISE SALES" ,xaxis = list(title='BRANCH
'),yaxis = list(title = 'SALES'), barmode='bar')
})

You'll need to show code for your UI as well. Since the purpose of reactivity is for your output to react to the user's input, we would need to know what kind of information the user will supply (e.g., selecting an option from a dropdown menu) in order to know how that information will affect the visualization you're producing server-side. Based on what I'm seeing here, it looks like you'll probably filter your data based on some sort of user input, meaning that you'll just reference something along the lines of input$USERINPUTHERE in your renderPlotly() function.

Also, if you put a triple backtick (```) before and after your code (or highlight it and select the </> button on the editor), it will be easier to read, like this:

server <- function(input, output, session) {
  output$branch <- renderPlotly({
    branch<-dbGetQuery(conn,"
                       SELECT DISTINCT Branch AS Branch  FROM inventory GROUP BY Branch " )
    bran<-dbGetQuery(conn,"SELECT SUM(ABS(sales)) AS SALES FROM inventory GROUP BY Branch ")
    data<- data.frame(branch$Branch,bran$SALES)
    p<-plot_ly(data,x = ~branch$Branch, y = ~bran$SALES, type='scatter', color =I("green"),mode='markers',marker = list(size =60, opacity = 0.5))%>%
      #add_trace(y = ~Bran$SALES, name = 'BRANCH') %>%
      layout(title = "BRANCHWISE SALES" ,xaxis = list(title='BRANCH <br> '),yaxis = list(title = 'SALES'), barmode='bar')
  })

library(shiny)
library(RMySQL)
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "data",
host = "localhost",
username = "root",
password = "")
ui<-mainpanel(
plotlyoutput("branch")
)

I will break it down to you in simple cases.

Everytime you need to create a dataframe, a list or a vector which you think you will have to use a multiple times you should wrap it up with a reactive.

like the data<- data.frame(branch$Branch,bran$SALES)
could become

data<- reactive({
                 data.frame(branch$Branch,bran$SALES)
                })

in the first case it calculates it multiple times when you use the expression while in the later it is calculated once and then used until it is changed in the backend.

but in your case you are getting data from a database and I hope you would want to respond when the data in the database changes than you should use. Reactivepoll function. there are video tutorials about reactivepoll and reactive in Rstudio webinars list

https://www.rstudio.com/resources/webinars/shiny-developer-conference/

look at this for more information... good luck..

Thank You for your valuable information....

Your welcome hope you got your answer.

yes, working through it...