2014-09-12 73 views
1

我一直无法弄清楚如何将数值输入值创建为我最终想要在其上执行模拟的数组。很容易直接将它们传递给输出(例如,textOutput为“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

也许这是不是唯一的问题,但你提到的错误是因为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,])) 
    }) 


} 

) 
+0

大约反应性官能团的更多信息可以在[闪亮教程]中找到(http://shiny.rstudio.com/tutorial/lesson6/)。 – bskaggs 2014-09-12 20:33:46

+0

好的。这工作。谢谢!但是如果我想稍后或进一步使用该阵列呢? – 2014-09-12 21:02:09

+0

例如,我想让数组的第一行取初始值,比如0.5。我试过这段代码:'X [1,] < - reactive({ – 2014-09-12 21:04:28