R Shiny Plotly - 参数不是字符向量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
答案
您使用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)
希望这可以帮助!
以上是关于R Shiny Plotly - 参数不是字符向量的主要内容,如果未能解决你的问题,请参考以下文章
在 R Shiny 中调整 Plotly::subplot 的高度和宽度
plotly::subplot 注释标题在 R Shiny 中消失