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)
  }
  
}

eventReactive() takes two arguments, a 'trigger' as the first argument and then a reactive expression as the second argument. You're getting an error message because you've only passed one argument to each of the eventReactive() calls.

I think maybe you meant to use reactive() instead, but that's unnecessary in this case where you can just rewrite your server as follows:

server <- function(input, output) {
  
  output$msg <- renderText({
    fxTest(n = input$name,
           g = input$gender,
           s = input$score)
  })
  
}

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.