How to handle empty data frame displaying text message

325 views
Skip to first unread message

Ed

unread,
Oct 22, 2014, 6:21:13 AM10/22/14
to shiny-...@googlegroups.com
Hi everyone,

I'm using the functions renderTable and tableOutput to display a data frame that changes depending on the input parameter. 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    output$geneDe <- renderTable({
      isDeGene(input$geneSymbol)
    })
}
)


It may happen that the data frame is empty. In this case, I would like to display some message. I can't see how I can do that with Shiny (I'm very new to it). 

The second question is kind of related with the first one.

I would like to display both my data frame and its length. Somehow I would like to do something like that : 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    df = isDeGene(input$geneSymbol)

    output$geneDe <- renderTable({
      df
    })

    output$nbGeneDe <- renderText({
      paste("Data frame length : ",length(df))
    })



}
)

But I'm getting the following error message : 

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Can you please help me with that ?

Thank you

Nicola Sturaro Sommacal

unread,
Oct 22, 2014, 8:04:43 AM10/22/14
to shiny-...@googlegroups.com
Hi


On Wednesday, October 22, 2014 12:21:13 PM UTC+2, Ed wrote:
Hi everyone,

I'm using the functions renderTable and tableOutput to display a data frame that changes depending on the input parameter. 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    output$geneDe <- renderTable({
      isDeGene(input$geneSymbol)
    })
}
)

It may happen that the data frame is empty. In this case, I would like to display some message. I can't see how I can do that with Shiny (I'm very new to it). 

A solution is quite simple: wrap your isDeGene() function inside an if environment, returning something only if your data frame has at least one row. Similarly, you can add a renderText, producing a message when your data is empty.
 
The second question is kind of related with the first one.

I would like to display both my data frame and its length. Somehow I would like to do something like that : 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    df = isDeGene(input$geneSymbol)

    output$geneDe <- renderTable({
      df
    })

    output$nbGeneDe <- renderText({
      paste("Data frame length : ",length(df))
    })



}
)

But I'm getting the following error message : 

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

df depends from an input value, so it must be defined in a reactive expression, as the message says.

Try replacing  df = isDeGene(input$geneSymbol with df <- reactive({isDeGene(input$geneSymbol)}) 

Ed

unread,
Oct 23, 2014, 8:20:24 AM10/23/14
to shiny-...@googlegroups.com
Hi Nicola

Le mercredi 22 octobre 2014 14:04:43 UTC+2, Nicola Sturaro Sommacal a écrit :
Hi

On Wednesday, October 22, 2014 12:21:13 PM UTC+2, Ed wrote:
Hi everyone,

I'm using the functions renderTable and tableOutput to display a data frame that changes depending on the input parameter. 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    output$geneDe <- renderTable({
      isDeGene(input$geneSymbol)
    })
}
)

It may happen that the data frame is empty. In this case, I would like to display some message. I can't see how I can do that with Shiny (I'm very new to it). 

A solution is quite simple: wrap your isDeGene() function inside an if environment, returning something only if your data frame has at least one row. Similarly, you can add a renderText, producing a message when your data is empty.
 

Well sure I would like to use an if statement. I tried to, but I'm getting the same error message about reactive context.

 
The second question is kind of related with the first one.

I would like to display both my data frame and its length. Somehow I would like to do something like that : 


source("isDeGene.R")

shinyServer(
  function(input, output) {

    df = isDeGene(input$geneSymbol)

    output$geneDe <- renderTable({
      df
    })

    output$nbGeneDe <- renderText({
      paste("Data frame length : ",length(df))
    })



}
)

But I'm getting the following error message : 

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

df depends from an input value, so it must be defined in a reactive expression, as the message says.

Try replacing  df = isDeGene(input$geneSymbol with df <- reactive({isDeGene(input$geneSymbol)}) 

Yep I tried that, problem doing that is the object df it returns is no longer a data frame but some kind of function and I don't know how to access my data. 

Thanks for your answer anyway.

Reply all
Reply to author
Forward
0 new messages