Cybernetic
Cybernetic

Reputation: 13354

Delay output that depends on reactive inputs (not yet loaded) in Shiny (R)

I have the following output in my server.R file which runs a function called myFunction in the final line:

output$myOutput <- renderTable({

subFrame <- subset(dataframe, ID1 == input$variable1 & ID2 == input$variable2, select = c("name", "interest"))

myFunction(subFrame)

})

I am displaying this output as a table in my ui.R file like this:

htmlOutput("myOutput")

The selectors for choosing the IDs look like this:

output$selectUI <- renderUI({


    selectInput("variable1", "Choose ID1:", unique(df1$1), selected = 1551553)
    })

output$selectUI2 <- renderUI({

    dfsub <- subset(df1, id1 == input$variable1)


 selectInput("variable2", "Choose ID2:", dfsub$ID2, selected = 2804)
})

So..the idea is that when you make your selection from the 2 selectors above it will show the table output (myOutput) based on this reactivity.

The problem is, when I first load the page I get an ugly error where the table is supposed to be:

RS-DBI driver: (could not run statement: Unknown column 'NA' in 'where clause')

This is because my function in the output (myFunction) passes a dataframe (subFrame) to a mysql query which returns the table to be displayed in the UI (myOutput). If I click on my submit button for the 2 selectors for choosing the IDs then it works fine. But when it first loads I get that ugly error.

I am assuming it has something to do with the fact that when I first load the page the input variables from the 2 selectors have not registered yet, so the sql query that gets triggered from myFunction is passing nothing. Only when I pressed the button for the selectors does it work because now there are variables in the selectors.

Is there a way to delay the loading of the table output (myOutput) so that it waits until the IDs in the 2 selectors are there?

Upvotes: 0

Views: 1801

Answers (2)

blerg
blerg

Reputation: 65

could also use conditionalPanel()

conditionalPanel(
    condition = "!(is.null(input.variable1) || is.null(input.variable2))",
    htmlOutput("myOutput")
)

the conditional syntax could be a little off as that part should be written in javaScript syntax (hence the . instead of the $) and I'm not super familiar with javaScript (yet) but that should work.

Upvotes: 1

mlist
mlist

Reputation: 285

You could check if the input variables are already set and return NULL if not, e.g.

if(is.null(input$variable1) | is.null(input$variable2) return(NULL)

Upvotes: 0

Related Questions