RShiny Plotly - 参数不是字符向量

时间:2018-03-03 09:26:10

标签: r shiny plotly

任何人都可以解释为什么这个例子给出错误:参数不是字符向量?

#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)

1 个答案:

答案 0 :(得分:1)

使用plotOutput输出使用renderUI呈现的对象。相反,您应该使用正确和匹配的渲染和输出元素。在这种情况下,renderPlotlyplotlyOutput

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)

或者,如果您要创建动态用户界面,请使用renderUIuiOutput,但仍需渲染图:

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)

希望这有帮助!