Gaurav Chaudhari
Gaurav Chaudhari

Reputation: 177

Reactive Function is not working

I am trying to get radio button values in server.R and after getting the value,want to perform some filter on the basis of selected value of radio button. Code is as follow:

ui.R

library(shiny)
library(shinydashboard)
library(dplyr)
library(plyr)
library(highcharter)

shinyUI(
  dashboardPage(skin = "black",
                dashboardHeader(title = img(src='BoA.png',height= 60,align = 
'middle')),
                dashboardSidebar(
                sidebarMenu(
                id ="tabs",
                menuItem("Block Trade",icon = icon("bank"),tabName = "blocktrade")
              )
            ),
            dashboardBody(
              tabItems(
                tabItem(
                  "blocktrade",tabBox(
                    id="tabset1",height = "475px",width = "1050px",
                    tabPanel("VOLUME BY CLIENT/STATUS",
                             column(width = 12,
                                    fluidRow(          
                                      box(width = 8,highchartOutput("block_trade_hcontainer1",height = "400px",width = "400px")),
                                      box(width = 4,title = "Status",radioButtons("status",label = NULL ,choices = c("Amended"="Amended","New"="New"),selected = "Amended",inline = TRUE))
                                    )
                             )
                    )

                  )
                )
              )
            )
          )
 )

server.R

library(shiny)
library(shinydashboard)
library(highcharter)
library(dplyr)
library(plyr)
library(xlsx)

block_trade<-read.xlsx('Blocktrade.xlsx',1)

shinyServer(function(input,output){
  nStatus<-reactive({input$status})
  block_trade<-block_trade[block_trade$Status == nStatus(),]
  block_trade_volume_by_client<-data.frame(table(block_trade$Associated.Client))

  output$block_trade_hcontainer1<-renderHighchart({
  highchart()%>%
  hc_chart(type="column")%>%
  hc_xAxis(categories=block_trade_volume_by_client$Var1)%>%
  hc_add_series(name="Quantity",data=block_trade_volume_by_client$Freq)%>%
  hc_exporting(enabled=TRUE)
  })
})

So,at server side i am unable to get radiobutton value to filter block trade data.

Upvotes: 0

Views: 1604

Answers (1)

SeGa
SeGa

Reputation: 9809

I adapted your reactive function a bit, as it was only listening for the input$status, which is already a "reactive" value. So I moved block_trade and block_trade_volume_by_client in the reactive. So, whenever you change the input$status, the data is filtered accordingly. And in the renderHighchart function you call the filtered data with nStatus <- block_trade_volume_by_client().

library(shiny)
library(shinydashboard)
library(dplyr)
library(plyr)
library(highcharter)
library(xlsx)


ui <- {shinyUI(
  dashboardPage(skin = "black",
                dashboardHeader(title = img(src='BoA.png',height= 60,align = 
                                              'middle')),
                dashboardSidebar(
                  sidebarMenu(
                    id ="tabs",
                    menuItem("Block Trade",icon = icon("bank"),tabName = "blocktrade")
                  )
                ),
                dashboardBody(
                  tabItems(
                    tabItem(
                      "blocktrade",tabBox(
                        id="tabset1",height = "475px",width = "1050px",
                        tabPanel("VOLUME BY CLIENT/STATUS",
                                 column(width = 12,
                                        fluidRow(          
                                          box(width = 8,highchartOutput("block_trade_hcontainer1",height = "400px",width = "400px")),
                                          box(width = 4,title = "Status",radioButtons("status",label = NULL ,choices = c("Amended"="Amended","New"="New"),selected = "Amended",inline = TRUE))
                                        )
                                 )
                        )

                      )
                    )
                  )
                )
  )
)}


block_trade<-read.xlsx('Blocktrade.xlsx',1)


server <- shinyServer(function(input,output){
  nStatus<-reactive({
    req(input$status)
    block_trade<-block_trade[block_trade$Status == input$status,]
    block_trade_volume_by_client<-data.frame(table(block_trade$Associated.Client))
    block_trade_volume_by_client
    })

  output$block_trade_hcontainer1<-renderHighchart({
    req(input$status)
    block_trade_volume_by_client <- nStatus()

    highchart()%>%
      hc_chart(type="column")%>%
      hc_xAxis(categories=block_trade_volume_by_client$Var1)%>%
      hc_add_series(name="Quantity",data=block_trade_volume_by_client$Freq)%>%
      hc_exporting(enabled=TRUE)
  })
})

Upvotes: 1

Related Questions