Shiny debugging error with if sentence

I am creating a Shiny application and when I am debugging with the browser() function I have errors with the if-statement

For example, if I write this (complete example below):

if(1 < 2) 
 {
      h <- 5
 }

And I receive this:
debug at #3: h <- 5

What could be wrong? It looks that if-statment doesn't work inside the browser.
Thanks

library(shiny)


# Global variables can go here
n <- 200


# Define the UI
ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  plotOutput('plot')
)


# Define the server code
server <- function(input, output) {
  
  browser()
  if(1 < 2) 
  {
    h <- 5
  }
  
  output$plot <- renderPlot({
    hist(runif(input$n))
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Could you try providing a reproducible example so that there is more background to your question.

With a reproducible example, the context would be more clear and a better understanding of the problem is obtained. This helps the community to support in a better way! You may construct the example with a sample data set.

1 Like

It is happening in all the cases but I have added a reproducible example. Thanks.

You have a call for browser() right before the if. It is a debugging tool, so the server is waiting before continuing. If you comment or remove that line, you will be able to see the plot.

Thanks for your answer. I know that. This example works if i remove or comment "the browser". I used this example to see that when I am debugging I can't run the if-statement.

Oh, my bad. I misunderstood the question.

I'm not sure what it is you are expecting to see. Perhaps more information will clarify.

When I am debugging I test if the next lines work. If I run a FOR loop or other functions, they work. If I try to run a if-statement, does not work.

Running your original example above, and entering 'n' twice, I get the following.

> # Return a Shiny app object
> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:4692
Called from: server(...)
Browse[1]> n
debug at #4: if (1 < 2) {
    h <- 5
}
Browse[2]> n
debug at #6: h <- 5
Browse[2]> 

When using browser, if statements should be present and a part of the line being debugged.

When you're running your situation, it goes directly to

debug at #3: h <- 5

?

Thanks for you answer Barret. I used to copy and paste the if sentence and this still make the error but I see that with n or F10 can work properly. Enough to debug.

1 Like