From 096c4934f62e62eb8b63221866bac3b22216a6c7 Mon Sep 17 00:00:00 2001 From: Shobha Mourya <39210012+shobha-nosimpler@users.noreply.github.com> Date: Fri, 7 Jan 2022 16:32:12 +0530 Subject: [PATCH] Update app.R Modified the server code to create datasetInput using eventReactive() function that creates a data frame for predictor variables to be updated in the event of the hitting the submit button; as such there is not need to create a csv file for test dataset. The reactive code in both, the text box and table also get triggered upon event. --- shiny/003-play-golf/app.R | 80 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/shiny/003-play-golf/app.R b/shiny/003-play-golf/app.R index 3715ade..3981eac 100644 --- a/shiny/003-play-golf/app.R +++ b/shiny/003-play-golf/app.R @@ -65,55 +65,51 @@ ui <- fluidPage(theme = shinytheme("united"), # Server # #################################### -server <- function(input, output, session) { - - # Input Data - datasetInput <- reactive({ - - # outlook,temperature,humidity,windy,play - df <- data.frame( - Name = c("outlook", - "temperature", - "humidity", - "windy"), - Value = as.character(c(input$outlook, - input$temperature, - input$humidity, - input$windy)), - stringsAsFactors = FALSE) - - play <- "play" - df <- rbind(df, play) - input <- transpose(df) - write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE) - - test <- read.csv(paste("input", ".csv", sep=""), header = TRUE) - - test$outlook <- factor(test$outlook, levels = c("overcast", "rainy", "sunny")) - - - Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3)) - print(Output) +server <- function(input, output, session){ + # Using event reactive function to update the predictors used in the model upon the hitting the submit button + + datasetInput <- eventReactive( input$submit,{ + + # create dataframe structure from weather dataframe + rvtest.df = weather[0,1:4] + + rvtest.df[1,1] = factor(input$outlook, levels = c("sunny", "rainy", "overcast")) + rvtest.df[1,2] = input$temperature + rvtest.df[1,3] = input$humidity + rvtest.df[1,4] = ifelse(input$windy == "TRUE", T, F) + + + " Note: dataframe with updated input values for the predictors will be used in the prediction model, hence we +skip the code below that writes to csv file to build the test dataset " + + # write the inputs to csv file + # write.table(rvtest.df[,1:4], paste0(path,"/csv/ui_inputs.csv"), sep=",", + # quote = FALSE, row.names = FALSE, col.names = TRUE) + + # Append the csv file without headers + #write.table(rvtest.df[,1:4], append = TRUE, paste0(path,"/csv/ui_inputs.csv"), sep=",", + # quote = FALSE, row.names = FALSE, col.names = FALSE) + + + Output <- data.frame(Prediction=predict(model,rvtest.df), + round(predict(model,rvtest.df,type="prob"), 3)) + }) - # Status/Output Text Box - output$contents <- renderPrint({ - if (input$submitbutton>0) { - isolate("Calculation complete.") - } else { - return("Server is ready for calculation.") - } + # Render output for Prediction results table + output$tabledata <- renderTable({ + datasetInput() }) - # Prediction results table - output$tabledata <- renderTable({ - if (input$submitbutton>0) { - isolate(datasetInput()) - } + # Render output for Status/Output Text Box + output$contents <- renderPrint({ + ifelse(!input$submitbutton,"Server is ready for calculation.","Calculation complete." ) }) -} +} # server + + #################################### # Create the shiny app #