闪亮的反应性 - 如何获取输入和创建数组

时间:2014-09-12 20:25:35

标签: r shiny

我无法弄清楚如何使用数字输入值来创建一个我最终想要进行模拟的数组。很容易将它们直接传递给输出(例如textOutput for“text1”[请参阅server.R],当我删除数组的代码时,它可以正常工作。)

这是一个简单的例子。

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

  )
))

这是server.R文件:

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


}

)

我收到此错误:

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

我的问题是我认为的反应导体。我是否需要使用输入$ N制作反应函数?为什么不能根据传递的用户输入简单地创建新变量?

非常感谢任何帮助

LP

1 个答案:

答案 0 :(得分:1)

也许这不是唯一的问题,但您提到的错误是因为X未定义为无功值。

像这样更改server.r

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


}

)