debugTools.R for shiny, new improved

357 views
Skip to first unread message

Roger Day

unread,
Jan 2, 2015, 5:46:28 PM1/2/15
to shiny-...@googlegroups.com
This has been enormously helpful to me.  Handy utilities first:
assign("%&%",  function (a, b) paste(a, b, sep = "")
catn = function(...) cat(..., "\n")
wasClicked =  function(button) {
    if(exists("input"))  (!is.null(button) ) {    if(button > 0) {        
          return(TRUE)        }      }
    return(FALSE)
  }
#####

Save the attached file as debugTools.R. 
In server.R, 
source("debugTools.R", local=TRUE)
and using in ui.R via uiOutput("debugTools").
In ui.R, include in your list of UI interface objects
uiOutput("debugTools")

It's handy to keep the debugTools panel visible at the top,
while you scroll your window. 
Not as straightforward as it should be.  See my post
which solves the problem. (Works in FF, Safari, Chrome, not tested in IE.)


debugTools.R

Vincent

unread,
Jan 4, 2015, 12:33:47 PM1/4/15
to shiny-...@googlegroups.com
Looks interesting Roger. Thanks for sharing. I'm not seeing any output on screen (using attached version). Could you give some examples of how you use this?
debugTools.R

Roger Day

unread,
Jan 4, 2015, 7:05:21 PM1/4/15
to shiny-...@googlegroups.com

Thanks for your interest.

Did you include uiOutput("debugTools”) somewhere in the return value for shinyUI()? If so, it should appear on the page.
It appears wherever you put it—  the top is nice— initially as just a single check box, cleared so that the rest of the stuff
is not visible. It looks like this:

 Click the check box and the whole debug panel should appear:

If the code you want to evaluate is multi-line, watch out. Pasting code into one of the boxes will not paste the end-of-line characters properly.

You might have to insert the end-of-lines by hand.  The two textareas are resizable; you can see the handles at the lower right.

Roger Day

unread,
Jan 4, 2015, 10:19:41 PM1/4/15
to shiny-...@googlegroups.com
You do need to click the button to the left of the box to cause the evaluation.

The JS result is a popup.  If there is an error in the code, 
the popup doesn't appear; no error message appears either.   
In that case, crank up the javascript console
for your browser; the error message will be there.

For the R code box, the result appears right in your page.
If there's an error, you see the error (wonderfully, in red; I don't know why).
The R evaluation uses capture.output(), so you can see lists, instances, etcetera,
though the formatting might not always be pleasant. 
If the output is a shiny expression you may see 
the result in the form of html components instead of text.




On Friday, January 2, 2015 5:46:28 PM UTC-5, Roger Day wrote:

Laurent Franckx

unread,
Mar 28, 2015, 3:53:52 PM3/28/15
to shiny-...@googlegroups.com
Dear Roger

Do I understand correctly that the tool you propose allows to debug function called from within server.R?

If yes, a few practical question: (a) is it possible that the closing parenthesis is missing in the line starting with assign("%&%".... (b) in "if(exists("input"))  (!is.null(button) )" shouldn't there be an & or an | ?
(c) do you put the "handy utilities" in server.R before you call
source("debugTools.R", local=TRUE)?

Best regards
Laurent


Roger Day

unread,
Mar 28, 2015, 5:53:40 PM3/28/15
to shiny-...@googlegroups.com
Hi Laurent,
Thanks for your interest.

Yes, you can do debug(myFunction) in the Rcode box,
and it works! (I just tested it.)

I just added debugTools to a new project,
and found that you also have to add these lines in server.R
before the source(“debugTools.R”, local=T) call.
rValues = reactiveValues() 
thisSession <<- session

Then it’s good to go.
Best regards,
Roger Day

…and here are the answers to your other questions.

(a)  Ah, yes, correct; add a final “)”.
Fine in the package this comes from ,
but wrong in my e-mail!!
Bad copy/paste job.

(b) Here’s the original wasClicked:

  wasClicked =  function(button) {
    if(exists("input"))  
      if(!is.null(button) ) {
        if(button > 0) {        
          return(TRUE)
        }
      }
    return(FALSE)
  }

It looks like an “if” was missing  in what I sent.
Damned if I know how that happened.
Note the nesting.

It’s just a convenience function to detect clicking
making sure the button has been created etc.
so as not to generate an error initially.

(c) You can put the convenience code anywhere that R sees it before
source’ing debugTools.
In the other package, it was in the R directory in a utilities file.
In the new package, I just add to the top of server.R

Laurent Franckx

unread,
Mar 28, 2015, 6:27:09 PM3/28/15
to shiny-...@googlegroups.com
Hi Roger

Thanks for the quick reply, but I do not think everything has been solved yet.


The exact order in which I have written everything in server.R is:



assign("%&%",  function (a, b) paste(a, b, sep = ""))

catn = function(...) cat(..., "\n")
wasClicked =  function(button) {
  if(exists("input")) 
    if(!is.null(button) ) {
      if(button > 0) {       
        return(TRUE)
      }
    }
  return(FALSE)
}

rValues = reactiveValues()
thisSession <<- session

source("debugTools.R", local=TRUE)

This yields the following error message when running the app:



> shiny::runApp()

Listening on http://127.0.0.1:3789
Error in eval(expr, envir, enclos) : object 'session' not found


Message has been deleted

Roger Day

unread,
Mar 29, 2015, 10:50:44 AM3/29/15
to shiny-...@googlegroups.com
When you call shinyServer,
be sure to add the session argument.

        shinyServer(function(input, output, session) {
instead of just 
        shinyServer(function(input, output) {

Laurent Franckx

unread,
Mar 29, 2015, 11:40:26 AM3/29/15
to shiny-...@googlegroups.com
Actually, I did. But I thought that the code you described had to be included before the call to shinyServer?
Reply all
Reply to author
Forward
0 new messages