conditionalPanel() in mainPanel() is shown, despite condition not being met

1,214 views
Skip to first unread message

Janna Maas

unread,
Feb 10, 2014, 10:40:37 AM2/10/14
to shiny-...@googlegroups.com

Hi all,
i posted this as an issue first on github, but i think that was the wrong place as i am not sure this is really an issue or me misunderstanding how shiny works.

i am trying to put a conditionalPanel inside the mainPanel. Whe i do, it gets displayed on refresh, after which the condition is evaluated, and the panel disappears if the condition is not met. I would expect it to not be displayed at all.
Excluding null/missing/empty variables in the condition does not help; it seems the condition simply does not get evaluated until after it's displayed.
toy example, using the example shiny application:

server.r:
library(shiny)

shinyServer(function(input, output) {
  
  output$distPlot <- renderPlot({
    dist <- rnorm(input$obs)
    hist(dist)
  })
 
})

ui.r:

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("New Application"),
  sidebarPanel(
    sliderInput("obs",
                "Number of observations:",
                min = 1,
                max = 1000,
                value = 500)
  ),
  mainPanel(
    plotOutput("distPlot"),
     conditionalPanel(condition = "input.obs==10", wellPanel(h4('you should not see me')))
  )
))

you'll see the 'you should not see me' header when you refresh the page.

In this example it's rather trivial, but i have an application that does an includeHTML() call in this way where it wrongly displays that HTMLpage for a couple of seconds.

Am i wrong in trying to use conditionalPanels this way?

anne

Patrick Toche

unread,
Feb 10, 2014, 6:44:23 PM2/10/14
to shiny-...@googlegroups.com
It's working for me... tested on Firefox. Copy-pasted the code below again.

One thing you can try, if the code below really is not working, is put your text on the server side, with uiOutput("conditionalText") in the conditionalPanel, and on the server side, in a conditionalText <- renderUI({}) you add a strict condition along the lines of 

conditionalText <- renderUI({
  if (is.null(input$obs) || is.na(input$obs) ) return()
  # what you want to show here
})

code below worked as is for me:

# server.r:

library("shiny")

shinyServer(function(input, output) {
   
  output$distPlot <- renderPlot({
    dist <- rnorm(input$obs)
    hist(dist)
  })
  
})


# ui.r:

library("shiny")

shinyUI(
  pageWithSidebar(
    headerPanel("New Application")
    ,
    sidebarPanel(
      sliderInput("obs", "N", min = 1, max = 20, value = 11)
    )
    ,
    mainPanel(
      plotOutput("distPlot")
      ,
      conditionalPanel(
        condition = "input.obs==10"
          , wellPanel(h4('I appear if N = 10'))
      )
    )
  )
)

Janna Maas

unread,
Feb 11, 2014, 4:07:20 AM2/11/14
to shiny-...@googlegroups.com
Hi Patrick,

thank you for looking at this. I've now also run this locally (windows+firefox, shiny 0.8.0), and then the conditional text appears and disappears so quickly you really have to look for it; when i run it over our server (ubuntu+firefox, shiny 0.8.0) the flicker is more obvious. And as the actual object i try to display (includeHTML call in a conditionalPanel) takes several seconds to load this is ofcourse even more obvious.
If i run the modified code below, when i refresh the page with 'no' selected i see the following:
* the panel for the textoutput, but not the text itself, flashes (very very briefly)
* the 'bla's flash, starting below the plot area (suggesting the plot area (but not the plot) are briefly displayed as well).

Also, but maybe unrelated, when i toggle from 'no' to 'yes' the textoutput briefly shows 'no' before switching to 'yes'- at least in my setup. I am curious whether this happens for you as well, considering that you didn't get a textoutput flash.

i wonder if this may be more a javascript thing than a shiny thing?

Janna

#ui.r:


library(shiny)

shinyUI(pageWithSidebar(
 
  # Application title
  headerPanel("New Application"),
 
  # Sidebar with a slider input for number of observations
  sidebarPanel(
    radioButtons("show",
                "display all the things:",
                c('yes','no'),
                selected = 'no')
  ),
 
 
  mainPanel(
    conditionalPanel(condition = "input.show=='yes'", verbatimTextOutput('show'),
                                                      plotOutput("distPlot")),
    conditionalPanel(condition = "input.show=='yes'", h4(paste0(rep('bla ',100),rep('',10000))))
                    
   
                    
  )
))
#server.r

library(shiny)


shinyServer(function(input, output) {
  
  output$distPlot <- renderPlot({
    
    dist <- rnorm(500)
    hist(dist)
   
  })
  output$show <- renderText({input$show})
 
})

Janna Maas

unread,
Feb 11, 2014, 7:53:33 AM2/11/14
to shiny-...@googlegroups.com
Ok, so i hijacked some of my colleagues' time and computers, and this issue only occurs when i am logged in, which makes it unlikely that this is a Shiny issue. Donning my tinfoil hat now, and sorry for wasting your time,

Janna


On Tuesday, February 11, 2014 12:44:23 AM UTC+1, Patrick Toche wrote:
Reply all
Reply to author
Forward
0 new messages