Map either numerical values or a character string

I'm trying to create a ShinyApp and hoping I could get some pointers.

I'm trying to get a summary table ("Summary score") to represent either i) the minimum value associated with user input for radio buttons (e.g., Id037_crit1 & Id038_crit1) or the text string "NA" if a checkbox is selected (Id039_crit1).

I'm not sure how to change the code such that the summary table shows either the minimum value for the radio buttons or the character string if the checkbox is selected. I'm assuming there's some kind of if-else statement but I can't get it to work.

library(shinydashboard)
library(shinythemes)
library(shiny)
library(shinyWidgets)
library(DT)
library(tidyverse) 
ui <- fluidPage(
  theme = shinytheme("united"),

  # Application title
  titlePanel("TITLE"),
  sidebarLayout(
    sidebarPanel(
      selectInput("select",
        label = helpText("Select a critera"),
        choices = list("Criteria_1", "Criteria_2"),
        selected = c("NULL")
      )
    ),
    mainPanel(tabsetPanel(
      tabPanel(
        "Criteria", conditionalPanel(h3("Question 1", align = "left"),
          condition = "input.select == 'Criteria_1'",
          prettyRadioButtons(
            inputId = "Id037_crit1",
            label = "Predictions:",
            choices = c(
              "Option 1" = 1,
              "Option 2" = 2,
              "Option 3" = 3
            ),
            inline = TRUE,
            status = "danger",
            fill = TRUE
          ),
        ),
        conditionalPanel(h3("Question 2", align = "left"),
          condition = "input.select == 'Criteria_1'",
          prettyRadioButtons(
            inputId = "Id038_crit1",
            label = "Hypotheses:",
            choices = c(
              "Option 1" = 1,
              "Option 2" = 2,
              "Option 3" = 3
            ),
            inline = TRUE,
            status = "danger",
            fill = TRUE)
        ),
 
conditionalPanel(h3("Or", align = "left"),
                                     condition =  "input.select == 'Criteria_1'",
                                     awesomeCheckbox(
                                       inputId = "Id039_crit1",
                                       label = "NA",
                                       status = "danger")
        ),

        # User side-pannel selection - criteria 2


        conditionalPanel(h3("Question 1", align = "left"),
          condition = "input.select == 'Criteria_2'",
          prettyRadioButtons(
            inputId = "Id040_crit2",
            label = "Methods:",
            choices = c(
              "Option 1" = 1,
              "Option 2" = 2
            ),
            inline = TRUE,
            status = "danger",
            fill = TRUE)),


      # Second Tab --------------------------------------------------------------


      tabPanel(
        "Summary score",
        DTOutput("summary")
      ),
    ))
  )
)

# SERVER ------------------------------------------------------------------


server <- function(input, output) {
  
  calc_min_val <- function(contains) {
    radios_inputid <- str_subset(names(input), contains)
    map_dbl(radios_inputid, ~ as.numeric(input[[.x]])) %>%
      min()
  }
  
  
  summ <- reactive({
    min_values <- c("crit1$", "crit2$") %>%
      map(calc_min_val)
     
    
    
    tibble(
      Lowest_Criteria = c("Specific hypotheses and prediction are provided?", "Predictions regarding the electromagnetic area of 
interest are sufficient?"),
      value = map(min_values, ~.)
    )
  })
  
  
  output$summary <- DT::renderDT({
    datatable(summ())
  })
# Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("DATA", ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasettable(summ()), file, row.names = FALSE)
    }
  )
}
shinyApp(ui, server)

Solution 1:

Perhaps you are looking for this

summ <- reactive({
      min_values <- c("crit1$", "crit2$") %>%
        map(calc_min_val)
      if (input$Id039_crit1 & input$select == 'Criteria_1') value = "NA" else value = map(min_values, ~.)
      
      tibble(
        Lowest_Criteria = c("Specific hypotheses and prediction are provided?", "Predictions regarding the electromagnetic area of 
interest are sufficient?"),
        value = value
      )
    })