I'm getting this error when the app is initially run when loading before the user enters any data. The page loads then closes. The error message is in the RStudio console. I want it to call the function and show the message after the user clicks the Submit button. It looks like the error is on } above the shinyApp(ui, server)
RShiny app.R file:
library(shiny)
library("stringr")
source("testLibrary.R")
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("Churn"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
textInput(inputId = "name",
label = "Name:"),
selectInput("gender", "Choose Gender:",
choices = c("Male", "Female"), selected = "Male"),
numericInput("score", "Rating Score:", 10),
actionButton("update", "Submit")
),
# Main panel for displaying outputs ----
mainPanel(
textOutput("msg")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
n <- eventReactive(input$name)
g <- eventReactive(input$gender)
s <- eventReactive(input$score)
retval <- fxTest(n,g,s)
output$msg <- renderText({
retval
})
}
# Create Shiny app ----
shinyApp(ui, server)
testLibrary.R file:
library(RODBC, warn.conflicts = FALSE)
library(dplyr)
library("stringr")
fxTest <- function(n,g,s) {
if(g == "Male")
{
cn = odbcConnect("MyODBC")
ds <- sqlQuery(cn, "SELECT COUNT([Gender])
FROM [MyDB].[dbo].[BankCustomersChurn]
WHERE [Gender] = 'Male'
AND [Exited] = 1")
ds2 = c("Hi ", n, ". The number of male customers that have left are ", ds, ". You rated this app ",s,".")
ds3 <- paste(ds2, collapse = '')
odbcCloseAll()
return(ds3)
}
if(g == "Female")
{
cn = odbcConnect("MyODBC")
ds <- sqlQuery(cn, "SELECT COUNT([Gender])
FROM [MyDB].[dbo].[BankCustomersChurn]
WHERE [Gender] = 'Female'
AND [Exited] = 1")
ds2 = c("Hi ", n, ". The number of female customers that have left are ", ds, ". You rated this app ",s,".")
ds3 <- paste(ds2, collapse = '')
odbcCloseAll()
return(ds3)
}
}