0
votes
  • This is my first shiny app
  • My code is probably horrible as its the first time i have done anything like this
  • I have spent 3 - 4 days researching and trying to find an answer
  • I have never used this forum before
  • I'm very close to having this complete and this last problem is killing me!! pleeeeassseeee help me!!!

My shiny app (for a football betting database) works fine locally and in a browser. When I upload it to the server I can view it fine but it doesn't load the data or the plot.

So far I have tried (with no success):

  1. adding explicit calls to packages (e.r dplyr::filter instead of just filter)
  2. deleting tabs and parts of the layout/app .
  3. deleting html code .
  4. adding ‘shinyServer’ and input, output, session into the server side program .
  5. moving around the calls to library(shiny) etc .
  6. changing the size of the data available (it doesn’t appear to be a problem with the data source or the size of the data)
  7. reinstalled R, Rstudio and all of the relevant packages
  8. putting the data.csv file into another folder and changing code accordingly
  9. changed the data directory to "/srv/shiny-server/FBC_Database_1819/Data/ResultsDatabase17.csv" at the start of the program
  10. re-installed tools/ devtools
  11. Created a global.R document that has libraries and data frame saved as a RDS variable
  12. changed the data to an .rds file

    • NONE OF THESE WORKED SO FAR... ....

My code is below:

    library(shiny)
    library(DT)
    library(dplyr)
    library(crosstalk)
    library(shinydashboard)

    FBC_Data <- read.csv("Data/ResultsDatabase17.csv", header = TRUE, stringsAsFactors = FALSE)

    Sys.setenv(TZ='GMT')
    Sys.time()

    FBC_Data$Date <- as.Date(FBC_Data$Date, "%d/%m/%Y")
    strftime(FBC_Data$Date, "%Y-%m-%d")
    format(FBC_Data$Date, "%Y-%m-%d")
    FBC_Data$Date <- gsub("0017", "2017", FBC_Data$Date)
    FBC_Data$Date <- gsub("0018", "2018", FBC_Data$Date)
    FBC_Data$Date <- as.Date(FBC_Data$Date)


    #START UI

    ui <- dashboardPage( skin = "yellow",
                 dashboardHeader(title = "THE FBC +2.5 DATABASE", titleWidth = 300),
                 dashboardSidebar(width = 300,
                                  sidebarMenu(
                                    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
                                    menuItem("Profits", tabName = "Profits", icon = icon("money")),




                                    # Select variable for y-axis 
                                    selectInput(inputId = "y", 
                                                label = "Y-axis:",
                                                choices = c("Odds for +2.5 goals" = "oddsovers",
                                                            "Home win odds" = "B365H", 
                                                            "Away win odds" = "B365A", 
                                                            "FBC Goal Rating" = "RATINGSSCORE",
                                                            "Actual Goals in Game" = "TotalGoals",
                                                            "Value" = "Value",
                                                            "Goals Scored per Game" = "Goalsscoredpg",
                                                            "Goals Concede Per Game" = "Goalsconcededpg",
                                                            "Total Goals Home" = "GoalstotalsH",
                                                            "Total Goals Away" = "GoalstotalsA",
                                                            "Over Two goals in games" = "Overtwogoals",
                                                            "+2.5 gls at home" = "OverglsdiffH",
                                                            "+2.5 gls away" = "OverglsdiffA",
                                                            "over three goals in games" = "overthreegls",
                                                            "Clean Sheets Home" = "CleansheetsH",
                                                            "Clean Sheets Away" = "CleansheetsA",
                                                            "Failed to score at home" = "FTSH",
                                                            "Failed to score away" = "FTSA",
                                                            "Failed to score at HT" = "FTSHT",
                                                            "BTTS percent" = "BTTSpercent",
                                                            "BTTS percent HOME" = "BTTSdiffH",
                                                            "BTTS percent AWAY" = "BTTSdiffa",
                                                            "Goal at HT percent" = "GoalHT",
                                                            "Clean Sheet at HT" = "CSHT",
                                                            "BTTS at HT" = "BTTSHT",
                                                            "Goals Per game Form" =" GoalsPGform",
                                                            "XG game total" = "Xgtotal",            
                                                            "XG week change" = "Xgchange",
                                                            "XGteam" = "Xgfor",
                                                            "XG opponent" ="XGagainst"), 
                                                selected = "RATINGSSCORE"),

                                    # Select variable for x-axis 
                                    selectInput(inputId = "x", 
                                                label = "X-axis:",
                                                choices = c("Odds for +2.5 goals" = "oddsovers",
                                                            "Home win odds" = "B365H", 
                                                            "Away win odds" = "B365A", 
                                                            "FBC Goal Rating" = "RATINGSSCORE",
                                                            "Actual Goals in Game" = "TotalGoals",
                                                            "Value" = "Value",
                                                            "Goals Scored per Game" = "Goalsscoredpg",
                                                            "Goals Concede Per Game" = "Goalsconcededpg",
                                                            "Total Goals Home" = "GoalstotalsH",
                                                            "Total Goals Away" = "GoalstotalsA",
                                                            "Over Two goals in games" = "Overtwogoals",
                                                            "+2.5 gls at home" = "OverglsdiffH",
                                                            "+2.5 gls away" = "OverglsdiffA",
                                                            "over three goals in games" = "overthreegls",
                                                            "Clean Sheets Home" = "CleansheetsH",
                                                            "Clean Sheets Away" = "CleansheetsA",
                                                            "Failed to score at home" = "FTSH",
                                                            "Failed to score away" = "FTSA",
                                                            "Failed to score at HT" = "FTSHT",
                                                            "BTTS percent" = "BTTSpercent",
                                                            "BTTS percent HOME" = "BTTSdiffH",
                                                            "BTTS percent AWAY" = "BTTSdiffa",
                                                            "Goal at HT percent" = "GoalHT",
                                                            "Clean Sheet at HT" = "CSHT",
                                                            "BTTS at HT" = "BTTSHT",
                                                            "Goals Per game Form" =" GoalsPGform",
                                                            "XG game total" = "Xgtotal",            
                                                            "XG week change" = "Xgchange",
                                                            "XGteam" = "Xgfor",
                                                            "XG opponent" ="XGagainst"), 
                                                selected = "TotalGoals"),

                                    # Select variable for color
                                    selectInput(inputId = "z", 
                                                label = "Color by:",
                                                choices = c("+2.5 odds" = "oddsovers", 
                                                            "Home win odds" = "B365H", 
                                                            "Away win odds" = "B365A", 
                                                            "FBC Goal Game Rating" = "RATINGSSCORE",
                                                            "BTTS" = "BTTS",
                                                            "Actual Goals in Game" = "TotalGoals"),
                                                selected = "+2.5 odds"),


                                    # Add a size numeric input
                                    numericInput("size", "Point size", 1, 1),


                                    img(src = "https://thefootballbetclub.files.wordpress.com/2016/07/02b_fbc_logo_url_neg.jpg", height = "200px")
                                  )
                 ),
                 ##BODY CONTENT - first page
                 dashboardBody(
                   tabItems(
                     # First tab content
                     tabItem(tabName = "dashboard",
                             # Boxes need to be put in a row (or column)
                             #add plot output
                             fluidRow(
                               tabBox(title = "Plots and Data", id = "tabset1", width = 12,
                                      tabPanel(title = "plot of games selected", plotOutput(outputId = "scatterplot")

                                      ),
                                      tabPanel(
                                        title = "Table of Selected Data", dataTableOutput("results")
                                      ),

                                      tabPanel(
                                        title = "Notes about this Database", htmlOutput("notesHTML")
                                      ),
                                      fluidRow(
                                        box(title = "Inputs", id = "inputbox1", width = 4,



                                            checkboxGroupInput("LeagueInput", "League",
                                                               choices = c("Prem" = "E0",
                                                                           "Champ" = "E1",
                                                                           "League1" = "E2",
                                                                           "League2" = "E3",
                                                                           "La Liga" = "SP1",
                                                                           "Bundasliga" = "D1",
                                                                           "Seria A"= "I1"), 
                                                               selected = c("E0", "E0", "E1", "E2", "E3", "SP1", "D1", "I1")),

                                            checkboxGroupInput("HomeAway", "Home or Away Teams",
                                                               choices = c("Home" = "H",
                                                                           "Away" = "A"),
                                                               selected = c("H", "A")),

                                            dateRangeInput("GameDates", "Choose Dates", start = "2017-04-04", end = "2018-05-13",
                                                           min = "2017-04-04", max = "2018-05-13", format = "yyyy-mm-dd",
                                                           startview = "month"),

                                            checkboxGroupInput("Month_select", "Month",
                                                               choices = c("September" = "September",
                                                                           "October" = "October",
                                                                           "November" = "November",
                                                                           "December" = "December",
                                                                           "January" = "January",
                                                                           "February" = "February",
                                                                           "March" = "March",
                                                                           "April" = "April",
                                                                           "May" = "May"),
                                                               selected = c("September", "October", "November", "December", "January", "February", "March", "April", "May")),

                                            sliderInput("CleansheetsH", "Clean Sheets at Home percent", min = 0, max = 1,
                                                        value = c(0, 1)),

                                            sliderInput("CleansheetsA", "Clean Sheets Away percent", min = 0, max = 1,
                                                        value = c(0, 1)),


                                            sliderInput("CSHT", "Kept a CLEAN SHEET by HALF TIME percent", min = 0.1, max = 1,
                                                        value = c(0.1, 1))

                                        ),
                                        box(title = "More Inputs", id = "inputbox2", width = 4,

                                            sliderInput("oddsInput", "B365 +2.5 odds for overs", min = 1, max = 3.2,
                                                        value = c(1, 3.2)),

                                            sliderInput("RATING_SCORE", "FBC Goal Rating", min = 0, max = 82.5,
                                                        value = c(0, 82.5)),

                                            sliderInput("GoalsscoredPG", "Goals Scored Per Game", min = 0, max = 3.5,
                                                        value = c(0, 3.7)),

                                            sliderInput("GoalsconcededPG", "Goals Conceded Per Game", min = 0, max = 3.1,
                                                        value = c(0, 3.1)),

                                            sliderInput("BTTSpercent", "BTTS percent", min = 0, max = 0.9,
                                                        value = c(0, 0.9)),

                                            sliderInput("BTTSHT", "BTTS by HALF TIME percent", min = 0, max = 0.6,
                                                        value = c(0, 0.6)),

                                            sliderInput("GoalTotalsH", "All Goals in matches at Home Per Game", min = 0, max = 5.3,
                                                        value = c(0, 5.3)),

                                            sliderInput("GoalTotalsA", "All Goals in matches Away Per Game", min = 0, max = 4.9,
                                                        value = c(0, 5)),

                                            sliderInput("Overtwogoals", "Over TWO goals per game percentage", min = 0, max = 1,
                                                        value = c(0, 1)),

                                            sliderInput("Overthreegoals", "Over THREE goals per game percentage", min = 0, max = 0.71,
                                                        value = c(0, 0.71))



                                        ),

                                        box(title = "Even More Inputs!", id = "inputbox3", width = 4,

                                            sliderInput("GoalsPGform", "Goals Per Game recent form (last 5 games) ", min = 0.5, max = 6.5,
                                                        value = c(0.5, 6.5)),

                                            sliderInput("Xgtotal", "XG Game Total (only for top Euro Leagues and Prem)", min = 0, max = 3.75,
                                                        value = c(0, 3.75)),

                                            sliderInput("Xgchange", "XG Game Total Difference from last game (only for top Euro Leagues and Prem)", min = -48, max = 49,
                                                        value = c(-48, 49)),

                                            sliderInput("Xgfor", "XG For last game (only for top Euro Leagues and Prem)", min = 0, max = 2.9,
                                                        value = c(0, 2.9)),

                                            sliderInput("XGagainst", "XG Against last game (only for top Euro Leagues and Prem)", min = 0, max = 2.4,
                                                        value = c(0, 2.4)),

                                            sliderInput("FTSH", "Failed to score at HOME percentage", min = 0, max = 1,
                                                        value = c(0, 1)),

                                            sliderInput("FTSA", "Failed to score AWAY percentage", min = 0, max = 1,
                                                        value = c(0, 1)),

                                            sliderInput("FTSHT", "Failed to score by HALF TIME percentage", min = 0, max = 1,
                                                        value = c(0, 1))
                                        )
                                      )

                               )
                             )
                     ),


                     # Second tab content
                     tabItem(tabName = "Profits",
                             h2("System Analysis"),
                             fluidRow(shiny::plotOutput(outputId = "plot"),
                                 actionButton("button", "Show Profit Earned over the period"),
                                 htmlOutput("testHTML")

                             )


                     )
                   )
                 )
)  

    #SERVER

    server <- function(input, output, session) {

    filtered_data <- reactive({
    FBC_Data %>% dplyr::filter(oddsovers >= input$oddsInput[1],
                    oddsovers <= input$oddsInput[2],
                    RATINGSSCORE >= input$RATING_SCORE[1],
                    RATINGSSCORE <= input$RATING_SCORE[2],
                    League %in%(input$LeagueInput),
                    HomeAway %in%(input$HomeAway),
                    Month %in%(input$Month_select),
                    CleansheetsH >= input$CleansheetsH[1],
                    CleansheetsH <= input$CleansheetsH[2],
                    CleansheetsA >= input$CleansheetsA[1],
                    CleansheetsA <= input$CleansheetsA[2],
                    BTTSpercent >= input$BTTSpercent[1],
                    BTTSpercent <= input$BTTSpercent[2],
                    BTTSHT >= input$BTTSHT[1],
                    BTTSHT <= input$BTTSHT[2],
                    CSHT >= input$CSHT[1],
                    CSHT <= input$CSHT[2],
                    Goalsscoredpg >= input$GoalsscoredPG[1],
                    Goalsscoredpg <= input$GoalsscoredPG[2],
                    Goalsconcededpg >= input$GoalsconcededPG[1],
                    Goalsconcededpg <= input$GoalsconcededPG[2],
                    GoalstotalsH >= input$GoalTotalsH[1],
                    GoalstotalsH <= input$GoalTotalsH[2],
                    GoalstotalsA >= input$GoalTotalsA[1],
                    GoalstotalsA <= input$GoalTotalsA[2],
                    Overtwogoals >= input$Overtwogoals[1],
                    Overtwogoals <= input$Overtwogoals[2],
                    overthreegls >= input$Overthreegoals[1],
                    overthreegls <= input$Overthreegoals[2],
                    FTSH >= input$FTSH[1],
                    FTSH <= input$FTSH[2],
                    FTSA >= input$FTSA[1],
                    FTSA <= input$FTSA[2],
                    FTSHT >= input$FTSHT[1],
                    FTSHT <= input$FTSHT[2],
                    GoalsPGform >= input$GoalsPGform[1],
                    GoalsPGform <= input$GoalsPGform[2],
                    Xgtotal >= input$Xgtotal[1],
                    Xgtotal <= input$Xgtotal[2],
                    Xgchange >= input$Xgchange[1],
                    Xgchange <= input$Xgchange[2],
                    Xgfor >= input$Xgfor[1],
                    Xgfor <= input$Xgfor[2],
                    XGagainst >= input$XGagainst[1],
                    XGagainst <= input$XGagainst[2],
                    between(Date,  input$GameDates[1], input$GameDates[2]))
  })


      observeEvent(input$button, {
       filtered_data2 <- filtered_data()
       filtered_data2$cumulative <- ifelse(filtered_data2$Overs == "TRUE", 1, -1)
       filtered_data2$cumulative2 <- ifelse(filtered_data2$cumulative == 1,         filtered_data2$oddsovers-1, -1)
     filtered_data2$cumulative3 <- cumsum(filtered_data2$cumulative2)
     profit_text1 <- tail(filtered_data2$cumulative3, n = 1)
     correct_results <- length(which(filtered_data2$Overs>="TRUE"))
    total_results <- length(which(filtered_data2$Overs>="FALSE"))
     profit_text2 <- round(correct_results / total_results, digits = 3)
winsprem <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("E0"))
winschamp <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("E1"))
winsleague1 <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("E2"))
winsleague2 <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("E3"))
winsspain <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("SP1"))
winsitaly <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("I1"))
winsgermany <- sum(filtered_data2$Overs>="TRUE" & filtered_data2$League %in% c("D1"))
Totalwins <- sum(winsprem, winschamp, winsleague1, winsleague2, winsspain, winsitaly, winsgermany)
winners <- dplyr::filter(filtered_data2, Overs %in% c("TRUE")) 
Return <- sum(winners$oddsovers)
Return
Profit <- Return-total_results
Profit
ROI <- round(Profit/total_results, digits = 2)
output$testHTML <- renderText(paste(" Your total profit for this system, (to £1 stakes),  would have been: ", "<b>", profit_text1, "</b>", "<br>", " It had a ", "<b>", profit_text2, "</b>", "% strike rate.", "<br>", " There were ", "<b>", Totalwins, "</b>", " winning +2.5 bets, out of ", "<b>", total_results, "</b>", "total results.", "<br>", "<b>", " Your ROI would have been ", ROI, " %", "</b>"))
output$plot <- renderPlot({
plot(filtered_data2$cumulative3, type = "l", lwd = "2",  main="System Profit Over Time",
     xlab="Games/Time", ylab="Profit", pch=18, col="blue")})
  })

    #OUTPUTS --

     output$results <- DT::renderDataTable({
     DT::datatable(newdata <- filtered_data())
     newdata[,c(1,2,3,4,5,6,7,8,11:14)]

  })  
      output$"notesHTML" <- renderText(paste(" Welcome .....", "<br>"))

     # GGPLOT
    require(ggplot2)
    require(shiny)

    output$scatterplot <- renderPlot({
    newdata_plot <- filtered_data()
    ggplot2::ggplot(newdata_plot, 
       aes_string(x = input$x, y = input$y, color = input$z)) +
      geom_point(size = input$size)+
      geom_smooth(method = "lm", se=T)
  })

}

shinyApp(ui = ui, server = server)
1
Are you sure it's reading the .csv file in properly? Is it definitely finding and loading the file correctly? You could use shinyjs::logjs() to print thing to the browser console to check that they're loading and being processed properly.divibisan
It would also help make your code easier to read if you indented it properly. In RStudio, you can select all and then go to the menu: Code/Reindent Linesdivibisan
yeah sorry for the bad code indenting, didn't know how to do that code/reindent lines thing, thanks. Also when i put shinyjs::logjs() in the console it says the argument text is missing. No idea what that means but will keep messing aboutGreg Sloan
Did you check that the .csv file is being loaded? shinyjs::logjs() is basically the equivalent of a print() statement, but it prints to the browser console so it works on shiny server. You need to put text or an R object into it like: shinyjs::logjs(head(FBC_Data)). You also need to put useShinyjs(), in the top of your ui function, just inside dashboardPage( for it to workdivibisan
thanks for your help, but i have tried this and still no joyGreg Sloan

1 Answers

0
votes

my problem was to do with the daterange selectors, once i took out the:

                           between(Date,  input$GameDates[1], input$GameDates[2]))

bit of code then it worked ok.

have to work out why that was causing it problems now thanks