conditionalPanel and actionButton

3,217 views
Skip to first unread message

Travis Hinkelman

unread,
Jan 7, 2013, 2:55:18 PM1/7/13
to shiny-...@googlegroups.com
I have shiny app that uses an actionButton to process data files and then the user interacts with the output (which is where the reactivity is really useful). The data processing step generally takes tens of minutes, so the user is provided with some indication that the app is running via messages in a conditional panel and via messages in the console window. It all works as intended for the first click of the actionButton (see relevant code below), but I'm not sure how to write the conditions so that the messages display properly for subsequent clicks of the actionButton.

Thanks,

Travis


actionButton("process_files", "Process Data Files"),
conditionalPanel(
        condition <- "input.process_files > 0 && !output.results",
        br(),
        p(strong("Processing data...please wait.")),
        p("Note: Progress messages are displayed in RStudio console.")
        ),
conditionalPanel(
        condition <- "output.results",
        br(),
        p(strong("Data processing is complete!"))
        )

Travis Hinkelman

unread,
Jan 8, 2013, 3:36:51 AM1/8/13
to shiny-...@googlegroups.com
At the time of my original post, I was thinking that I just needed to know JavaScript to get the condition statements working, but it no longer seems that simple. Here is a little more info to try to clarify where (and why) I am stuck:

(1) When the app is loaded, the progress message is hidden (condition <- "input.process_files > 0 && !output.results") and the completion message is hidden (condition <- "output.results")
(2) When the actionButton is clicked for the first time, the progress message is displayed while the completion message remains hidden
(3) When the data processing step is complete, the progress message is hidden and the completion message is displayed
(4) When the actionButton is clicked a second time, the results data frame still exists and, thus, the progress message is hidden (but should be displayed) and the completion message is displayed (but should be hidden)

I spent some time trying to use a global variable as an indicator at the beginning (e.g., processing <<- "true") and end (e.g., processing <<- "false") of the data processing function, which is located in the global.R file. But I have a poor understanding of how global variables fit into the Shiny framework and had no luck getting the global variable to update properly and no clue of how to write a conditionalPanel condition with a global variable.

Do global variables present a possible solution to this problem? Or should I pursue other approaches?

Joe Cheng

unread,
Jan 8, 2013, 12:48:35 PM1/8/13
to shiny-...@googlegroups.com
(I believe this should work but have not tested it)

You could make the condition for progress this:

$('html').hasClass('shiny-busy')

and for completion this:

input.process_files > 0 && !$('html').hasClass('shiny-busy')

This is testing for global busy-ness for Shiny, but you can use a similar test to determine whether a specific output is recalculating:

$('outputId').hasClass('recalculating')


--
 
 

Travis Hinkelman

unread,
Jan 8, 2013, 12:58:07 PM1/8/13
to shiny-...@googlegroups.com
The global test for busy-ness suits my needs and worked like a charm. Thanks, Joe!

Glen DePalma

unread,
Jan 8, 2013, 5:43:21 PM1/8/13
to shiny-...@googlegroups.com
Very helpful advice, I got it to work.  However is there any way to update the message say every 30 seconds?

This goes back to this post:

https://groups.google.com/forum/?fromgroups=#!topic/shiny-discuss/-orslt7ThBc

I'm still working on it, I'm trying something like:

  intensiveFunction.hasClass('recalculating'):after{
    ...
  },

but this doesn't work.

Thanks

Joe Cheng

unread,
Jan 8, 2013, 6:07:04 PM1/8/13
to shiny-...@googlegroups.com
The code with hasClass is for JavaScript. In CSS, an after-selector for a recalculating output would look like this:

intensiveFunction.recalculating:after{
    ...
}

To answer your other question, to make it update every 30 seconds then you would change the second argument to setTimeout to 30. That causes the update to happen approx every 30 seconds though--it doesn't guarantee that the elapsed time will be an even multiple of 30 seconds. You could just do some dividing/rounding there to get the closest 30 seconds.

Joe Cheng

unread,
Jan 8, 2013, 6:38:26 PM1/8/13
to shiny-...@googlegroups.com
Sorry, I sent this replying thinking I had already sent my other reply (the one with the long JavaScript snippet).

Jemus42

unread,
Jul 5, 2014, 3:05:53 PM7/5/14
to shiny-...@googlegroups.com
On a related note:

I can't work out how to properly base the condition of conditonalPanel on the actionButton.

My conditionalPanel looks like this:

        conditionalPanel(condition =  "input.get.show > 0",
           tabsetPanel(id = "mainPanel", selected = "tab.plot",
                       tabPanel(title = "Plot", value = "tab.plot",
                                ggvisOutput(plot_id = "ggvis")
                       )
           )
        )

Assuming that this way, the tabsetPanel will only be visible if the actionButton (set via actionButton(inputId = "get.show", label = "Do things")) is clicked.
However, this doesn't seem to work, and also other variations I have tried like "input.get.show != 0" or "input.get.show != 'null'" have failed.

I'd appreciate any hints or working examples on this.

Yihui Xie

unread,
Jul 7, 2014, 11:42:24 AM7/7/14
to Jemus42, shiny-discuss
I'm not exactly sure about the problem here, but you are strongly
recommended not to use the period "." in input id's, since it is a
special character in JavaScript. Use get_show or getShow instead. If
you have to use ".", input['get.show'] is the correct way to obtain
the value of get.show from input.

Regards,
Yihui

Jemus42

unread,
Jul 7, 2014, 2:27:33 PM7/7/14
to shiny-...@googlegroups.com, jem...@gmail.com
Thanks for the response, that solved the problem.
I guess I'll need to learn a few things about javascript after all, especially R's indexing methods and capability to have variable names contain dots seem to have spoiled me.

I also noticed how the javascript console showed a type error directly after startup, which I know understand was caused by the incorrect variable name.
Maybe it would be a good idea to mention the different naming/indexing conventions of R and javascript in the documentation of conditionalPanel, or somewhere the tutorial at least? I hope I'm not the only person working with shiny without any prior experience in web development.

Regards,
Jemus42

Yihui Xie

unread,
Jul 10, 2014, 1:08:38 AM7/10/14
to Jemus42, shiny-discuss
No, you are definitely not the only person. I'll add a note about this
in the documentation of conditionalPanel(). Thanks!

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages