I have a DT table and a Plotly function in which there are some conditions and based on each condition a specific figure will be plotted (based on the table's data). In one of those conditions, I get the selected rows of a table(users select some desired rows). I would like to add an action button there that users can press it then see the plots of selected rows.
This is a part of my code (It's an example and It isn't complete), It works but it shows an error middle of the output page.
I need to mention here I wrote the code for the action button in the GG function.
The error is:
no applicable method for 'plotly_build' applied to an object of class "c('Observer.event', 'Observer', 'R6')"
# a part of UI section
actionButton("add_graph", "Plots of selected rows")
),
mainPanel(
actionButton("download1", "Download table"),
DT::dataTableOutput("fancyTable"),
tags$hr(),
plotlyOutput("fancyPlot"
,width = "auto"
, height = "auto"
),
#server section
GG = observeEvent(input$add_graph,{
output$fancyPlot <- renderPlotly({
#I got E which is my data set, I removed those lines
plot1 = ggplot(E, aes(x = b , y = E, text = paste("Cell type:", b,
"\n", "shap value:", E))) +
geom_jitter(alpha = 0.3, color = "tomato")+
geom_boxplot(alpha = 0)
plot1 <- plot1%>%
ggplotly(tooltip="text")
return(plot1)
})
})
#main plot
output$fancyPlot <- renderPlotly({
if(a){
#a specific action and plot, I removed it here
}
else (b){
#a specific action and plot, I removed it here
}
else{
GG #I think error is related to this part
}
})
now what is GG ? GG has been defined by you as an observeEvent... this is really not something to be done in shiny and should be avoided. In this particular case the bottom renderPlotly is going to try to draw the observeEvent, and it complains it doesnt know how to do this. In general it is not meaningful to assign an observe* anything to a name in shiny. the observe stands alone as a function that will be called by reactive framework when its input(s) trigger. It is run for side effects and not to return data.
if it was intended to return data it would be a form of reactive() and then it would be meaningful to have a name (so other code can call it so see the data in it )
Thank you for the information, But as I understood this error is not related to assigning the Event to a name.
Because I used the Event in the middle of the Plotly code, without the "GG" variable, Like this:
Well, I assumed it was related because perhaps it gave you the illusion that it might be meaningful that something with a name, like GG could be something to be plotted. which an observeEvent like you have is not. because its a formula for shiny reactivity to cause sideeffects, in this case to redefine the fancy plot output when the button is pressed, i.e. not itself a plot.
Simply put trying to plot anything unplottable is an error.
Here is an example of that based on your further minimisation.
library(shiny)
library(plotly)
devmode()
ui <- fluidPage(
actionButton("add","add"),
plotlyOutput("p1")
)
server <- function(input, output, session) {
output$p1 <- renderPlotly({
"not something that can be plotted"
})
}
shinyApp(ui, server)