r - 反应功能不起作用
问题描述
我正在尝试获取 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)
})
})
因此,在服务器端,我无法获取单选按钮值来过滤大宗交易数据。
解决方案
我稍微调整了你的反应函数,因为它只监听输入$状态,这已经是一个“反应”值。所以我搬家block_trade
了block_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)
})
})
推荐阅读
- reactjs - React Hooks - 为什么数组解构优于对象解构?
- javascript - 如何利用 lodash defaultsDeep?
- javascript - 在链接路径中使用“#”的 React-router-dom 不会导航到组件
- android-studio - 如何识别存在图标的颜色
- c# - 在进程名称中搜索特定字母
- compiler-errors - 在编译时发出警告?
- javascript - slice() 方法的问题,数组显然是自动排序的,但它们没有
- android - WebView UI 中的引导模式位置
- python - 在 Python 中使用 + 和 += 运算符进行连接
- python - 如何使用请求模块 python3 推送键(值)?