我在闪亮的应用程序的服务器功能中具有以下代码。
要绘制每种类型的图表,我需要每次调用getPlotData()函数。有什么办法可以避免它使应用程序更快?
功能
#This gets Sales data for selected client data
getPlotData <- reactive({
inpParam <<- input$noc
getSalesData(inpParam)
End_date <<- input$endDate
SalesClientData <<- GetSalesClientData()
WeeklySales <<- GetWeeklyData()
FortNightData <<- GetFortNightlyData()
EWS <<- GetEWSData()
MONTHLYSALES <<- GetMonthlyData()
wtSafetyRatio <<- round(as.numeric(crossprod(MONTHLYSALES$SafetyRatio,MONTHLYSALES$Weights)/sum(MONTHLYSALES$Weights)),2)
wtHealthScore <<- round(as.numeric(crossprod(MONTHLYSALES$HealthScore,MONTHLYSALES$Weights)/sum(MONTHLYSALES$Weights)),2)
wtPaymentScore <<- round(as.numeric(crossprod(MONTHLYSALES$PaymentScore,MONTHLYSALES$Weights)/sum(MONTHLYSALES$Weights)),2)
ROLLDATA <<- GetRollingData()
EARLY_TOPUP <<- GetEarlyTopUp()
})
#DPD Plots
output$DPDPLot<-renderPlotly({
getPlotData()
plot_ly(data=SalesClientData, x = ~Date, y = ~DPD, name='DPD', type='scatter', mode = 'lines') %>%
add_trace(y = ~CHECKNACHDPD, name = 'CHEQUE-NACH Payment', type='bar') %>%
add_trace(y = ~CHKNACHBounceDPD, name = 'CHEQUE-NACH Bounce', type='bar') %>%
layout(title= "DPD Pattern", legend = list(orientation = "h"))
})
#Sales Plot
output$SalesPlot<-renderPlotly({
getPlotData()
SalesDayWise = group_by(SalesClientData,Day)
SalesDayWise = summarize(SalesDayWise,TotalSales = sum(TotalPayRecvdAdj, na.rm=TRUE))
plot_ly(data=SalesDayWise, x = ~Day, y = ~TotalSales, name='Total Sales', type='bar') %>%
layout(title="Daywise Sales", legend = list(orientation = "h"))
})
答案 0 :(得分:1)
我认为您有一个错误的印象,就是仅仅因为您在再次调用该函数,而该函数在每次调用时都在进行计算。但实际上,当反应式中的输入小部件发生更改时,它仅计算一次。
您可以通过在函数GetSalesClientData()
或该反应式表达式中的任何其他函数中放置一个打印语句来对此进行测试。
即使您调用反应表达式100次,仅当反应表达式中包含的输入窗口小部件发生更改时,它仍会计算并获得新结果。
在这种情况下,它是input$noc
和input$endDate