R闪亮我怎么不多次运行相同的功能?

时间:2013-12-06 19:28:03

标签: r shiny

我想:

(1)我接受了用户的输入(在这个玩具模型中,我取一个数据(一个常数到多个矩阵)。在现实世界中,我需要10个输入)

(2)我用那个输入来计算一个矩阵(在这个玩具模型中,它只是一个随机矩阵乘以用户选择的常数)

(3)我使用步骤2中的矩阵进行多次渲染绘图(即绘图#1和绘图#2使用步骤2中的相同矩阵。我希望步骤2中的计算仅在一次完成。)< / p>

我的问题出在第3步。我不知道如何编写代码,以便步骤#2中的矩阵只计算一次。

我目前收到错误消息: 'closure'类型的对象不是可子集化的

我附上了运行玩具模型的代码,这里是启动它的代码。只需将它放在一个新的脚本中,其中“testShiny”是R项目的名称:

library(shiny)
runApp("C:/Users/me/Desktop/R Projects/testShiny")

这是您可以创建的server.R文件:

 library(shiny)

 shinyServer(function(input, output) {

number<- reactive({ input$Number}) #get the input from the user


  testMatrix<- function()
  {
    number<- as.numeric(number())
     testMatrix<- replicate(10, rnorm(10)) *number #do some stuff with the input and return a matrix
  }

getMatrix<- reactive ({ testMatrix() }) #return the matrix to a matrix that can be used multiple     times and will recalulate when the user changes the  UI 

output$plotVector1 <- renderPlot({ 
    data[,1]<- testMatrix()
    plot(data) #plot the 1st column of the matrix
    })

output$plotVector2 <- renderPlot({ 
  data[,2]<- testMatrix()
  plot(data) #plot the 2nd column of the matrix
})

})

这是您可以创建的ui.R文件:

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Hello Shiny!"),

  sidebarPanel(selectInput("Number", "Select Numbers", c(1,2,3,4), selected = 5)),

  mainPanel(
    plotOutput("plotVector1"),
    plotOutput("plotVector2")   
             )
))

如果不清楚,请告诉我。谢谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

是的,在图上方

eigenVectors<-reactive ({ eigenVectorsR() })

稍后当您需要时,请将其称为eigenVectors()

修改

在查看最新更新的问题后 - 您唯一需要做的就是:

取代:

data[,1]<- testMatrix()
plot(data)

with:

plot(testMtrix()[,1])

或您希望的任何一列。

您的测试应用程序现在适用于我,希望您很高兴。