在将 Plotly 集成到闪亮仪表板中时,试图使用条件语句通过 selectInput 切换图表时遇到错误。我已经成功地使用 circlize 包和 ggplot 图形,但是在尝试使用 Plotly 时,出现以下错误:
"Error in UseMethod: no applicable method for 'plotly_build' applied to an object of class "NULL"
我在此处找到了一个与此类似但并未完全解决我的问题的帖子:
Convert ggplot object to plotly in shiny application
下面是一个示例,其中使用了与上述帖子类似的代码,但进行了修改以展示我想要做的事情以及不断弹出的错误提示。
"Error in UseMethod: no applicable method for 'plotly_build' applied to an object of class "NULL"
我在此处找到了一个与此类似但并未完全解决我的问题的帖子:
Convert ggplot object to plotly in shiny application
下面是一个示例,其中使用了与上述帖子类似的代码,但进行了修改以展示我想要做的事情以及不断弹出的错误提示。
library(shiny)
library(ggplot2)
library(ggthemes)
library(plotly)
ui = dashboardPage(
dashboardHeader(title = 'sample'),
dashboardSidebar(), ##Body content dashboardBody(
fluidRow(
box(background = "green", selectInput(inputId = "dimension",
label = strong("Choose Metric"),
choices = c('choice' = '1', 'choice2' = '2'),
multiple = FALSE, selectize = TRUE)),
box(plotlyOutput(outputId = 'plot2')))
))
server < - function(input, output) {
output$plot2 < -renderPlotly({
print(
ggplotly(
ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method =
lm, formula = y~x) + geom_point() + theme_gdocs()))
if (input$dimension == '2') {
print(
ggplotly(
ggplot(data = mtcars, aes(x = hp, y = cyl)) + geom_smooth(method =
lm, formula = y~x) + geom_point() + theme_gdocs()))
}
})
}
shinyApp(ui, server)
我还在学习中,所以肯定会有一些简单的错误逃过我的注意,但是我不确定它可能是什么。感谢您的帮助!