0

Running the below code as a app.r renders a ggplotly for p2 in a shiny app, but not p1, though p1 does render in the RStudio plot pane. I want to be able to get plotly plots of p1 and p2 in the app and on Shiny. What am I missing?

library(shiny)
library(plotly)
library(ggplot2)

x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)

p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()


ui <- fluidPage(
            fluidRow(
              column(width=8,
                     radioButtons("radioInput", "Radio Button Header", choices =
                                    c("name of plot 1 for user" = "plot 1",
                                      "name of plot 2 for user" = "plot 2"
                                  )   )
                    )
            ),             
            plotlyOutput("distPlot", height="700px")
)

server <- function(input, output) {

output$distPlot <- renderPlotly({

if (input$radioInput == "plot 1")  {
  p1 <- ggplotly(p1)
  print(p1)}   
if (input$radioInput == "plot 2")  {
  p2 <- ggplotly(p2)
  print(p2)}  

 })
}  

shinyApp(ui = ui, server = server)

Without plotly (removing ggplotly calls and changing plotlyOutput back to plotOutput, and renderPlotly back to renderPlot), Shiny plots both:

library(shiny)
library(plotly)
library(ggplot2)

x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)

p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()


ui <- fluidPage(
fluidRow(
column(width=8,
       radioButtons("radioInput", "Radio Button Header", choices =
                      c("name of plot 1 for user" = "plot 1",
                        "name of plot 2 for user" = "plot 2"
                      )   )
 )
),             
 plotOutput("distPlot", height="700px")
)

server <- function(input, output) {

output$distPlot <- renderPlot({

if (input$radioInput == "plot 1")  {

  print(p1) }   
if (input$radioInput == "plot 2")  {

  print(p2)}  

})
}  

shinyApp(ui = ui, server = server)

1 Answer 1

1

You're missing an else if:

library(shiny)
library(plotly)
library(ggplot2)

x <- seq(1:100)
y <- rnorm(100)
df <- data.frame(x,y)

p1 <- ggplot(df, aes(x,y)) + geom_line()
p2 <- ggplot(df, aes(x,y)) + geom_point()


ui <- fluidPage(
  fluidRow(
    column(width=8,
           radioButtons("radioInput", "Radio Button Header", choices =
                          c("name of plot 1 for user" = "plot 1",
                            "name of plot 2 for user" = "plot 2"
                          )   )
    )
  ),             
  plotlyOutput("distPlot", height="700px")
)

server <- function(input, output) {

  output$distPlot <- renderPlotly({

    if (input$radioInput == "plot 1")  {
      p1 <- ggplotly(p1)
      print(p1) }   
    else if (input$radioInput == "plot 2")  {
      p2 <- ggplotly(p2)
      print(p2)}  

  })
}  

shinyApp(ui = ui, server = server)
2
  • that worked, fantastic. but, curious why the original structure (just if and no if else) works for regular non-ggplotly plots? - just added that above.
    – dbo
    Dec 17, 2018 at 16:45
  • 1
    Tbh am not sure why - but have noticed the same behaviour myself in the past, there's something within the plotly implementation I guess.
    – arg0naut91
    Dec 17, 2018 at 17:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.