Shiny with showing different table and data in tab

437 Views Asked by At

I really need some help here!!!Would be greatly appreciated if anyone can help. I am creating a shiny app that showcases some of my analytic results that contains a datafram,a plot, Confusion Matrix result and a Brier Score Result. But I am getting stuck on trying to put everything in together.

I have created a basic shiny panel that the user can set the date limit to look at the data.(That's only for the Win Prob tab). I was hopping it will go away when the user goes to another tab. Here are the data that I have at the moment:

#Data frame that I would like to put in the Win Prob Table Tab
> head(results_from_2017)
  X       Date Week    HomeTeam       AwayTeam HomeScore AwayScore Score p_elo p_glicko p_steph tip_elo tip_glicko tip_steph
1 1 2017-03-23    1     Carlton       Richmond        89       132     0    NA       NA      NA      NA         NA        NA
2 2 2017-03-24    1 Collingwood      Footscray        86       100     0    NA       NA      NA      NA         NA        NA
3 3 2017-03-25    1    St Kilda      Melbourne        90       120     0    NA       NA      NA      NA         NA        NA
4 4 2017-03-25    1      Sydney  Port Adelaide        82       110     0    NA       NA      NA      NA         NA        NA
5 5 2017-03-25    1    Essendon       Hawthorn       116        91     1    NA       NA      NA      NA         NA        NA
6 6 2017-03-25    1  Gold Coast Brisbane Lions        96        98     0    NA       NA      NA      NA         NA        NA

###########
#data of plot, it is an calibration plot (ggplot(calib))
> head(calib)
$data
   calibModelVar            bin  Percent      Lower    Upper Count  midpoint
1          p_elo     [0,0.0909]  0.00000  0.0000000 70.75982     0  4.545455
2          p_elo (0.0909,0.182] 20.00000  4.3312005 48.08911     3 13.636364
3          p_elo  (0.182,0.273] 31.25000 18.6596283 46.25136    15 22.727273
4          p_elo  (0.273,0.364] 41.42857 29.7720961 53.83384    29 31.818182
5          p_elo  (0.364,0.455] 52.08333 41.6435917 62.39108    50 40.909091
6          p_elo  (0.455,0.545] 59.12409 50.4041426 67.44078    81 50.000000
7          p_elo  (0.545,0.636] 62.00000 51.7460694 71.52325    62 59.090909
8          p_elo  (0.636,0.727] 76.82927 66.2047918 85.43612    63 68.181818
9          p_elo  (0.727,0.818] 76.47059 58.8292158 89.25382    26 77.272727
10         p_elo  (0.818,0.909] 91.30435 71.9620664 98.92900    21 86.363636
11         p_elo      (0.909,1]  0.00000         NA       NA     0 95.454545
12      p_glicko     [0,0.0909] 11.11111  0.2809137 48.24965     1  4.545455
13      p_glicko (0.0909,0.182] 27.90698 15.3289233 43.66870    12 13.636364
14      p_glicko  (0.182,0.273] 42.85714 30.4554340 55.95290    27 22.727273
15      p_glicko  (0.273,0.364] 48.33333 35.2312930 61.60540    29 31.818182
16      p_glicko  (0.364,0.455] 43.75000 32.6763953 55.30024    35 40.909091
17      p_glicko  (0.455,0.545] 63.04348 52.3449283 72.87734    58 50.000000
18      p_glicko  (0.545,0.636] 60.52632 48.6495032 71.55668    46 59.090909
19      p_glicko  (0.636,0.727] 74.68354 63.6438278 83.79941    59 68.181818
20      p_glicko  (0.727,0.818] 75.43860 62.2389695 85.87314    43 77.272727
21      p_glicko  (0.818,0.909] 84.09091 69.9346786 93.35565    37 86.363636
22      p_glicko      (0.909,1] 60.00000 14.6632800 94.72550     3 95.454545
23       p_steph     [0,0.0909]  0.00000         NA       NA     0  4.545455
24       p_steph (0.0909,0.182]  0.00000         NA       NA     0 13.636364
25       p_steph  (0.182,0.273] 46.10390 38.0501192 54.31048    71 22.727273
26       p_steph  (0.273,0.364]  0.00000         NA       NA     0 31.818182
27       p_steph  (0.364,0.455]  0.00000         NA       NA     0 40.909091
28       p_steph  (0.455,0.545] 58.18815 52.2473498 63.95810   167 50.000000
29       p_steph  (0.545,0.636]  0.00000         NA       NA     0 59.090909
30       p_steph  (0.636,0.727]  0.00000         NA       NA     0 68.181818
31       p_steph  (0.727,0.818] 67.06587 59.3823743 74.13113   112 77.272727
32       p_steph  (0.818,0.909]  0.00000         NA       NA     0 86.363636
33       p_steph      (0.909,1]  0.00000         NA       NA     0 95.454545

$cuts
[1] 11

$class
[1] "1"

$probNames
[1] "p_elo"    "p_glicko" "p_steph" 

$call
calibration.formula(x = factor(Score, levels = c(1, 0)) ~ p_elo + 
    p_glicko + p_steph, data = results_from_2017[cond, ])

###############################
# Here is the confusion matrix from the model that I build
> head(cm_elo)
$positive
NULL

$table
          Reference
Prediction   0 0.5   1
       0   170   3 127
       0.5   0   0   0
       1    88   1 223

$overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
  0.6421568627   0.2868726855   0.6027469604   0.6801875233   0.5718954248   0.0002330252   0.0113301975 

$byClass
           Sensitivity Specificity Pos Pred Value Neg Pred Value Precision    Recall       F1  Prevalence Detection Rate Detection Prevalence
Class: 0     0.6589147   0.6327684      0.5666667      0.7179487 0.5666667 0.6589147 0.609319 0.421568627      0.2777778            0.4901961
Class: 0.5   0.0000000   1.0000000            NaN      0.9934641        NA 0.0000000       NA 0.006535948      0.0000000            0.0000000
Class: 1     0.6371429   0.6603053      0.7147436      0.5766667 0.7147436 0.6371429 0.673716 0.571895425      0.3643791            0.5098039
           Balanced Accuracy
Class: 0           0.6458415
Class: 0.5         0.5000000
Class: 1           0.6487241

$mode
[1] "sens_spec"

$dots
list()

> head(cm_glicko)
$positive
NULL

$table
          Reference
Prediction   0 0.5   1
       0   166   3 130
       0.5   0   0   0
       1    92   1 220

$overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
   0.630718954    0.263718246    0.591108675    0.669061195    0.571895425    0.001761803    0.014730372 

$byClass
           Sensitivity Specificity Pos Pred Value Neg Pred Value Precision    Recall        F1  Prevalence Detection Rate Detection Prevalence
Class: 0     0.6434109   0.6242938      0.5551839      0.7060703 0.5551839 0.6434109 0.5960503 0.421568627      0.2712418            0.4885621
Class: 0.5   0.0000000   1.0000000            NaN      0.9934641        NA 0.0000000        NA 0.006535948      0.0000000            0.0000000
Class: 1     0.6285714   0.6450382      0.7028754      0.5652174 0.7028754 0.6285714 0.6636501 0.571895425      0.3594771            0.5114379
           Balanced Accuracy
Class: 0           0.6338523
Class: 0.5         0.5000000
Class: 1           0.6368048

$mode
[1] "sens_spec"

$dots
list()

> head(cm_steph)
$positive
NULL

$table
          Reference
Prediction   0 0.5   1
       0   203   4 238
       0.5   0   0   0
       1    55   0 112

$overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
    0.51470588     0.09697741     0.47430337     0.55496587     0.57189542     0.99806573            NaN 

$byClass
           Sensitivity Specificity Pos Pred Value Neg Pred Value Precision    Recall        F1  Prevalence Detection Rate Detection Prevalence
Class: 0     0.7868217   0.3163842      0.4561798      0.6706587 0.4561798 0.7868217 0.5775249 0.421568627      0.3316993            0.7271242
Class: 0.5   0.0000000   1.0000000            NaN      0.9934641        NA 0.0000000        NA 0.006535948      0.0000000            0.0000000
Class: 1     0.3200000   0.7900763      0.6706587      0.4651685 0.6706587 0.3200000 0.4332689 0.571895425      0.1830065            0.2728758
           Balanced Accuracy
Class: 0           0.5516029
Class: 0.5         0.5000000
Class: 1           0.5550382

$mode
[1] "sens_spec"

$dots
list()

######################
#Lastly is just a few Brier Score that I want to show
> head(brier_elo)
[1] 0.2231096
> head(brier_glicko)
[1] 0.2277511
> head(brier_steph)
[1] 0.2511355

I am unsure what function within the shiny package that I should use to show everything I have. But I have created a ui, just the function is unclear.

ui <- fluidPage(
  
  # App title ----
  titlePanel("2019 AFL Win Pro Result"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # br() element to introduce extra vertical spacing ----
      br(),
      
      # Input: Date Range
      dateRangeInput("date", "Date range:",
                     start  = "2017-03-30",
                     end    = "2019-09-28",
                     min    = "2017-03-30",
                     max    = "2019-09-28",
                     format = "yyyy/mm/dd",
                     separator = " - ")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = "tabs",
                  tabPanel("Win Prob Table", tableOutput("plot")),
                  tabPanel("Model Comparison Plot", plotOutput("calib")),
                  tabPanel("Confusion Matrix", textOutput("Matrix")),
                  tabPanel("Brier Score and Accruacy", textOutput("Matrix"))
      )
      
    )
  )
)

And here is what the ui and the plot of my ggplot(calib) look like: enter image description here

UI: enter image description here

Here's the code that works for the first bit that I wanted to show. After that I want the other tab panel to show other results that are not related to the data set which is just simply print out the analytical result that I have.

data <- results_from_2017
# Convert character to Date
data$Date=  as.Date(incidents$Date, format = "%Y-%m-%d")

# Define UI for application that draws a histogram
ui <- fluidPage(
  titlePanel("2019 AFL Win Prob Result"),
  
  mainPanel(
    tabsetPanel(
  
  # App title ----
  tabPanel("2019 AFL Win Pro Result",
  
           dateRangeInput('dateRange',
                          label = 'Filter match by date',
                          start = as.Date('2017-03-23') , end = as.Date('2019-09-30')
           )
  ),
  tabPanel("Model Comparison Plot"),
  tabPanel("Confustion Matrix")
  ),
  dataTableOutput('my_table'),
  
  ),
  
  )

  server <- function(input, output, session) {
    output$my_table  <- renderDataTable({
      # Filter the data
      incidents %>% filter(Date >= input$dateRange[1] & Date <= input$dateRange[2])
    })
  }
  
  shinyApp(ui = ui, server = server)

0

There are 0 best solutions below