19

There are many questions about conditionalPanel in R shiny, but I still don't understand how I can use values created by server.R for conditionalPanel. Here is what I would like to do: I have a URL like http://some-url.com/php/session_check.php?sid=session_id. When the session_id starts with a 0, like http://some-url.com/php/session_check.php?sid=00221245 a string with a username is returned (e.g. 'testuser'). When the session_id starts with any other number but 0, like http://some-url.com/php/session_check.php?sid=10221245 a 0 is returned. Now I would like to hide a panel, depending on whether the a 0 or a username is returned. Therefore I try to do something like this:

conditionalPanel(
 condition="output.disable_ui!=0"

I know that this is the wrong, but I don't really understand how the condition argument works for outputs, as the same would work if I would do this for any input from ui.R.

Here is my sample code:

server.R

library(shiny)
library(raster)
library(rgdal)

shinyServer(function(input, output, clientData) {

  output$disable_ui<-reactive({
    query<-parseQueryString(clientData$url_search)
    url_path<-paste(sep="","http://some-url.com/php/session_check.php?sid=",query, collapse="")
    read.table(url_path)
  })

  data <- reactive({  
    inFile <- input$example_layer 

    if (is.null(inFile)) 
      return(NULL)
    raster.file<- raster(inFile$datapath) 
  })

  output$raster.plot <- renderPrint({
    "Nothing to see here"
  })
})

ui.R

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("test"),

  sidebarPanel(
    conditionalPanel(
      condition="output.disable_ui!=0",

    #File Upload
    fileInput('example_layer', 'Choose Raster Layer (ASCII)', multiple=FALSE, accept='asc')

  )),

  mainPanel(
    verbatimTextOutput("raster.plot")
  )
))
2
  • If this would work all that would happen is that a file upload button would be shown (or not) depending on the url. No file will be uploaded. The user would be asked to search for and select a file after they press the button. Is that what you want? Commented Feb 6, 2014 at 20:29
  • It is not really about the file upload button. All I want is, that the sidebar panel or even all panels are not displayed when the url gives back a '0' and all panel are displayed when the url returns some 'username'. Commented Feb 7, 2014 at 8:50

3 Answers 3

18

@Julien Navarre is right: the output must be rendered. Except if you set its option suspendWhenHidden to FALSE:

  output$disable_ui<-reactive({
    query<-parseQueryString(clientData$url_search)
    url_path<-paste(sep="","http://some-url.com/php/session_check.php?sid=",query, collapse="")
    read.table(url_path)
  })
  outputOptions(output, 'disable_ui', suspendWhenHidden=FALSE)
Sign up to request clarification or add additional context in comments.

2 Comments

can describe this in detail? What does suspendWhenHidden do? I just copied the line in my code and it didn't work. But it sounds like I really interesting solution, I would like to understand.Thanks
@user2524906 I don't know technical details but concretely if it is set to TRUE then the output is "detected" only when it is rendered. This is the cleanest solution. Other example here: stackoverflow.com/questions/19686581/…
13

I think that the output must be rendered in the UI if you want to use it after in the condition of a conditionalPanel.

With you example, the HTML for the conditional panel will look like something like this :

<div data-display-if="output.disable_ui!=0">

If no elements in your page (created as outputs in the server side) have the id "disable_ui" then the condition "output.disable_ui!=0" is always TRUE, and the conditional panel always displayed.

A simple example :

shiny::runApp(list( 
  ui = pageWithSidebar(
    
    headerPanel("test"),
    
    sidebarPanel(
      selectInput(
        "var", "Var",
        0:9)),
    
    mainPanel(
      verbatimTextOutput("id"),
      conditionalPanel(
        condition="output.id!=0",
        h4('Visible')
      )
    )
  ),
  server = function(input, output) {
    
    output$id<-reactive({input$var})
    
  }
))

If you select a number different of 0 the conditional panel will be displayed. Now, comment the line verbatimTextOutput("id"),, there is no more element with the id "id" in the page and then the condition of the conditional panel <div data-display-if="output.id!=0"> can't be FALSE.

1 Comment

Thanks @Julien Navarre, to render the output did the trick for me.
10

This is the real answer to this question: Use this inside of your server function:

outputOptions(output, "outputId", suspendWhenHidden = FALSE)

You will then be able to use the output.item in your conditionalPanel.

Answer from here: https://github.com/daattali/advanced-shiny/blob/master/server-to-ui-variable/app.R

And here: https://github.com/rstudio/shiny/issues/1318

3 Comments

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
This is no longer a link only answer.
Thanks a lot, this is exactly the issue I have. I'm glad to find this answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.