0

I have been having trouble with visualizing a ggplot. When I run the following code, all I get is a blank screen, but no error messages:

library(ggplot2)
library(shiny)

if (interactive()) {
  ui <-{plotOutput(outputId = "main_plot", height = "300px")}

  server <- function(input, output){
    plotInput <- reactive({ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()})

    output$main_plot <- renderPlot(expr=function(){
      #conditional statements
      plotInput()}
    )

  }
  shinyApp(ui, server)
}

If you run the exact same code without the function definition, however, the plot shows up. I am very perplexed because the documentation of renderPlot permits the use of expressions:

if (interactive()) {
  ui <-{plotOutput(outputId = "main_plot", height = "300px")}

  server <- function(input, output){
    plotInput <- reactive({ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()})

    output$main_plot <- renderPlot(plotInput())

  }
  shinyApp(ui, server)
}

This is a bit of a problem, since I would like to run some conditional statements that determine whether or not to show the plot. Am I using the right approach?

0

1 Answer 1

1

The expr= of rennderPlot() should be an expression. You are passing a function. Those aren't the same. Try taking off the function() part.

library(ggplot2) library(shiny)

if (interactive()) {
  ui <- plotOutput(outputId = "main_plot", height = "300px")

  server <- function(input, output){
    plotInput <- reactive({ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()})

    output$main_plot <- renderPlot(expr={
      #conditional statements
      plotInput()}
    )

  }
  shinyApp(ui, server)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! Thanks. My code is working now. I got a little confused with the lingo- beginner programmer. The deprecated version (reactivePlot) used to take functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.