Error with renderPlot but not renderPrint when using shinyjs

While trying to help this person, I got stuck with a problem with shinyjs.

In the example below, clicking on the big green rectangle prints the text, as it should be:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  div(id = "01", 
      style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
      HTML("01")),
  textOutput("plot")
)

server <- function(input, output) {
  
  onclick(id = "01", {
    output$plot <- renderPrint({
      print("foo")
    })
  })
}

shinyApp(ui, server)

However, if I try to replace the print with a plot, I am facing the error:

Warning: Error in origRenderFunc: argument "name" missing, with no default

Here's the code that creates the error:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  div(id = "01", 
      style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
      HTML("01")),
  plotOutput("plot")
)

server <- function(input, output) {
  
  onclick(id = "01", {
    output$plot <- renderPlot({
      plot(mtcars)
    })
  })
}

shinyApp(ui, server)

I suppose the problem comes from the function onclick since I can't reproduce this error if I put renderPlot outside of onclick. Does anybody know how to solve this?

Also asked on StackOverflow

I heve tried to put plot outside of onclick() and it works

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  div(id = "01", 
      style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", 
      HTML("01")),
  fluidRow(plotOutput("plot"))
)

server <- function(input, output) {
  
  p <- output$plot <- renderPlot(plot(mtcars))
  
  onclick("01", p)
    

Hi @Eve_Zen , have you seen my answer below your post on StackOverflow?

1 Like

Oops, i didnt noticed that, sorry

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