[英]R shiny How do I not run thw same function multiple times?
我想要:
(1)我從用戶那里獲取輸入(在這種玩具模型中,我獲取一個數據(一個常數到多個矩陣)。在現實世界中,我接受10個輸入)
(2)我使用該輸入來計算矩陣(在此玩具模型中,它只是用戶選擇的常數乘以的隨機矩陣)
(3)我將步驟2中的矩陣用於多個渲染圖中(即,圖1和圖2使用步驟2中的相同矩陣。我希望僅一次執行步驟2中的計算。)
我的問題在步驟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")
)
))
讓我知道是否不清楚。 謝謝大家的幫助!
是的,在地塊上方
eigenVectors<-reactive ({ eigenVectorsR() })
然后在以后需要時,將其稱為eigenVectors()
編輯
在查看了新近更新的問題之后,您唯一需要做的就是:
更換:
data[,1]<- testMatrix()
plot(data)
與:
plot(testMtrix()[,1])
或您希望的任何列。
您的測試應用程序現在對我有用,希望您一切順利。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.