Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to return from ggvis when the data is empty?

Tags:

r

shiny

ggvis

My ggvis plot depends on several input fields that work like filter for input data. For some of combinations the resulting data frame is empty and ggvis throws error and breaks the whole application. I tried to put

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

which didn't help. What is the proper return parameter in this situation (I want to have an empty plot or some text message instead)?

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")

Update

I don't want to show all data when data is empty (as suggested here) It's confusing

like image 340
RInatM Avatar asked Sep 23 '14 14:09

RInatM


2 Answers

So it would have been helpful to see some more of the logic in your code. I guess I'd say in general it's really important to understand how reactive expressions work in the context of program logic. I'd try reading as much code on the shiny home page as you can. Here is a quick script I wrote up that I think gets at what you are asking. cheers.

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")
       )
      )
     )
    )  
like image 121
miles2know Avatar answered Nov 18 '22 14:11

miles2know


In my case, with the latest shiny, I can just send an empty data.table (or data.frame) and shiny just plots an empty graph (the data.frame or data.table has to have the corresponding columns)

like image 26
Juancentro Avatar answered Nov 18 '22 15:11

Juancentro