Monte Carlo模拟的闪亮应用程序

Monte Carlo模拟的闪亮应用程序,r,shiny,montecarlo,R,Shiny,Montecarlo,我正在尝试在R中为蒙特卡罗模拟构建一个闪亮的应用程序。所以我想预测一家公司的收入,它依赖于4个变量,a,B,C,D。 我希望用户输入这些变量的最小值和最大值,根据这些值生成统一的随机数,从而计算收入 请在下面找到我正在使用的代码- 用户界面 服务器.R shinyServer(function(input, output) { getAFYP_round <- reactive({ set.seed=12345 options(scip

我正在尝试在R中为蒙特卡罗模拟构建一个闪亮的应用程序。所以我想预测一家公司的收入,它依赖于4个变量,a,B,C,D。 我希望用户输入这些变量的最小值和最大值,根据这些值生成统一的随机数,从而计算收入

请在下面找到我正在使用的代码-

用户界面

服务器.R

    shinyServer(function(input, output) {

      getAFYP_round <- reactive({
        set.seed=12345
        options(scipen = 999)
        AA<-runif(10000, input$num1, input$num2)
        BB<-runif(10000, input$num3, input$num4)
        CC<-runif(10000, input$num5, input$num6)
        DD<-runif(10000, input$num7, input$num8)

        AFYP <- AA*BB*CC*DD

        AFYP_round=round(AFYP, digits = -7)
        AFYP_round
      })

      output$distPlot <- renderPlot({
        options(scipen = 999)
        AFYP_round<-getAFYP_round() 
        zz<-as.data.frame(AFYP_round)
        Freqa=as.data.frame(prop.table(table(AFYP_round)))
        library(dplyr)
        Freqa=Freqa %>% mutate(cumsum = cumsum(Freq))
        library(ggplot2)
        ggplot(Freqa, aes(x=AFYP_round, y=Freq)) +         geom_bar(stat='identity',col="red", fill="yellow") + labs(title="Histogram for Simulation")+labs(x="AFYP", y="Freq")

      })

      output$mytable <- renderUI({
        options(scipen = 999)
        AFYP<-getAFYP_round()
        c1 <- c("Mean", mean(AFYP))
        c2 <- c("Std Dev", sd(AFYP))
        c3 <- c("Minimum", min(AFYP))
        c4 <- c("Maximum", max(AFYP))
        c5 <- c("Maximum", median(AFYP)) 
        c6 <- c("Q(.25)", quantile(AFYP,0.25)) 
        c7 <- c("Q(.75)", quantile(AFYP,0.75))
        c8 <- c("Q(.05)", quantile(AFYP,0.05))
        c9 <- c("Q(.95)", quantile(AFYP,0.95))
        c10 <- c("Q(.025)", quantile(AFYP,0.025))
        c11 <- c("Q(.975)", quantile(AFYP,0.975))

        Summary=rbind(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11)
        ab<-as.data.frame(Summary)
        names(ab)[names(ab)=="V1"] <- "Measures"
        names(ab)[names(ab)=="25%"] <- "Value"
        output$ac <- renderDataTable(ab,options = list(paging = FALSE,searching = FALSE))
        dataTableOutput("ac")

      })

    })
shinyServer(功能(输入、输出){

getAFYP_round我在你的代码中做了一些
print()
,CC和DD被生成。@ChristopheD。你把print()放在哪里?就在
CCTrue之后。但是在那之后,它没有被用于AFYP公式中。或者CC和DD的向量被生成。CC和DD被使用,我试图打印
平均值(round(AA*BB,数字=-7))
mean(round(AA*BB*CC,digits=-7))
mean(round(AA*BB*CC*DD,digits=-7))
结果是
[1]0[1]142093000[1]2127709000
我想我不明白你看到的问题我在你的代码中做了一些
print()
,然后生成了CC和DD。@ChristopheD.你把print()放在哪里?就在
CCTrue之后。但是在那之后,它没有在AFYP公式中使用。或者生成了CC和DD的向量。使用了CC和DD我尝试打印
平均值(四舍五入(AA*BB,数字=-7))
平均值(四舍五入(AA*BB*CC,数字=-7))
结果是
[1]0[1]142093000[1]2127709000
我想我不明白你看到的问题
    shinyServer(function(input, output) {

      getAFYP_round <- reactive({
        set.seed=12345
        options(scipen = 999)
        AA<-runif(10000, input$num1, input$num2)
        BB<-runif(10000, input$num3, input$num4)
        CC<-runif(10000, input$num5, input$num6)
        DD<-runif(10000, input$num7, input$num8)

        AFYP <- AA*BB*CC*DD

        AFYP_round=round(AFYP, digits = -7)
        AFYP_round
      })

      output$distPlot <- renderPlot({
        options(scipen = 999)
        AFYP_round<-getAFYP_round() 
        zz<-as.data.frame(AFYP_round)
        Freqa=as.data.frame(prop.table(table(AFYP_round)))
        library(dplyr)
        Freqa=Freqa %>% mutate(cumsum = cumsum(Freq))
        library(ggplot2)
        ggplot(Freqa, aes(x=AFYP_round, y=Freq)) +         geom_bar(stat='identity',col="red", fill="yellow") + labs(title="Histogram for Simulation")+labs(x="AFYP", y="Freq")

      })

      output$mytable <- renderUI({
        options(scipen = 999)
        AFYP<-getAFYP_round()
        c1 <- c("Mean", mean(AFYP))
        c2 <- c("Std Dev", sd(AFYP))
        c3 <- c("Minimum", min(AFYP))
        c4 <- c("Maximum", max(AFYP))
        c5 <- c("Maximum", median(AFYP)) 
        c6 <- c("Q(.25)", quantile(AFYP,0.25)) 
        c7 <- c("Q(.75)", quantile(AFYP,0.75))
        c8 <- c("Q(.05)", quantile(AFYP,0.05))
        c9 <- c("Q(.95)", quantile(AFYP,0.95))
        c10 <- c("Q(.025)", quantile(AFYP,0.025))
        c11 <- c("Q(.975)", quantile(AFYP,0.975))

        Summary=rbind(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11)
        ab<-as.data.frame(Summary)
        names(ab)[names(ab)=="V1"] <- "Measures"
        names(ab)[names(ab)=="25%"] <- "Value"
        output$ac <- renderDataTable(ab,options = list(paging = FALSE,searching = FALSE))
        dataTableOutput("ac")

      })

    })