Home > Net >  Invalid NULL assignment R when having to assign a variable to a shiny app
Invalid NULL assignment R when having to assign a variable to a shiny app

Time:03-12

I see many people running into a similar issue with great answers but I believe this is an unanswered variation to this question!

I have a graph


g <- reactive(graph_from_dataframe(df))

Now I want to assign a degree property to this.


V(g())$degree <- degree(g())

This howevers throws the null assignment error but I don't know how to assign it without calling g. I avoided this by assigning the degree property inside an output$... however this heavily violates DRY and leads to double degree calculation.

CodePudding user response:

I guess the following syntax should work

V(g)$degree <- degree(g)

CodePudding user response:

I do not think that reactive(.) objects take assignments.

Two thoughts:

  1. Use reactiveVal:

    g <- reactiveVal()
    observeEvent(df, {
      newg <- graph_from_dataframe(df)
      V(newg)$degree <- degree(new)
      g(newg)
    })
    
  2. Handle the $degree assignment in the original reactive:

    g <- reactive({
      newg <- graph_from_dataframe(df)
      V(newg)$degree <- degree(newg)
      newg
    })
    
  • Related