2014-09-23 21 views
6

我的ggvis圖取決於幾個輸入字段,它們像輸入數據的過濾器一樣工作。對於某些組合,結果數據框是空的,ggvis會拋出錯誤並中斷整個應用程序。 我試圖把數據爲空時從ggvis返回的正確方法是什麼?

if(nrow(inputdataframe) == 0) return NULL 
if(nrow(inputdataframe) == 0) return ggvis() 

哪沒有幫助。在這種情況下什麼是正確的返回參數(我想要有一個空的圖或一些文本消息)?

server.R

effvis <- reactive ({ 
     dta <- siteeff() 
     if(nrow(dta) == 0) return(NULL) 
     dta %>% 
      ggvis(~param.value, ~yvar) %>% 
      layer_points(size := 50, size.hover := 200) %>% 
      set_options(width = 800, height = 500) 
}) 
effvis %>% bind_shiny("effplot") 

ui.R -

ggvisOutput("effplot") 

更新

我不想顯示所有數據時,數據是空的(如建議here )這很令人困惑

+0

我也對此感興趣。我只是有條件地提供一個data.frame與相同的列和零。你應該可以在ui.R中使用'conditionalPanel'來查看反應字符串,該字符串指示data.frame是否爲空,但它並不總是適用於我的情況。 – ideamotor 2014-09-23 19:03:45

+0

您的「此處」鏈接對我來說是有幫助的。我還會查看她在評論中工作的應用程序的鏈接。你的UI有輸入控件嗎?您應該測試那些存在於server.r – miles2know 2014-09-25 02:01:08

+0

@ miles2know謝謝指向評論。事實上,我看着她的代碼,發現了我不知道的validate()函數 - http://shiny.rstudio.com/articles/validation.html 但是,它似乎並不適用於ggvis – RInatM 2014-09-25 08:24:09

回答

3

在我的情況下,用最新的閃亮,我可以發送一個n空的data.table(或data.frame)和閃亮只是繪製一個空的圖形(data.frame或data.table必須有相應的列)

+0

您是否也使用過遠程'dplyr'表?我不能再現這個例子,因爲我不知道任何公共數據庫我可以使用,但當tbl不是本地的時候出現以下錯誤:使用方法錯誤(「Prop_type」): 沒有適用於'prop_type'的適用方法一個類的對象「c('tbl_mysql','tbl_sql','tbl_lazy','tbl')」' – Dambo 2016-08-05 21:11:42

+0

感謝您的答案,這是我做了解決我的應用程序中類似的情況,但我想補充說你必須返回一個ggvis對象而不是data.frame。也許你的意思,但我不得不再努力爭取,所以相反,在重新調用NULL時,你必須返回一個空的data.frame與相同的列,然後使用dplyr來創建ggvis'data.frame(col1 ='',col2 ='')%>%ggvis(〜col1,〜col2)%>%layer_points()'+ for the answer :) – ghub24 2017-08-10 07:46:25

4

所以它會一直有助於查看代碼中的更多邏輯。我想我會說一般情況下,瞭解被動表達式在程序邏輯上如何工作是非常重要的。我會盡量在閃亮的主頁上閱讀儘可能多的代碼。這是我寫的一個快速腳本,我認爲可以得到你所問的內容。乾杯。

Global.r

library(plyr) 
    library(dplyr) 

    exp <- data.frame(Ind=rep(c("a","b"),each=50),val1=rgamma(100,10,5),val2=rnorm(100,2,3.5))

Server.r

library(shiny) 
    library(ggvis) 

    shinyServer(function(input, output, session) { 

    output$selectO <- renderUI({ selectInput(inputId="selectI", label = h4("Level to Plot"), 
     choices = list("a","b","c"),selected="a") 
    }) 

    observe({ 

    if(!is.null(input$selectI)){ 

    expfilter <- reactive({ 
      vals <- exp %>% filter(Ind == input$selectI) 
      return(vals) 
    }) 

    if(nrow(expfilter())==0){ 

    fail <- reactive({ return("filter failed") }) 

    output$trouble <- renderText({fail()}) # notice the use of() since fail is a function. when you want to grab the values of reactives use the() 

    } else { 

    success <- reactive({ return("good") }) 

    output$trouble <- renderText({success()}) 

    P1 <- reactive({ 
     expfilter %>% ggvis(~val1, ~val2) %>% 
     add_axis("x",title="Var1",title_offset=45,properties=axis_props(labels=list(fontSize = 13, fontWeight = "bold"),title=list(fontSize = 15))) %>% 
     add_axis("y",properties=axis_props(labels=list(fontSize = 13, fontWeight = "bold"))) 
    }) 

    P1 %>% bind_shiny("plot1") 

     } 
     } 
    }) 
    }) 


ui.r

library(shiny) 

    shinyUI(fluidPage(
    column(3, 
     wellPanel(
     uiOutput("selectO") 
     ) 
    ), 
    column(9, 
     wellPanel(
     ggvisOutput("plot1")), 
     wellPanel(h6("How Did the Filter Do"), 
     textOutput("trouble") 
     ) 
    ) 
    ) 
    ) 
+0

感謝這段代碼,它讓我意識到,它本身並不是ggviz,但只有%>%layer_smooths()負責破壞代碼 如果將%>%layer_smooths()添加到您的圖中,則您的解決方法不起作用,應用程序中斷。我提交了一個問題,但問題仍然存在 - 如何在數據爲空時啓動ggvis – RInatM 2014-09-26 08:03:33

+0

嗨。這並不令我感到驚訝。我並沒有遇到你的ggvis代碼的問題,我只是扔在任何舊代碼,我躺在那裏工作。它看起來像你試圖使用閃亮,並找出如何當你的過濾器空空如也不讓​​它破碎。那正是我所掌握的。所以除非我誤解了你,否則上面的代碼提供了前進的方向。再次,雖然我上面的例子並不是要證明ggvis本身特有的任何東西。祝你好運。 – miles2know 2014-09-26 11:23:31

+0

'observe()'函數對我來說是缺失的部分。乾杯。 – 2015-01-20 04:15:56

相關問題