MatCordTo
MatCordTo

Reputation: 257

render graphics after inputting information in Shiny

I am trying to generate some graphs after entering certain parameters, however, while some graphs are being entered they throw an error because the input has not yet been entered, so the idea occurred to me if the value was null, do not make the graph but annex code is not working:

shinyApp(ui =
           fluidPage(sidebarPanel(numericInput('x','x', value = NULL),
                                  numericInput('x2','x2', value = NULL),
                                  numericInput('x3','x3', value = NULL)), 
                     mainPanel(renderPlot('plot1'))),
         
         server = function(input, output, session){
           
         condition <- reactive(is.null(input$x) | is.null(input$x2) |is.null(input$x3))
         
         output$plot1 <- renderPlot(if(condition()){NULL}else{plot(1:10,1:10)})
         
         })

I would appreciate a way to do it, thanks.

Upvotes: 0

Views: 111

Answers (1)

HubertL
HubertL

Reputation: 19544

There is an error in your ui code that prevents to display any plot at all, you should replace :

mainPanel(renderPlot('plot1'))

with :

mainPanel(plotOutput('plot1'))

As stated in @MrFlick comment, you can use req() for required inputs:

output$plot1 <- renderPlot(
  {
    req(input$x, input$x2, input$x3)
    plot(1:10,1:10)
  })

Upvotes: 1

Related Questions