任何人都可以解释为什么这个例子给出错误:参数不是字符向量?
#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)
您使用plotOutput
输出使用renderUI
渲染的对象。相反,您应该使用正确和匹配的渲染和输出元素。在这种情况下,renderPlotly
和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)
或者,如果您要创建动态UI,请使用renderUI
和uiOutput
,但您仍需渲染绘图:
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)
希望这可以帮助!