Possible Shiny Bug

I am trying to achieve the following results with this code. Basically I want to store an output from res() when i click the "set as base" button to store for later.

The code works perfectly when you uncomment the blocks of code that are currently commented out. But gives me this error otherwise: evaluation nested too deeply: infinite recursion / options(expressions=)?

Why does the "resultmult" need to be output in the UI for the code to work work? Seems like a bug.

Input = 5. Run. Output = 5. Setbase. Change input to 10. Click rebase. Click Run. Output Should be 50.
Then if I change input = 4. Leave rebase ticked. Click Run. Output Should be 4base = 45 =20.
Then should be able to set the output (20) as base by clicking set base. Then change input to lets say 3. Have rebase ticked. Click run. Output should be 3*20 = 60

tempserver<-shinyServer(function(input, output, session){
  
  res<-eventReactive(input$runit,{
    if(input$rebase){
      mult<-multiplyer()
      result <- input$numin * mult}
    else {result<-input$numin}
    result
  })
  
  
  multiplyer <- 
    
    eventReactive(input$setbase, {
      
      res()
      
    })
  
  output$resultoutput<- renderText({
    res()
    
  })
  
  
  # output$resultmult<- renderText({
  #   multiplyer()
  #   
  # })
  
})



tempui<-shinyUI(fluidPage(
  
  fluidRow(
    column(4,numericInput('numin', 'Input Number', 2)),
    column(4, actionButton("runit","Run")),
    column(4, actionButton("setbase","Set As Base")),
    column(4, checkboxInput("rebase","ReBase")),
    column(4, textOutput("resultoutput"))
    # ,column(4, textOutput("resultmult"))
    
  )
  
))

shinyApp(server=tempserver,ui=tempui)

its a basic principle of reactive programming to be late and lazy. no need to calculate things that people dont want. if they aren't looking at a plot, no need to calculate the content of the plot. etc.

Well let me re-phrase that, "resultmult" is being used. But i don't want to output it anywhere. Why won't shiny let me do that without the error mentioned?

I don't really understand your program logic.
I'll do a proof of concept of a related program and maybe we can adapt that for you.

tempserver<-shinyServer(function(input, output, session){
  
  res<-reactive({
    req(input$numin) * req(multiplyer())
  })
  
  
  multiplyer <- 
    eventReactive(input$setbase, {
     new_val<-input$newbasenum
      updateNumericInput(session=session,
                         inputId="newbasenum",
                         value = "")
      new_val
    })
  
  output$resultoutput<- renderText({
    res()
    
  })
  
  # output$resultmult<- renderText({
  #   multiplyer()
  # 
  # })
  
})



tempui<-shinyUI(fluidPage(
  
  fluidRow(
    column(4,numericInput('numin', 'Input Number', 2)),
    column(4, actionButton("runit","Run")),
    column(4,numericInput('newbasenum', 'Input Base', 2)),
    column(4, actionButton("setbase","Set As Base")),
    column(4, textOutput("resultoutput"))
    # ,column(4, textOutput("resultmult"))
    
  )
  
))

shinyApp(server=tempserver,ui=tempui)

Thanks, but I can't have base as an input.

It's for a more complex problem but basically the logic is as follows:

I can only have one input, and i want one output.

output = input * multiplier.

multiplier = 0 if "set base" is not clicked.
mutliplier = the output at the moment "set base" is clicked. (And multiplier doesn't change value until you click "base again").

So again, the outcomes will be:
Input = 5. Run. Output = 5. Setbase. Change input to 10. Click rebase. Click Run. Output Should be 50.
Then if I change input = 4. Leave rebase ticked. Click Run. Output Should be 4 base = 4 5 =20.
Then should be able to set the output (20) as base by clicking set base. Then change input to lets say 3. Have rebase ticked. Click run. Output should be 3*20 = 60

closer ?

tempserver<-shinyServer(function(input, output, session){
  
  res <- reactiveVal(0)

  
  observeEvent(input$runit,{
    res(req(input$numin) * req(multiplyer()))
  })
  
  
  multiplyer <- 
    eventReactive(input$setbase, {
input$numin

    })
  
  output$resultoutput<- renderText({
    res()
    
  })
  
  # output$resultmult<- renderText({
  #   multiplyer()
  # 
  # })
  
})



tempui<-shinyUI(fluidPage(
  
  fluidRow(
    column(4,numericInput('numin', 'Input Number', 2)),
    column(4, actionButton("runit","Run")),
    column(4, actionButton("setbase","Set As Base")),
    column(4, textOutput("resultoutput"))
    # ,column(4, textOutput("resultmult"))
    
  )
  
))

shinyApp(server=tempserver,ui=tempui)

Unfortunately not. It fails this:
I want to set the output as the multiplier by clicking set as base.
Right now your code is setting the input as the multiplier when i click set as base.

In principle we might be able to do that, but not if the default base is zero.
because no matter what numeric its multiplied by the result will be zero, so the base will never be able to be updated from zero.

library(shiny)

tempserver <- shinyServer(function(input, output, session) {
  res <- reactiveVal(0)
  multiplyer <- reactiveVal(1)


  observeEvent(input$runit, {
    res(req(input$numin) * req(multiplyer()))
  })

  observeEvent(input$setbase, {
    multiplyer(res())
  })

  output$resultoutput <- renderText({
    res()
  })

  # you can hide this
  output$resultmult <- renderText({
    multiplyer()
  })
})


tempui <- shinyUI(fluidPage(
  fluidRow(
    column(4, numericInput("numin", "Input Number", 2)),
    column(4, actionButton("runit", "Run")),
    column(4, actionButton("setbase", "Set As Base")),
    column(4, textOutput("resultoutput")),
    column(4, textOutput("resultmult"))
  )
))

shinyApp(server = tempserver, ui = tempui)
1 Like

Wow thanks nirgrahamuk, that works.

I guess I still do not understand what the problem with my initial code is, vs the working code you just provided.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.