get browsing state in a function

前端 未结 4 1606
灰色年华
灰色年华 2021-02-13 20:30

I have a function such as this one :

fun <- function() {
  browser()
  is_browsing()
} 

I would like to know what the code of is_browsin

4条回答
  •  无人及你
    2021-02-13 21:15

    It is described in the documentation for browser, browseText and browseCondition:

    Instead of just calling browser(), call it and set the argument for browseText or browseCondition.

    browser(text="foo")
    

    Then you can check for the condition to determine if browser is running:

    is_browsing<-function(n)
    {
        result = FALSE
     result = tryCatch({
        browserText(n=1)
         result = TRUE
    }, warning = function(w) {
        #warning-handler-code
    }, error = function(e) {
       # error-handler-code
    }, finally = {
    
        #code you always want to execute
     })
       return (result)
    }
    

    The n=1 in browseText refers to which context to retrieve the value from.

    If you are not browsing, then the call to browseText() throws an error - > This is why we wrapped it in a try catch. So if an error is thrown we know that browser is not running. If no error is thrown, result is set to true, and you can run your own custom logic.

    To test, try:

    browser(text="foo")
    if(isTRUE(is_browsing())){
        print("is browsing!!!")
    }else{
        print("is not browsing!!!");
    }
    

    Then comment out the call to browser(text="foo"), and see the difference.

    EDIT: If you cannot pass an argument to browser() for any reason, you can use debug instead:

    https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/debug

    Or you can set the value using some other external debugger.

提交回复
热议问题