conditionalPanel linked to a reactive value?

3,718 views
Skip to first unread message

João Carlos

unread,
Dec 8, 2013, 6:55:56 AM12/8/13
to shiny-...@googlegroups.com
Based on Joe´s the elegant solution at https://gist.github.com/jcheng5/6604738

I´m trying to build a very simple and newbie solution for the control of a condtionalPanel based on some logic which derives from  some input.

Essencialy, in my full case I have an observe and I just want to hide a dataTable if after some processing the result is NULL. I did not get it, so I build a toy example to start again from the scratch and at the same time started to search for similar solution. N now I have the code below, and it did not work too.

Please, what am I doing wrong?


library(shiny)

shinyServer( function(input, output, session) 
{
  
    output$controlPanel <- reactive({

    if (input$test) 1  else 0

  })
  outputOptions(output, 'controlPanel', suspendWhenHidden=FALSE)
  
}
)

shinyUI(
  
  pageWithSidebar(
  
  headerPanel("Example"),
  
  sidebarPanel(
    
    checkboxInput("test", "test", FALSE)
  
  ),
        
  mainPanel(
  
    wellPanel(
              textOutput("headcrumbs")),
              
              conditionalPanel(condition = 'output.controlPanel == 1', h4(textOutput('caption'))))

  )
)

Stéphane Laurent

unread,
Dec 8, 2013, 7:51:11 AM12/8/13
to shiny-...@googlegroups.com
Here you can simply use :

conditionalPanel(condition = 'input.test == 1', h4(textOutput('caption')))) 
 
(and remove  output$controlPanel )

João Carlos

unread,
Dec 8, 2013, 7:57:50 AM12/8/13
to shiny-...@googlegroups.com
Thanks Stéphane,

but I made this example so simple that maybe I did not let it clear that in my real case input.test is used in several tasks before some tests must decide if it shows or not the conditionalPanel.

Its not meant to be a direct reaction to changes in the input.test.

Regards

Joao Carlos

Patrick Toche

unread,
Dec 8, 2013, 10:36:52 AM12/8/13
to shiny-...@googlegroups.com
1. you'll get better feedback if you create a minimal example at:  gist.github.com, you set up a ui.R and server.R and it can be run with something like this:


2. From what you write it sounds like you want to hide some table in the mainPanel? but not so sure after reading the rest ...

This has worked for me (for example):

  output$outputSomething <- renderTable({
      if (is.null(input$dataset) || !nzchar(input$dataset)){return(invisible())}
      if (ncol(Data()$df)< 2){return(invisible())}
        dta <- Data()$df
      print(dta)
    } 
    , 'include.rownames' = FALSE
    , 'include.colnames' = TRUE
    , 'sanitize.text.function' = function(x){x}
  )

3. If you are trying to hide something in the sidebarPanel, I think you need to have 

    uiOutput("outputWhatever") in your ui.R

and something like this in your server.R

  output$outputWhatever <- renderUI({
    if (is.null(input$dataset) || !nzchar(input$dataset)){return(invisible())}
        if (ncol(Data()$df)< 2){return(invisible())}
    selectInput(# Replace with what you want to have in sidebarPanel
      'inputId' = "inputName"
      , 'label' = "select:"
      , 'choices' = names(Data()$df)
      , 'selected' = Data()$df[1] # pick first column in dataset as names
    )
  })

If none of these vague hints help, upload on gist.github and describe what you want to hide explicitly based on what we can see on the screen.

best,

Patrick.

João Carlos

unread,
Dec 8, 2013, 12:49:31 PM12/8/13
to shiny-...@googlegroups.com
Thanks Patrick,

I will try to set up a god example file 

Meanwhile what I have is an observe on some input

based on this input I update several outputs 

  observe(
  {
    myValue <-  input$value

   output$text1 <- renderText({ myValue })

   output$plot1 <- renderPlot({  doSomething(myValue)   })

   output$table1 <-  renderTable ({    doSomethingElse(myValue)  })
   ..
   ....

})


but sometimes  doSomethingElse(myValue)  returns a ZERO rows/ empty dataFrame

So I´d like to put table1 inside a conditionalPanel and hide and show this panel depending on I have rows to show or not , 

something like this in the ui.R

              conditionalPanel(condition = 'output.controlPanel == 1', 
                               
                               p("conditional Panel"),
                               
                               dataTableOutput("table1")
                               
                               )

 Untill I dont get how to set the condition control variable inside an "observe"

Regards

Joao 

Stéphane Laurent

unread,
Dec 8, 2013, 1:23:16 PM12/8/13
to shiny-...@googlegroups.com
I have tried your code and it works. 
But obviously you need to define output$caption in server.R, otherwise there's nothing to render.


Le dimanche 8 décembre 2013 12:55:56 UTC+1, João Carlos a écrit :

Vincent Nijs

unread,
Dec 8, 2013, 1:45:34 PM12/8/13
to shiny-...@googlegroups.com
Why can't you do something like the following in server.R:

returnValue <- doSomethingElse(myValue)

If(nrow(returnValue) == 0) {
output$table1 <-  renderTable ({    NULL  })
} else {

output$table1 <-  renderTable ({    doSomethingElse(myValue)  })
}

Stéphane Laurent

unread,
Dec 8, 2013, 2:06:46 PM12/8/13
to shiny-...@googlegroups.com
Please send your requests to the Shiny group, not to me.
If there is an error with renderDataTable for the NULL dataframe then probably you have not updated shiny to its latest version.


I have change my logic from one observe with several outputs inside it to several outputs being rendered as the inputs change, but 
In the code below at some conditions tempData will be NULL and I get a JavaScript Error from DataTable. 
So in this case I would need to accomplish 2 things : 1) to deal with NULL or zero rows returning from the sub-setting operation and 2) better if I could to hide a conditionalPanel surrounding de dataTable in ui.R
  output$table <- renderDataTable( 
    {
      if( input$var1 =="")
      {
        tempData <- myData[myData$CODE==input$var2, ]
        tempData
      }else
      {
        tempData <- myData[myData$CODE==input$var1, ]
        tempData
      }
    
    
    }, options = list(bSortClasses = TRUE))

Patrick Toche

unread,
Dec 9, 2013, 12:45:30 AM12/9/13
to shiny-...@googlegroups.com
if it's a temporary error that lasts a few seconds while calculations are made, this may be your friend:

return(invisible())
Reply all
Reply to author
Forward
0 new messages