wbaeckelmans
wbaeckelmans

Reputation: 397

Shiny conditionalPanel - condition doesn't work (output condition)

I'm trying to use a conditionalPanel in Shiny based on an output generated in my server.R code. I can't use an input because this does not suit my needs. I have based my code on the example I have found on the following website: http://rstudio.github.io/shiny/tutorial/#dynamic-ui

UI.R code:

tabSubMenuSaveData <-
  tabItem("subMenuSaveData",      
          conditionalPanel(
            condition = "output.test",  
              uiOutput(outputId = "ui_save_data")
          )
  )

Server.R code:

dataset <- reactive({
  datasets()$datasetlist[[input$datasetSelector]]
})

output$test <- reactive({
  print("Test")
  nrow(dataset())
})

-> dataset() provides the currenly selected dataset in my Shiny app.

It seems that my application doesn't even get to the output$test part because it doesn't show my print. It should go here whenever I press on the Save data tab.

If someone could provide me with the solution or explain me why my application doesn't get to the output$test part, I would be very grateful.

Upvotes: 1

Views: 2261

Answers (3)

wbaeckelmans
wbaeckelmans

Reputation: 397

Didn't manage to find a working solution based on the answers given earlier. A friend of mine did manage to help me find a working solution. Answer below:

UI.R code:

tabSubMenuSaveData <-
  tabItem("subMenuSaveData", 
          conditionalPanel(
            condition = ("output.file_Uploaded > 0"),
            uiOutput(outputId = "ui_save_data")
          )
  ) 

Server.R code:

output$file_Uploaded <- reactive({
  return(!is.null(getData())) 
})

So basically I've used another reactive function defined earlier in my code. Nonetheless, thanks for your help!

Upvotes: 2

DeanAttali
DeanAttali

Reputation: 26313

I think those docs are very outdated... I've never seen output being used that way, and I just tried the code from that page and it doesn't seem to work.

As an alternative solution, you could use shinyjs to show/hide the panel instead of using conditionalPanel. For example (this is untested code):

# install.packages("shinyjs")
library(shinyjs)

# UI:

...
tabSubMenuSaveData <-
  tabItem("subMenuSaveData",   
          div(id = "mydiv",    
            uiOutput(outputId = "ui_save_data")
          )
  )
...


# server:

...
dataset <- reactive({
  datasets()$datasetlist[[input$datasetSelector]]
})

observe({
  toggle(id = "mydiv", condition = nrow(dataset()) > 0)
})
...

Upvotes: 0

user5029763
user5029763

Reputation: 1933

output$test must be an output and it must be called at the ui-side. I have an app that deals with the same problem and I used something like this:

#ui.R

...
htmlOutput("check_cond"),
conditionalPanel( 
    condition = "!output.check_cond",
    ...
)

#server.R

output$check_cond <- renderUI({ 
     if(nrow(dataset()) > 0) return NULL
     div('Choose a proper dataset!', align = 'center', style = 'color: red;')
})

Upvotes: 0

Related Questions