2016-08-03 7 views
2

이 광택 대시 보드에 플롯으로 통합되고 조건문을 사용할 때 오류가 발생하면 'plotly_build'에 적용 가능한 메소드가 "NULL"클래스의 객체에 적용되지 않습니다. selectInput 함께 'if'문을 사용하여 차트를 전환하려고합니다. 나는 circlize 패키지와 ggplot 그래프를 문제없이 사용했지만 음모와 함께 사용하려고하면 다음과 같은 오류가 발생합니다.ggplotly 문법

UseMethod에있는 오류 : 'plotly_build'의 적용 가능한 메소드가 클래스 " 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) 

난 아직도이 저를 탈출하는 간단한 오류 확신 너무 배우고하지만 난 그게 될 일을 확실입니다. 도움을 감사하십시오!

답변

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