从闪亮的用户定义函数返回无功输出

时间:2018-04-20 19:55:29

标签: r shiny user-defined-functions

我相对较新。几个星期前我创建了一个NBA胜利概率模型,并且一直在尝试创建一个闪亮的应用程序,它将从我的模型生成输出,我已经创建了一个用户定义的函数。

在我的用户界面中,我想要一个地方为" Home Points"," Away Points"和" Time Remaining"输入数字输入值。一旦为这些值输入了值,我希望用户单击一个操作按钮。单击操作按钮后,我只想让应用程序在主面板中显示我的功能输出。但是,我无法弄清楚如何让它发挥作用。

这是我的代码:

library(shiny)

# Define UI for application that calculates win probability
ui <- fluidPage(

# Application title
titlePanel("Win Probability"),

# Sidebar layout with inputs and output definitions
sidebarLayout(

 #sidebar panel for inputs
  sidebarPanel(

    #Add numeric input for home team points
    numericInput(inputId = "home.pts", label = h3("Home Points"), value = 0),

    #Add numeric input for away team points
    numericInput(inputId = "away.pts", label = h3("Away Points"), value = 0),

    #Add numeric input for time remaining in fourth quarter
    numericInput(inputId = "time", label = h3("Time Remaining"), value = 0),

    #Add action buttion
    actionButton("goButton","Apply")),

  # Show output
  mainPanel(
     verbatimTextOutput("win_prob")
  )))

win_prob <- function(time, home.pts, away.pts) {
  #calculate point difference
  diff <- home.pts - away.pts
  #Store intercept and betas
  intercept <- 0.09564935
  b_time <- 0.01087832
  b_diff <- 0.5243092
  b_interact <- -0.03066952
  #calculate and store logit
  logit <- intercept + (time*b_time) + (diff*b_diff) + 
  ((time*diff)*b_interact)
  #function to change logit to probability
  logit2prob <- function(logit) {
    odds <- exp(logit)
    prob <- odds/(1+odds)
  }
  #Store probability
  prob <- logit2prob(logit)
  prob
}


# Define server to return win probability
server <- function(input, output) {

  #Store reactive values
  home.pts <- reactive({input$home.pts})
  away.pts <- reactive({input$away.pts})
  time <- reactive({input$time})

  output$win_prob <- renderPrint({win_prob(reactive({input$home.pts}), 
reactive({input$away.pts}), reactive({input$time}))})

}


# Run the application 
shinyApp(ui = ui, server = server)

如果有人可以帮助我,我会非常感激!

谢谢!

2 个答案:

答案 0 :(得分:0)

您不需要将所有输入存储在无功值中。他们已经自我更新。当您拥有actionButton时,通过点击按钮触发事件的最佳方式是使用observeEvent。如果我理解得很好,我会像这样重写你的服务器功能:

# Define server to return win probability
server <- function(input, output) {
  observeEvent(input$goButton, {
    output$win_prob <- renderPrint({
      win_prob(input$home.pts, 
               input$away.pts, 
               input$time)
    })
  })
}

答案 1 :(得分:0)

Using reactivity

server <- function(input, output) {

  #Store reactive values
   home.pts <- reactive({input$home.pts})
   away.pts <- reactive({input$away.pts})
   time <- reactive({input$time})
   output$win_prob <- renderPrint({win_prob(home.pts(), away.pts(), time())})

  }

Using ObserveEvent

server <- function(input, output) {
           data <- reactiveValues()

           observeEvent(input$goButton,{
                              data$home.pts <- input$home.pts
                              data$away.pts <- input$away.pts
                              data$time <- input$time

                            })
output$win_prob <- renderPrint({
req(data$home.pts)  #to delay displaying result until user press Apply 
win_prob(data$home.pts,data$away.pts, data$time)})
  }

Now you can see the deference between the two approaches