R Shiny Plotly - 参数不是字符向量

问题描述 投票:1回答:1

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

#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)
r shiny plotly
1个回答
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)

或者,如果您要创建动态UI,请使用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)

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.