Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactives: invalid (NULL) left side of assignment

Tags:

r

shiny

I'm trying to extract an output of a personal function called tests.stats()

I make a function with return statement:

   return(c(list.test.1, list.test.2 ,list.test.3, list.test.4))

Where each list.test.<i> is a list object.

In Shiny Server:

 outlist() <- reactive({
   if(is.null(input$datafile)){
     return()
   }
   if(is.null(input$varinteret)
      &&is.null(input$vartemps)
      &&is.null(input$varsujet)
      &&is.null(input$varprod) 
      &&is.null(input$apparie)
      &&is.null(input$tempsrefouinon)
      &&is.null(input$prodrefouinon)
      &&is.null(input$checkprod)
      &&is.null(input$checkprodref)){
     return()
   } else {
     data<-filedata()
     res <- tests.stats(data = data(),
              y = input$varinteret, 
              group = input$varprod, 
              group2 = input$vartemps, 
              sujet = input$varsujet, 
              apparie = input$apparie, 
              temoin = input$prodrefouinon, 
              TemoinName = input$checkprodref)
   }  

and the error message:

Warning: Error in <-: invalid (NULL) left side of assignment
Stack trace (innermost first):
    40: server [C:\Users\itm\Desktop\Documents\appli/server.R#357]
     1: shiny::runApp
Error in outlist() <- reactive({ : invalid (NULL) left side of assignment

Why am I receiving this error?

like image 885
Maïna Kerbrat Avatar asked Mar 03 '16 10:03

Maïna Kerbrat


1 Answers

This happened because of this assignment:

outlist() <- reactive({...})

This is incorrect syntax as you are telling Shiny that outlist is an already defined reactive expression and that you should evaluate it. You are then trying to assign something to this reactive expression in a syntax that Shiny does not understand.

Therefore if you instead use:

outlist <- reactive({...})

You have now correctly defined the outlist as a reactive Shiny object which can be called with outlist() elsewhere in the code.

The documentation for this topic is very good, and can be found here:

http://shiny.rstudio.com/reference/shiny/latest/reactive.html

http://shiny.rstudio.com/tutorial/lesson6/

It seems like you could also make use of validate and need to handle your sea of requires inputs:

http://shiny.rstudio.com/articles/validation.html

And also a style guide for your code from Lord Hadley:

http://adv-r.had.co.nz/Style.html

like image 91
mlegge Avatar answered Nov 07 '22 04:11

mlegge