Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change error message with Shiny app

Tags:

r

shiny

Simply, I have a Shiny app, where users can select various combinations of values in a data set and produces a graph. However for some of those combinations there is no data and Shiny produces an error message:

Error: 'from' must be length 1

How can I replace this error message with a more informative message, like:

Sorry, there is no data for you requested combination. Please change your input selections

Thanks.

like image 533
DND Avatar asked Jun 13 '16 01:06

DND


People also ask

How do you add text to the Shiny app?

You can add content to your Shiny app by placing it inside a *Panel function. For example, the apps above display a character string in each of their panels. The words “sidebar panel” appear in the sidebar panel, because we added the string to the sidebarPanel function, e.g. sidebarPanel("sidebar panel") .

What is error message sanitization?

Error sanitization applies to all errors generated within your app: both the ones you yourself produce using stop() , and the ones produced by code you rely on (although, ultimately, some code somewhere will have had to use a stop() or a similar function for an error to occur).

How do you show a message in Shiny?

Simply call shinyalert() with the desired arguments, such as a title and text, and a modal will show up. In order to be able to call shinyalert() in a Shiny app, you must first call useShinyalert() anywhere in the app's UI.

Do you need R to run Shiny app?

But the simplest way to run a Shiny app is to run it locally. You only need the shiny R package installed, and you can run the app in your browser.


1 Answers

You can do this using the validate and need functions. The code would look something like this:

output$MyPlot<-renderPlot({
  validate(
     need(MyData(), "Sorry, there is no data for you requested combination. 
                      Please change your input selections"
     )
   )
   ...code for making plot...
})

If the MyData() part does not exist due to giving an error, than the message will display, otherwise the plot will. Check ?validate for all the details.

like image 74
John Paul Avatar answered Sep 21 '22 07:09

John Paul