ifelse inside observe in shiny - not waiting for the trigger

Hi all.

At some part of my code I have the following observe object. It works fine until I add an if statment inside it. The statment executes right away when the script runs and does not wait for the action associated to observe to be triggered. What do I have to do in order to "hold the horses" of this if inside observe?

observe({
    print("UPDATING COORDS")
    print(input$map_marker_dragend)
    id <- input$map_marker_dragend$id
    lat_nova <- input$map_marker_dragend$lat
    lon_nova <- input$map_marker_dragend$lng
    print(lat_nova)
    print(lon_nova)
    print(WP$df$latitude)
    df2 <- ifelse(is.null(df2), WP$df, df2) # this is the line causing problem
    df2 <- if (is.null(df2)) return(WP$df) else return(df2) # this is a second try to make it work
    df2$latitude[which(WP$df$id == id)] <- lat_nova
    df2$longitude[which(WP$df$id == id)] <- lon_nova
    print(df2$latitude)
    print(df2, width = Inf)
    
    # declarando pra usar adiante
    WP$df2 = df2
  })

Basically what I want is that shiny checks if df2 exists... if not than df2 should be WP$df... if yes than df2 = df2 and proceed with calculations.

Thanks!

You should be using if here, not ifelse(). ifelse() is designed to work with vectors and evaluated both the true and false arguments.

I think the problem is that you're using return() which is going to exit the observer. Just do:

df2 <- if (is.null(df2)) WP$df else df2