showing text on shiny app

I have a plotly function that shows my plots, I would like to show a specific text to the user if he/she selects a wrong input.
please see the code:

</>
output$fancyPlot <- renderPlotly({
if (length(input$celltype1) == 1 && length(input$celltype2) == 1 && input$TF_query1 != "ALL"){
f1 = da_cell_plot1()
plot1 = ggplot(f1, aes(x = input$celltype1, y = V1)) +
geom_jitter(alpha = 0.1, color = "tomato")+
geom_boxplot(alpha = 0)
f2 = da_cell_plot2()
plot2 = ggplot(f2, aes(x = input$celltype2, y = V1)) +
geom_jitter(alpha = 0.1, color = "tomato") +
geom_boxplot(alpha = 0)
Final_fig_box <- subplot(plot1, plot2)%>%
layout(title = "Inferred Celltype Activity")
print("it works")
return(Final_fig_box)
}else{
showNotification("You should select first and second celltype and a TF as well.")
}
})
</>

In the els section I used "showNotification", But I got an error, Also I wrote a function that had this message and then I called it in the els section but I got an error as well.
How can I handle it?
Thanks in advance

showNotification causes a side effect, and is not itself something that renderPlotly knows how to render, therefore you can hack-solve that by returning NULL

}else{
showNotification("You should select first and second celltype and a TF as well.")
return(NULL)
}

aside from that you might choose to use shiny validate/need which is the more common approach to letting the user know you wont render something for a reason, not least because it ties the message to the undrawn item

output$fancyPlot <- renderPlotly({
  shiny::validate(shiny::need(
    expr = (length(input$celltype1) == 1 && length(input$celltype2) == 1 && input$TF_query1 != "ALL"),
    message = "You should select first and second celltype and a TF as well."
  ))
    f1 = da_cell_plot1()
    plot1 = ggplot(f1, aes(x = input$celltype1, y = V1)) +
      geom_jitter(alpha = 0.1, color = "tomato")+
      geom_boxplot(alpha = 0)
    f2 = da_cell_plot2()
    plot2 = ggplot(f2, aes(x = input$celltype2, y = V1)) +
      geom_jitter(alpha = 0.1, color = "tomato") +
      geom_boxplot(alpha = 0)
    Final_fig_box <- subplot(plot1, plot2)%>%
      layout(title = "Inferred Celltype Activity")
    print("it works")
    return(Final_fig_box)
})
1 Like

Thanks, @nirgrahamuk
yes, I saw shiny validate/ need as well, but I don't know how to use it.
Is it possible to use it if there is an error for the developers?
for example, there is a dimensional error in a reactive function, how can I use it there?
I mean instead of showing a red error on the shiny window, show desired message.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.