communication between server and ui (cf. updateActionButton)

38 views
Skip to first unread message

Norman Packard

unread,
Jun 26, 2015, 7:37:02 PM6/26/15
to shiny-...@googlegroups.com

This is actually a followup on a nice suggestion of Joe Cheng on how to communicate between server and ui to implement a "reset" function that switches between conditional panels based on stuff that happens on the server side.  A minimal example of Joe's suggestion is given below; the key point is using makeReactiveBinding() to create a variable (server side) that can be referenced by the UI in the condition for a conditionalPanel.  Seems to work quite well, except for two things:

1.  On the server side, I have two calls to updateRadioButtons(), one for the x-axis choice, and one for the y-axis choice.  Only the first call seems to work; the radioy buttons are not updated.  What is the reason?

2.  The first time I press the "Plot this" button, I get a blank screen in the main panel until the system("sleep 2") completes and plot() is called.  But after I reset X-axis and Y-axis, then hit "Plot this" again, I see the old plot until system("sleep 2") completes, and then the new plot appears.   Is there any way to reset the conditional panel in some way so that the canvas is cleared so I don't have to look at the old plot while I'm waiting?  Seems like a somewhat silly request, but if I'm going to have hundreds or (hopefully) thousands of clients looking at the pages, I want to avoid any source of confusion, and so I would like the blank screen, if possible, after the reset.


library(shiny)

mychoices = list()
for(i in 1:5) mychoices[[i]]=i
names(mychoices)=1:5

app = shinyApp(
    ui = pageWithSidebar(
        headerPanel(h3('State test'),"State test"),
        sidebarPanel(
            conditionalPanel("output.mystate == 0",
                             actionButton('doplot',"Do the plot")),
                         conditionalPanel("output.mystate == 1",
                                          actionButton('reset',"Reset X & Y"))
                         ),
                     mainPanel(
                         conditionalPanel("output.mystate == 0",
                                          radioButtons("radiox", label = h3("X-axis"),
                                                       choices = mychoices,
                                                       selected = 0),
                                          radioButtons("radioy", label = h3("Y-axis"),
                                                       choices = mychoices,
                                                       selected = 0)),
                         conditionalPanel("output.mystate == 1",
                                          plotOutput("xyplot"))
                         )
        ),
    server =function(input, output, session) {
        mystate = 0                # for reset functionality
        makeReactiveBinding("mystate")
        output$mystate <- reactive(mystate)
        outputOptions(output, "mystate", suspendWhenHidden = FALSE)
      
        dat = matrix(rnorm(500),ncol=5)
        observeEvent(input$doplot,{
            mystate = 1
            output$mystate <- reactive(mystate)
            outputOptions(output, "mystate", suspendWhenHidden = FALSE)
        })
        observeEvent(input$reset,{
            mystate = 0
            output$mystate <- reactive(mystate)
            outputOptions(output, "mystate", suspendWhenHidden = FALSE)
        })
        output$xyplot = renderPlot({
            if(input$doplot==0)
                return(NULL)
            isolate({
                xidx = as.numeric(input$radiox)
                yidx = as.numeric(input$radioy)
                updateRadioButtons(session,"radiox", label = h3("X-axis"),choices = mychoices,selected = 0)
                updateRadioButtons(session,"radioy", label = h3("Y-axis"),choices = mychoices,selected = 0)
                system("sleep 2")       # simulate heavy computation...
                plot(dat[,xidx],dat[,yidx],
                     xlab = paste('X var = ',xidx),ylab = paste('Y var = ',yidx))
            })
        })
    }
)

runApp(app)



Reply all
Reply to author
Forward
0 new messages