Home > Back-end >  Pass reactive object as input to function in a shiny app
Pass reactive object as input to function in a shiny app

Time:05-01

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))
    }
      
      
      
  
  
}

shinyApp(ui = ui, server = server)

CodePudding user response:

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())
  • Related