RenderUI is not executed until a react function dependent on renderUI is called

In my brilliant application, I have two tabs: tab 1 has checkboxInput and selectInput, which is encoded as renderUI on the .R server and is displayed only if the checkbox is selected. On tab 2, there is a ggvis function for constructing a data frame that is created using a reactive function only if selectInput was shown on tab 1.

Unexpectedly, selectInput does not appear on tab 1 unless I first click on tab 2 and then go back to tab 1, even if selectInput depends only on the checkbox located on the same tab, i.e. tab 1.

Apparently, I do not understand the idea of ​​reactive functions. Could you please indicate where is my mistake? Thank!

PS The structure is quite complicated, but this is what I need for my "real" application.

ui.R

library(ggvis)

shinyUI(navbarPage("",
                   tabPanel("1",
                              checkboxInput("START", label = "Start calculations", value = F),
                              htmlOutput("SELECT")
                   ),
                   tabPanel("2",
                            ggvisOutput("PLOT")
                   )
))

server.R

library(ggvis)

shinyServer(function(input, output, session) {

  output$SELECT<-renderUI({ 
    if (input$START==T) {
    selectInput("SELECTINPUT","Make your choice:",c(1,2))
    }
  })

  PLOTDF<-reactive({
    if (is.null(input$SELECTINPUT)==F) {
      plotdf<-data.frame(c(1,1,2,2),c(1,2,3,4))
      colnames(plotdf)<-c("X","Y")
      plotdf
    }
  })

  reactive({
    PLOTDF() %>% ggvis(~X,~Y) %>% layer_points
    }) %>% bind_shiny("PLOT")

})
+4
source share
1 answer

Not sure what he is doing is.null(input$SELECTINPUT)==F. I suppose you want something like !is.null(input$SELECTINPUT)that could also wrap the call ggvisin the observer and check the input

The following works for me:

library(ggvis)
library(shiny)
runApp(list(ui = navbarPage("",
                            tabPanel("1",
                                     checkboxInput("START", label = "Start calculations", value = F),
                                     htmlOutput("SELECT")
                            ),
                            tabPanel("2",
                                     ggvisOutput("PLOT")
                            )
)
, server = function(input, output, session) {

  output$SELECT<-renderUI({ 
    if (input$START==T) {
      selectInput("SELECTINPUT","Make your choice:",c(1,2))
    }
  })
   PLOTDF<-reactive({
    if (!is.null(input$SELECTINPUT)) {
      plotdf<-data.frame(c(1,1,2,2),c(1,2,3,4))
      colnames(plotdf)<-c("X","Y")
      plotdf
    }
  })
  observe({
    if (!is.null(input$SELECTINPUT)) {
    PLOTDF() %>% ggvis(~X,~Y) %>% layer_points%>% bind_shiny("PLOT")
    }
  }) 
}
)
)
+2
source

All Articles