Reputation: 69
I have a plot which refreshes itself everytime an action button is pressed. I would like the plot to be displayed on first render, ie. when user navigates to the page for the first time. Currently the action button must be pressed before the plot is displayed.
However, ignoreNULL is not going to do the trick here. I have a req statement which for some reason negates the effect of ignoreNULL. I have a simple reproducible example below for debugging.
library(shiny)
library(data.table)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput('inin'),
actionButton(inputId = "trigger",
label = "Trigger",
icon = icon("refresh"))
),
mainPanel(
plotOutput("outTable")
)
)
)
server <- function(input, output){
output$inin = renderUI(selectInput(inputId = 'ins', label='hi', choices=c('x','y')))
re <- eventReactive(input$trigger, {
req(input$ins) # This is absolutely required for my app
a = data.table(x = c("a", "b"), y = round(runif(2),2))
return(a)
}, ignoreNULL = F)
output$outTable <- renderPlot({
ggplot(re(), aes(x=y)) + geom_histogram()
})
}
shinyApp(ui, server)
Is there anyway to resolve this? It might be tempting to say comment out the req statement but for the actual app (not this example) I must have it otherwise the graph will run into problems with errors such as "argument is of length zero".
Thanks
Upvotes: 0
Views: 28
Reputation: 21359
Perhaps you can use a non-reactive data.table before you press the action button as shown below.
server <- function(input, output){
output$inin = renderUI(selectInput(inputId = 'ins', label='hi', choices=c('x','y')))
re1 <- data.table(x = c("a", "b"), y = round(runif(2),2))
re <- eventReactive(input$trigger, {
req(input$ins) # This is absolutely required for my app
a = data.table(x = c("a", "b"), y = round(runif(2),2))
return(a)
}, ignoreNULL = F)
output$outTable <- renderPlot({
if (input$trigger==0) df <- re1
else df <- re()
ggplot(df, aes(x=y)) + geom_histogram()
})
}
Upvotes: 1