firmo23
firmo23

Reputation: 8404

Pass reactive object as input to function in a shiny app

In the shiny app below I want to pass as input in the initialize_NN() function the layers() which is a reactive object. But when I try to pass it as layers() instead of layers I get an error in my console like unexpected token'layers',expected RPAREN. Any solution to this or any workaround?The initialize_NN() do not expect to give something now but I just want to know how could I pass reactive input into it.

    library(shiny)
    library(dplyr)
    
    ### user inter phase
    ui <- fluidPage(
      
      ### App title ----
      
      
      titlePanel(
        #title=div(img(src="pics/IRP_NHSc.jpg", width="99%"))
        title="Network App"
      )
      ,
      
      # Sidebar layout with input and output definitions ----
      sidebarLayout(
        
        # Sidebar panel for inputs ----
        sidebarPanel(
          sliderInput("lay","Adjust layers",min=2,max = 6,value = 2,step=1),
          sliderInput("neu","Adjust Neurons",min=10,max = 60,value = 50,step=1)
          
          
          
          
        ),
        
        ### Main panel for displaying outputs ----
        
        mainPanel(
          
         
        )
      )
    )
    
    
    #### Server 
    server <- function(input, output, session) {
     
      layers <- reactive({
        l<-c(1, rep(input$neu, input$lay), 1)
        
      })
      
      
       
  

    initialize_NN <- function(layers()){
        weights <-c()
        biases <- c()
        num_layers <- length(layers())-1
        for (i in seq(num_layers)){
          W <- xavier_init(c(layers()[i], layers()[i+1]))
          weights <- c(weights, W)
          biases <- c(biases, b)
        }
        return(list(Weights = weights, Biases = biases))
        }
          
  obj <- initialize_NN(layers=layers())
  weights <- obj$Weights
  biases <- obj$Biases

neural_net <- function(x_, weights, biases){
    num_layers <- length(weights) + 1
    H <- x_
    for (l in seq(num_layers-2)){
      W <- weights[[l]]
      b <- biases[[l]]
      H <- tf$nn$tanh(tf$add(tf$matmul(H, W), b))
    }
    W <- tail(weights, n=1)[1]
    b <- tail(biases, n=1)[1]
    Y <- tf$add(tf$matmul(H, W), b)
    return(Y)
  }
   
          
      
      
    }
    
    shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 640

Answers (1)

Mwavu
Mwavu

Reputation: 2217

Define function initialize_NN() with "normal" arguments:

initialize_NN <- function(layers){
  weights <-c()
  biases <- c()
  num_layers <- length(layers)-1
  for (i in seq(num_layers)){
    W <- xavier_init(c(layers[i], layers[i+1]))
    weights <- c(weights, W)
    biases <- c(biases, b)
  }
  return(list(Weights = weights, Biases = biases))
}

Then wherever you call the function pass your reactive as the argument layers:

initialize_NN(layers = layers())

Edit 1:

As @jpdugo17 pointed out in the comments, it needs to be done inside a reactive context: whether a reactive(), observe(), observeEvent() etc.

For example:

r_NN <- reactive({
  initialize_NN(layers = layers())
})

Upvotes: 2

Related Questions