I was trying to update a value with some ifelse conditions.
For instance, in the code below, I just want to update the reactive "multiplier" if the reactive "decim_factor" changes its value, and not if it's only updated. I tried to use the observeEvent with "decim_factor" as eventExpr, but it didn't work. Can someone help with this issue?
library(shiny)
rm(list = ls())
ui <- shinyUI(pageWithSidebar(
headerPanel("Click the button"),
sidebarPanel(
numericInput("index","Numeric Input",1),
p("This is the criteria to change the decim_factor depending on numericInput"),
br(),
p(" num_input >= 20 - decim_factor=3"),
p("20 > num_input >= 10 - decim_factor=2"),
p("10 > num_input - decim_factor=1")
),
mainPanel(
verbatimTextOutput("decim"),
verbatimTextOutput("multip")
)
))
server <- shinyServer(function(input, output) {
decim_factor <- reactive({
num_input <- input$index
if(num_input>=20) {decim_factor <- 3}
else if(10<=num_input & num_input<20) {decim_factor <- 2}
else decim_factor <- 1
as.numeric(decim_factor)
})
#I was trying to calculte the 'multiplier' only if 'decim_factor' changes its value
#and not if it is updated
#
#I tried to use observeEvent, depending on decim_factor() but it didn't work
#If I put the observeEvent function as comment it works, but the multiplier
#updates every time I change the numeric_input
observeEvent(decim_factor(),{
multiplier <- reactive({
decim_factor()*10
})
})
#The outputs come with da Sis.time() just to check if the values are updated
output$decim <- renderText({paste("Decim = ",decim_factor(), ";",Sys.time())})
output$multip <- renderText({paste("Muliplier =", multiplier(), ";",Sys.time())})
})
shinyApp(ui,server)
Aucun commentaire:
Enregistrer un commentaire