17

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")
  )
))
llrs
  • 3,308
  • 35
  • 68
viktor_r
  • 701
  • 1
  • 10
  • 21
  • 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? – Vincent Feb 06 '14 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'. – viktor_r Feb 07 '14 at 08:50

3 Answers3

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)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 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 – viktor_r Feb 07 '14 at 09:08
  • 2
    @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: http://stackoverflow.com/questions/19686581/make-conditionalpanel-depend-on-files-uploaded-with-fileinput – Stéphane Laurent Feb 07 '14 at 13:56
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.

micstr
  • 5,080
  • 8
  • 48
  • 76
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
8

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

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
clancy
  • 182
  • 1
  • 3
  • 10
  • 2
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) 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.](//stackoverflow.com/help/deleted-answers) – Filnor Aug 07 '18 at 06:39
  • This is no longer a link only answer. – clancy Aug 08 '18 at 07:06