Mike Tauber
Mike Tauber

Reputation: 59

RShiny Plotly - argument is not a character vector

Can anyone explain why this example gives the error: Argument is not a character vector?

#p <- plot_ly(
#  x = c("giraffes", "orangutans", "monkeys"),
#  y = c(20, 14, 23),
#  name = "SF Zoo",
#  type = "bar")

# Define UI for application that draws a histogram
ui <- fluidPage(plotOutput("distPlot"))

# Define server logic required 
server <- function(input, output) {

output$distPlot <- renderUI({p})

}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 2

Views: 1692

Answers (1)

Florian
Florian

Reputation: 25385

You use plotOutput to output an object that is rendered with renderUI. Instead you should use correct and matching render- and output elements. In this case, renderPlotly and plotlyOutput.

library(plotly)
library(shiny)

p <- plot_ly(
 x = c("giraffes", "orangutans", "monkeys"),
 y = c(20, 14, 23),
 name = "SF Zoo",
 type = "bar")

# Define UI for application that draws a histogram
ui <- fluidPage(plotlyOutput("distPlot"))

# Define server logic required 
server <- function(input, output) {

  output$distPlot <- renderPlotly({p})

}

# Run the application 
shinyApp(ui = ui, server = server)

Or alternatively, if you are creating a dynamic UI, use renderUI and uiOutput, but you still have to render the plot:

library(plotly)
library(shiny)

p <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar")

# Define UI for application that draws a histogram
ui <- fluidPage(uiOutput("distPlot"))

# Define server logic required 
server <- function(input, output) {

  output$distPlot <- renderUI({
    tagList(renderPlotly({p}))
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

Hope this helps!

Upvotes: 1

Related Questions