【问题标题】:How to Store Monte Carlo Simulation Outputs Within a reactive() Function In Shiny如何在 Shiny 中的 reactive() 函数中存储蒙特卡罗模拟输出
【发布时间】:2017-02-28 20:57:55
【问题描述】:

我一直在从事一个附带项目,其中涉及一个简单的闪亮应用程序,该应用程序允许用户输入棋盘游戏掷骰子的参数,然后让代码使用这些参数预先生成 10,000 次掷骰并显示掷骰的平均值。我有一个基本代码可以成功实现这一点,但我正在努力将其变成一个闪亮的应用程序以供其他人访问。

我面临的问题是,在闪亮代码的服务器部分,我不知道如何将中间结果存储在单个 reactive() 函数中。是否有与重复功能一起使用的本地存储选项?

我使用的代码是:

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("10,000 Roll Simulator"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         numericInput(inputId = "num_tac", label = "TAC", 
                      value =1 , min = 1, max = 20),
         numericInput(inputId = "num_def", label = "DEF", 
                      value =1 , min = 1, max = 10),
         numericInput(inputId = "num_arm", label = "ARM", 
                      value =0 , min = 0, max = 10)
      )
   )
)

server <- function(input, output){
 data()<- reactive({
   One_roll<- function(){dice <- sample(1:6, size = input$num_tac, replace = TRUE)
   return(sum((dice >= input$num_def)-input$num_arm))
   sims<-replicate(10000, One_roll()}
output$stats <- renderPrint({mean(data())})
}
# Run the application 
shinyApp(ui = ui, server = server)

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: r shiny montecarlo


    【解决方案1】:

    您的代码存在一些问题:

    • data()&lt;- 是不允许的。改用data&lt;-,然后用data()调用它

    • 在函数内部使用input$绝对不是正确的传参方式

    这是一个修改后的服务器函数,其中 One_roll 函数在响应式外部定义,但在内部调用,输入作为参数传递:

    server <- function(input, output){
      One_roll<- function(num_tac,num_def,num_arm){
        dice <- sample(1:6, size = num_tac, replace = TRUE)
        sum((dice >= num_def)-num_arm)
      }
      data<- reactive(replicate(10000, One_roll(input$num_tac,input$num_def, input$num_arm )))
      output$stats <- renderPrint(mean(data()))
    }
    

    您还需要在ui 函数中使用textOutput 来调用renderText,例如:

    ui <- fluidPage(
    
      # Application title
      titlePanel("10,000 Roll Simulator"),
    
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
        sidebarPanel(
          numericInput(inputId = "num_tac", label = "TAC", 
                       value =1 , min = 1, max = 20),
          numericInput(inputId = "num_def", label = "DEF", 
                       value =1 , min = 1, max = 10),
          numericInput(inputId = "num_arm", label = "ARM", 
                       value =0 , min = 0, max = 10)
        ), mainPanel = textOutput("stats")
      )
    )
    

    【讨论】:

    • 哇,非常感谢!我没有意识到我需要/可以在反应方程式之前在服务器部分设置方程式。 --谢谢!
    • @Nathan 您也可以在使用它之前定义函数,但是每次变量更改时都会创建它,并且您不能在服务器函数的其他部分使用它
    【解决方案2】:

    您也可以先将所有用户输入的数据保存到一个静态变量中,然后将它们用作普通变量。

    server <- function(input, output) {
    
    temp <- reactive({
    
      tac <- input$num_tac
    
      def <- input$num_def
    
      arm <- input$num_arm
    
      One_roll <- function(tac, def, arm) {
    
        dice <- sample(1:6, size = tac, replace = TRUE)
    
        sum((dice >= def) - arm)
    
      }
    
      data <- replicate(10000, One_roll(tac, def, arm))
    
      #remember to print the data again so the results will be saved to temp
    
      data
    
    })
    
    output$stats <- renderPrint({
    
      mean(temp())
    
    })
    
    }
    

    【讨论】:

    • 对于不熟悉函数的用户来说,使用相同的变量名来存储输入和函数定义中的参数名可能会有点混乱。
    猜你喜欢
    • 1970-01-01
    • 2012-04-26
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多