2016-08-03 6 views
2

を光沢のあるダッシュボードにプロットして統合していて、条件文を使用しているときにエラーが発生した場合、クラス「NULL」のオブジェクトに適用される 'plotly_build'の適用可能なメソッドがありません。 selectInputでif文を使ってチャートを切り替えようとしています。私はこれをcirclizeパッケージとggplotグラフで問題なく使用しましたが、プロットして使用しようとすると、次のエラーが発生します。ggplotlyステートメント

UseMethodのエラー: 'plotly_build' NULL」

私は同様の問題で、ここでの投稿を見つけましたが、まさに私の具体的な質問に答えるではない。ここで

Convert ggplot object to plotly in shiny application

を上記の投稿ではなくて使用されるものに類似したコードを使用したサンプルであります私が何を探しているのか、ポップを維持するエラーを表示するための変更pingがアップ:

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) 

私はまだ私はこの私をエスケープし、単純なエラーと確信しているので、学んでいるが、私はそれが何であるかわかりませんよ。助けを感謝します!

答えて

2

まもなく、input$dimensionがの場合、戻り値はありませんでした。あなたはプロットを印刷していましたが、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) 
+1

これは完璧で、魅力的なように機能しました。大幅に助けを感謝します! – LoF10

関連する問題