2016-11-24 71 views
0

我試圖添加一個rCharts輸出到一個閃亮的應用程序。如果輸入無效,那麼我不想輸出任何東西 - 所以我希望能夠返回NULL或在輸出中以某種方式產生「空白圖」。但我無法弄清楚如何在rCharts中創建一個空的圖。如何在rCharts中創建一個空的圖?

這裏是我的代碼中,例如取直出README,但我註釋掉的情節代碼,返回NULL,而不是

library(shiny) 
library(rCharts) 

ui <- pageWithSidebar(
    headerPanel("rCharts: Interactive Charts from R using polychart.js"), 

    sidebarPanel(
    selectInput(inputId = "x", 
       label = "Choose X", 
       choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), 
       selected = "SepalLength"), 
    selectInput(inputId = "y", 
       label = "Choose Y", 
       choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), 
       selected = "SepalWidth") 
), 
    mainPanel(
    showOutput("myChart", "polycharts") 
) 
) 

server<- function(input, output) { 
    output$myChart <- renderChart({ 
    # names(iris) = gsub("\\.", "", names(iris)) 
    # p1 <- rPlot(input$x, input$y, data = iris, color = "Species", 
    #    facet = "Species", type = 'point') 
    # p1$addParams(dom = 'myChart') 
    # return(p1) 
    return(NULL) 
    }) 
} 

shinyApp(ui, server) 

回答

1

showOutput預計的對象返回正確的,所以爲什麼你給回該類的空對象。

output$myChart <- renderChart({ 
     mychart <- Polycharts$new() 
     return(mychart) 
}) 
+0

謝謝。我是rCharts的完全noob,這正是我需要知道如何去做的。謝謝。我看到一個不同的答案,說我應該使用'renderChart2'而不是 - 你碰巧知道區別? –

+1

我不是100%肯定是誠實的時候使用一個或另一個,我認爲有一些庫的依賴,它必須返回一個對象,所以他引入了一個補丁'renderChart2'來覆蓋,但再次我不是100%確定。主要區別在於你使用的庫如'polycharts','nvd3','highcharts'或其他 –