1

Title says it basically. For a complex Shiny App I have to be able to send NULL values to renderPlotly(), because I want to display a plot only when certain conditions are met. The normal shiny::renderPlot() is able to do this. A small example which gives an error I do not want:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plotly"),
  plotOutput("plot")
)

server <- function(input, output) {

  # I need to be able to put NULL in here or anything so that
  # there is no output and also no error message.
  output$plotly <- renderPlotly({
    NULL
  })

  # This works and sends no error message
  output$plot <- renderPlot({
    NULL
  })

}

shinyApp(ui, server)

Note that the Web App displays only one error message, the one from renderPlotly(). I am looking for any workaround to this. How can I switch between those two plots which should be displayed at the same spot in the app, and always ignore one of them, depending on other input?

  • Have you considered this: https://shiny.rstudio.com/reference/shiny/latest/conditionalPanel.html? – Alex Sep 07 '17 at 17:34
  • Didn't see this before. Can the conditionalpanel access objects from the server function? Meaning, can I access conditions which are rendered from the server? – Stanislaus Stadlmann Sep 07 '17 at 17:38
  • 1
    I think that chould be possible. Look here: https://stackoverflow.com/questions/41710455/shiny-conditionalpanel-set-condition-as-output-from-server. – Alex Sep 07 '17 at 18:04

2 Answers2

2

You could utilize the plotly_empty() function when your data is null.

Assuming you assign your plotly (or NULL) to a variable called 'myplotly', you can use the following:

  output$plotly <- renderPlotly({
    if(is.null(myplotly)) plotly_empty() else myplotly
  })
jenwen
  • 246
  • 3
  • 5
1

Example using shiny::conditionalPanel. Plotly plot is shown only when certain condition is met.

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

ui <- fluidPage(
    selectInput("shouldShow", "show plot", c("yes", "no"), "yes"),
    conditionalPanel(
        condition = "input.shouldShow == 'yes'",
        plotlyOutput("foo")
    )
)

server <- function(input, output) {
    output$foo <- renderPlotly({
        gg <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
        ggplotly(gg)
    })
}
shinyApp(ui, server)
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Thanks for your answer. But is it possible that the conditional panel depends on a condition which is created in the server part? – Stanislaus Stadlmann Sep 07 '17 at 20:18
  • @Stan125 Yes. Can you give an example what you mean by "in server". If loaded file size was bigger than something or what? – pogibas Sep 07 '17 at 20:20