library(shiny)
library(shinydashboard)
## 
## 载入程辑包:'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readr)
library(jsonlite)
## 
## 载入程辑包:'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
## The following object is masked from 'package:shiny':
## 
##     validate
library(DT)
## 
## 载入程辑包:'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
library(stringr)
library(memoise)
library(rsconnect)
## Warning: 程辑包'rsconnect'是用R版本4.1.2 来建造的
## 
## 载入程辑包:'rsconnect'
## The following object is masked from 'package:shiny':
## 
##     serverInfo
#Define UI for dataset viewer app ----
  ui <- fluidPage(
    
    # App title ----
    titlePanel("DICE_GAME"),
    
    # Sidebar layout with a input and output definitions ----
    sidebarLayout(
      
      # Sidebar panel for inputs ----
      sidebarPanel(
        
        # Input: Selector for choosing dataset ----
        numericInput(inputId = "num_people",
                    label = "The number of people:",
                    value = 3),
        
        numericInput(inputId = "num_dice_per",
                     label = "Number of dices per person:",
                     value = 6),
        
        numericInput(inputId = "risk",
                     label = "Risk expectation:",
                     value = 0.5),
        textInput(inputId = "my_re",
                     label = "My dice result:",
                     value = "1,2,3,0,0,0"),
        textInput(inputId = "ene_re",
                  label = "The annuncment of previous person:",
                  value = "1,2")
      ),
      
      # Main panel for displaying outputs ----
      mainPanel(
        
        # Output: Verbatim text for data summary ----
        verbatimTextOutput("rmk1",placeholder = FALSE),
        verbatimTextOutput("summary"),
        htmlOutput("rmk2",placeholder = FALSE),
        
      )
    )
  )
  
  multi_n <- function(n_1,p,pro) {
    x=seq(0,n_1*p,by=1)
    
    out=pbinom(x,n_1*p,pro)
    
    return(out)
  }
  
  dice_judge= function(n_p,n_d,my_re,ene_judge,p){
    
    n_1=n_p-1
    output=multi_n(n_1,n_d,1/3)
    judge_int1=ene_judge[2]
    judge_int2=which.max(my_re)
    judge_num1=my_re[1]+my_re[judge_int1]+max(which(output<=p))+1
    judge_num2=my_re[1]+max(my_re)+max(which(output<=p))
    
    out_put=list(output1=c(judge_num1,judge_int1),output2=c(judge_num2,judge_int2))
    
    
    if (out_put[[1]][1]<ene_judge[1] | (out_put[[1]][1]==ene_judge[1] & out_put[[1]][2]<=ene_judge[2])){
      out_put$output1=0
    }
    
    if (out_put[[2]][1]<ene_judge[1] | (out_put[[2]][1]==ene_judge[1] & out_put[[2]][2]<=ene_judge[2])){
      out_put$output2=0
    }
    
    if (out_put$output1[1]==0){
      out_put$output1=NULL
    }
    
    if (out_put$output2[1]==0){
      out_put$output2=NULL
    }
    
    if (purrr::is_empty(out_put)){
      out_put="over"
    }
    
    return(out_put)
  }
  
  
  
  # Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
  
  
  num_peo <- reactive({
    as.integer(input$num_people)
  })
  
  num_di <- reactive({
    as.integer(input$num_dice_per)
  })
  
  my_re <- reactive({
    as.numeric(unlist(strsplit(input$my_re,",")))
  })
  
  ene_re <- reactive({
    as.numeric(unlist(strsplit(input$ene_re,",")))
  })
  
  risk_input <- reactive({
    as.double(input$risk)
  })
  
  
  
  
  #out <- dice_judge(num_peo(),num_di(),my_re(),ene_re(),risk_input())
 
  
  output$rmk1 <- renderPrint({
    "If there are two results, choose one."
    
  })
 
  output$summary <- renderPrint({
    dice_judge(num_peo(),num_di(),my_re(),ene_re(),risk_input())

  })

  output$rmk2 <- renderUI({
    HTML("PS:This decision-making system can’t judge 1, this problem is easy to solve, but I’m too lazy and don’t want to solve it.")
  })
}
  
  
  shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents