[英]Returning an invalid first argument error when returning a plot in a shiny app
Newbie user for Shiny so apologies if this is a silly question.如果这是一个愚蠢的问题,Shiny 的新手用户很抱歉。 I want to make an app version of a simulation function that I have already made.
我想制作一个我已经制作的模拟功能的应用程序版本。 Here's the original function:
这是原始函数:
GeometricBrownian<-function(trials = 100, periods = 10, mean = .07, sd = .1, capital = 100)
{
#Reads in the parameters
paths <- trials
count <- periods
interval<- 5/count
mean <- mean
sigma <- sd
sample <- capital
#Generate matrix based on parameters
sample<-matrix(0,nrow=(count+1),ncol=paths)
#generates samples using GBM and inputted parameters
for(i in 1:paths)
{
sample[1,i]<- capital
for(j in 2:(count+1))
{
sample[j,i]<-sample[j-1,i]*exp(interval*(mean-((sigma)^2)/2)+((interval)^.5)*rnorm(1)*sigma) #Expression for Geometric Brownian Motion
}
}
#Plot the samples generated
sample
matplot(sample,main="Geometric Brownian",xlab="Periods",ylab="Total Value",type="l")
#Reformat for summary results
df<- as_tibble(sample)
final <- na.trim(df[periods,1:trials])
final <- as.data.frame(t(final))
colnames(final) <- "Results"
results <- as.numeric(final[,1])
#Output a csv of trial results
#write_csv(df, "All Trials Time Series.csv")
#Print summary and histogram
print(summary(final))
print(
ggplot(final, aes(x=Results))+
geom_histogram(color = "white",
alpha = .8,
fill="lightblue",
bins = trials/(trials/10))+
labs(title = "Trial Results", y = "Frequency")+
theme_minimal()
)
}
And now for my preliminary attempt at making it into a shiny app:现在我初步尝试将它变成一个闪亮的应用程序:
library(tidyquant)
library(shiny)
library(shinydashboard)
ui <- fluidPage(
# App title ----
headerPanel("Return Simulation"),
# Sidebar panel for inputs ----
sidebarPanel(
numericInput("trials",
"Number of Trials:",
min = 10,
max = 10000,
step = 1,
value = 100),
numericInput("periods",
"Number of Periods:",
min = 2,
max = 10000,
step = 1,
value = 10),
numericInput("mean",
"Average Return (in decimals):",
min = -100,
max = 100,
step = .01,
value = .07),
numericInput("sd",
"Return Standard Deviation (in decimals):",
min = -100,
max = 100,
step =.01,
value = .2),
numericInput("capital",
"Starting Capital",
min = 0,
max = 100000,
value = 100),
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput("Plot")
)
)
server <- function(input,output){
Simulation<-function(trials = 100, periods = 10, mean = .07, sd = .1, capital = 100)
{
#Reads in the parameters
paths <- trials
count <- periods
interval<- 5/count
mean <- mean
sigma <- sd
sample <- capital
#Generate matrix based on parameters
sample<-matrix(0,nrow=(count+1),ncol=paths)
#generates samples using GBM and inputted parameters
for(i in 1:paths)
{
sample[1,i]<- capital
for(j in 2:(count+1))
{
sample[j,i]<-sample[j-1,i]*exp(interval*(mean-((sigma)^2)/2)+((interval)^.5)*rnorm(1)*sigma) #Expression for Geometric Brownian Motion
}
}
#Return a tibble
return(sample)
}
data <-reactive({Simulation(trials= input$trials,
periods = input$period,
mean =input$mean,
sd = input$sd,
capital = input$capital)
})
output$Plot <- renderPlot({
matplot(data,main="Geometric Brownian",xlab="Periods",ylab="Total Value",type="l")
})
}
shinyApp(ui,server)
runApp("~/shinyapp")
When I run the app, I get the following error:当我运行该应用程序时,出现以下错误:
Listening on http://127.0.0.1:4029
Warning: Error in <-: invalid first argument, must be vector (list or atomic)
[No stack trace available]
My main question is, what am I doing wrong here?我的主要问题是,我在这里做错了什么? I suspect it's an issue with how I set up my reactive functions, but even after looking at examples, I'm having a hard time finding the mistake.
我怀疑这是我如何设置响应式函数的问题,但即使在查看示例之后,我也很难找到错误。
data()
is a reactive function, you should call: data()
是一个反应函数,你应该调用:
output$Plot <- renderPlot({
matplot(data(),main="Geometric Brownian",xlab="Periods",ylab="Total Value",type="l")
})
and to avoid the NA error on input$period:并避免 input$period 上的 NA 错误:
data <-reactive({
req(!is.na(input$period))
Simulation(trials= input$trials,
periods = input$period,
mean =input$mean,
sd = input$sd,
capital = input$capital)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.