LP_640
LP_640

Reputation: 579

Shiny reactivity - how to take input and and create an array

I have not been able to figure out how to take numerical input values to create an array that I ultimately want to perform simulations on. It is easy enough to pass them directly to output (e.g the textOutput for "text1" [see server.R], which works fine when I remove the code for the array.)

Here is a simple example.

ui.R

library(shiny)
library(ggplot2)

shinyUI(pageWithSidebar(

  headerPanel("Wright Fisher Simulations with selection"),

  sidebarPanel(

    sliderInput("N", label="Population size (Ne):",value=30, min=1, max=10000,
            ),
    numericInput("p", "initial allele frequency of p:", .5,
             min = 0, max = 1, step = .05 ),


    submitButton("Run")
  ),

  mainPanel(
    textOutput("text1"),
    textOutput("text2")

  )
))

Here is the server.R file:

library(reshape)
library(shiny)
library(ggplot2)

shinyServer(function(input, output) {


  X = array (0,dim=c(input$N,input$N+1))
  output$text1 <- renderText({ 
   paste(" sqrt of N is :", sqrt(input$N))
   })

  output$text2 <- renderText({ 
    paste(" X is this many columns long:",length(X[1,]))
     })


}

)

I get this error:

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

My problem is the reactive conductors I think. Do I need to make a reactivity function with input$N? Why can't you simply create new variables based on the passed user input?

Any help will be much appreciated

LP

Upvotes: 1

Views: 2511

Answers (1)

Mahdi Jadaliha
Mahdi Jadaliha

Reputation: 1977

Maybe this is not the only problem, but error that you mentioned is because X is not defined as reactive value.

change server.r like this

library(reshape)
library(shiny)
library(ggplot2)

shinyServer(function(input, output) {


  X = reactive({array (0,dim=c(input$N,input$N+1)) })

  output$text1 <- renderText({ 
   paste(" sqrt of N is :", sqrt(input$N))
   }) 

  output$text2 <- renderText({ 
    paste(" X is this many columns long:",length(X()[1,]))
     })


}

)

Upvotes: 1

Related Questions