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)
dbo
  • 1,174
  • 1
  • 11
  • 19

1 Answers1

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)
arg0naut91
  • 14,574
  • 2
  • 17
  • 38
  • 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 '18 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 '18 at 17:14