Reputation: 2720
In the below super-simple MWE code, I'm trying to make the plot reactive to user slider input 'periods'. I've tried various iterations of observeEvent with no luck. Can someone please help me make this reactive?
The plot is supposed to show endBal
on the y-axis and periods
on the x-axis.
Also, in the below line of MWE code, res <- Reduce(f,seq(periods),init=beginBal,accumulate=TRUE)
is not picking up the value of periods from the slider input. Instead to make the MWE run for demo I have to manually define the periods
variable in the "Define vectors" section at the top of the code (periods <- 5
for example). Any idea why this part isn't working?
library(shiny)
### Define vectors ##############################################################
beginBal <- 1000
yield_vector <- c(0.30,0.30,0.30,0.30,0.30,0.28,0.26,0.20,0.18,0.20)
npr_vector <- c(0.30,0.30,0.30,0.30,0.30,0.30,0.30,0.30,0.30,0.30)
mpr_vector <- c(0.20,0.20,0.20,0.20,0.20,0.20,0.20,0.20,0.20,0.20)
default_vector <- c(0.10,0.10,0.10,0.10,0.10,0.09,0.08,0.07,0.06,0.05)
### End define vectors ##########################################################
ui <- fluidPage(
sliderInput(inputId = "periods",
label = "Periods:",
min = 1,
max = 10,
value = 5),
plotOutput(outputId = "balancePlot")
) # close fluid page
server <- function(input,output){
### Generates data for plotting ###############################################
f <- function(x,y){x*(1+npr_vector[y]-mpr_vector[y]-default_vector[y]/12)}
res <- Reduce(f,seq(periods),init=beginBal,accumulate=TRUE)
b <- head(res,-1)
endBal <- res[-1]
### End data for plotting #####################################################
output$balancePlot <- renderPlot({plot(endBal)})
} # close server
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 26
Reputation: 21297
Try this
output$balancePlot <- renderPlot({
f <- function(x,y){x*(1+npr_vector[y]-mpr_vector[y]-default_vector[y]/12)}
res <- Reduce(f,seq(input$periods),init=beginBal,accumulate=TRUE)
b <- head(res,-1)
endBal <- res[-1]
plot(endBal)
})
Upvotes: 1