mtr2021
mtr2021

Reputation: 31

Class Error When Trying To Pass Radio Button Values to Server

I have simplified my example below from the code I am currently working with. I am trying to pass values that a user chooses from a set of radio buttons, to a total score that is conducted. My code was working fine, but now I am getting the following error:

Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'

Stack trace (innermost first): 1: runApp

Error : cannot coerce type 'closure' to vector of type 'character'

It sounds like this is a syntax issue, but I am drawing a blank trying to figure this out.

UI.R

ui <- fluidPage(

  #title header
  titlePanel("This is My Form"),

  fluidRow(
    column(6,
           h3("BUTTON"),
           radioButtons("Button1","My First Button", choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           h3("BUTTON 2 AND 3"),
           radioButtons("Button2","My Second Button", choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           radioButtons("Button3","My Third Button", 
                        choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T),
           br(),
           br()
    ),
    column(6,
           h3("BUTTON 4"),
           radioButtons("Button4","My Fourth Button", 
                        choices = c("Yes" = "y","No" = "n","N/a" = "na"), selected = "na", inline = T)
    )
  ),
  actionButton(inputId = "Submit", label = "Calculate"),
  (br),
  mainPanel(
    h1(textOutput('totals'), align = "center")
  )
)

SERVER.R

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

  button1 <- reactive({ ifelse(input$Button1 == "y", 50, ifelse(input$Button1 == "n", 25, 0)) })
  button2 <- reactive({ ifelse(input$Button2 == "y", 50, ifelse(input$Button2 == "n", 25, 0)) })
  button3 <- reactive({ ifelse(input$Button3 == "y", 50, ifelse(input$Button3 == "n", 25, 0)) })
  button4 <- reactive({ ifelse(input$Button4 == "y", 50, ifelse(input$Button4 == "n", 25, 0)) })

  output$totals <- renderText({

    if (input$Submit == 0)
      return(NULL)
    isolate({

      total <- as.numeric(Button1())+as.numeric(Button2())+as.numeric(Button3())+as.numeric(Button4())

      if (is.na(total)){
        print("Make Selections and Click Submit")
      } else 
        print(total)

    })  
  })
}

GLOBAL.R

library(shiny)
library(rsconnect)

Upvotes: 0

Views: 61

Answers (1)

Fredrik Isaksson
Fredrik Isaksson

Reputation: 101

You indeed have a typo in your UI, (br) -> br()

  actionButton(inputId = "Submit", label = "Calculate"),
  (br),
  mainPanel(
    h1(textOutput('totals'), align = "center")
  )

Should be

  actionButton(inputId = "Submit", label = "Calculate"),
  br(),
  mainPanel(
    h1(textOutput('totals'), align = "center")
  )

Upvotes: 1

Related Questions