Correct way to return from ggvis when data is empty?

My ggvis graph depends on several input fields that act as a filter for input. For some combinations, the resulting data frame is empty, and ggvis throws an error and crashes the entire application. I tried to put

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

that didn't help. What is the correct return option in this situation (I want to have an empty storyline or 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 do not want to show all the data when the data is empty (as suggested here ) This is confusing

+6
source share
2 answers

In my case, with the last shine, I can just send an empty data.table (or data.frame) table and a brilliant just graph empty graph (data.frame or data.table should have the corresponding columns)

+3
source

Thus, it would be useful to see another logic of your code. Perhaps I would say that in general it is very important to understand how reactive expressions work in the context of program logic. I would try to read as much code as possible on the shiny homepage. Here is a quick script, I wrote that I think it turns out that you are asking. amuses.

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") ) ) ) ) 
+4
source

Source: https://habr.com/ru/post/975720/


All Articles