Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

renderUI not executed until a reactive function dependent on renderUI is called

Tags:

r

shiny

ggvis

In my shiny app, I have two tabs: tab 1 has a checkboxInput and a selectInput that is encoded as a renderUI in server.R and is shown only if the box is checked. In the tab 2, there is a ggvis function to plot a data frame that is made through a reactive function only when selectInput was shown in the tab 1.

Unexpectedly, the selectInput is not shown in the tab 1 unless I click on the tab 2 first and then return back to the tab 1, even though selectInput is only dependent on the checkbox that is in the same tab, i.e. tab 1.

Apparently, I do not get the idea of reactive functions right. Could you please indicate where is my mistake? Thanks!

P.S. The structure is quite complicated, but it is what I need for my "real" app.

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")

})
like image 613
bsierieb Avatar asked Oct 20 '22 03:10

bsierieb


1 Answers

Not sure what is.null(input$SELECTINPUT)==F is doing I'm guessing you want something like !is.null(input$SELECTINPUT) also maybe wrap the ggvis call in an observer and check for 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")
    }
  }) 
}
)
)
like image 153
jdharrison Avatar answered Oct 23 '22 03:10

jdharrison