首页 > 解决方案 > 反应功能不起作用

问题描述

我正在尝试获取 server.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))
                                    )
                             )
                    )

                  )
                )
              )
            )
          )
 )

服务器.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)
  })
})

因此,在服务器端,我无法获取单选按钮值来过滤大宗交易数据。

标签: rhighchartsshinydashboard

解决方案


我稍微调整了你的反应函数,因为它只监听输入$状态,这已经是一个“反应”值。所以我搬家block_tradeblock_trade_volume_by_client。因此,无论何时更改 ,都会input$status相应地过滤数据。在 renderHighchart 函数中,您使用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)
  })
})

推荐阅读