R gg几乎没有适用于'的方法;精心打造';适用于“类”的对象;空";如果语句
正在将plotly集成到Shining dashboard中,在使用条件语句时遇到错误。我试图使用带有selectInput的“if”语句在图表中切换。我在Circize软件包和ggplot图形中使用了此功能,但在尝试将其用于plotly时,出现以下错误: UseMethod中出错:没有适用于“plotly_build”的方法应用于类为“NULL”的对象 我在这里发现了一个类似问题的帖子,但没有完全回答我的具体问题: 下面是一个示例,使用了与上面帖子中使用的代码类似的代码,但经过了修改以显示我希望做的事情以及不断出现的错误:R gg几乎没有适用于'的方法;精心打造';适用于“类”的对象;空";如果语句,r,ggplot2,shiny,plotly,shinydashboard,R,Ggplot2,Shiny,Plotly,Shinydashboard,正在将plotly集成到Shining dashboard中,在使用条件语句时遇到错误。我试图使用带有selectInput的“if”语句在图表中切换。我在Circize软件包和ggplot图形中使用了此功能,但在尝试将其用于plotly时,出现以下错误: UseMethod中出错:没有适用于“plotly_build”的方法应用于类为“NULL”的对象 我在这里发现了一个类似问题的帖子,但没有完全回答我的具体问题: 下面是一个示例,使用了与上面帖子中使用的代码类似的代码,但经过了修改以显示我
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)
库(闪亮)
图书馆(GG2)
图书馆(主题)
图书馆(绘本)
ui=仪表板页面(
仪表板标题(标题=‘示例’),
仪表板侧栏(),##主体内容仪表板主体(
fluidRow(
框(background=“green”),选择输入(inputId=“dimension”,
label=strong(“选择度量”),
choices=c('choice'='1','choice2'='2'),
multiple=FALSE,selectize=TRUE)),
框(plotlyOutput(outputId='plot2'))
))
服务器<-功能(输入、输出){
输出$plot2<-renderpltly({
印刷品(
绘声绘色(
ggplot(数据=mtcars,aes(x=显示,y=循环))+geom_平滑(方法=
lm,公式=y~x)+几何点()+主题点()
如果(输入$dimension==“2”){
印刷品(
绘声绘色(
ggplot(数据=mtcars,aes(x=马力,y=气缸))+geom_平滑(方法=
lm,公式=y~x)+几何点()+主题点()
}
})
}
shinyApp(用户界面、服务器)
我还在学习,所以我确信这是一个简单的错误,但我不确定它可能是什么。谢谢你的帮助 很快,问题是如果
input$dimension
是'1'
,则没有返回值。您正在打印绘图,但R进一步检查了条件是否满足。有几种方法可以正确地编写代码
您可以将绘图保存在对象中,例如res
,如果满足条件,则使用新绘图覆盖它,最后在函数结束时返回它-参见下面的示例。您还可以使用else
语句
library(shiny)
library(shinydashboard)
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({
res <- ggplotly(
ggplot(data = mtcars, aes(x = disp, y = cyl)) +
geom_smooth(method = lm, formula = y ~ x) +
geom_point() +
theme_gdocs())
if (input$dimension == '2') {
res <- ggplotly(
ggplot(data = mtcars, aes(x = hp, y = cyl)) +
geom_smooth(method = lm, formula = y ~ x) +
geom_point() +
theme_gdocs())
}
res
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)
图书馆(GG2)
图书馆(主题)
图书馆(绘本)
ui=仪表板页面(
仪表板标题(标题=‘示例’),
仪表板侧栏(),
##身体内容
仪表板主体(
fluidRow(
框(background=“green”),选择输入(inputId=“dimension”,
label=strong(“选择度量”),
choices=c('choice'='1','choice2'='2'),
multiple=FALSE,selectize=TRUE)),
框(plotlyOutput(outputId='plot2'))
))
服务器这是完美的,像一个魅力。非常感谢您的帮助!