0

I have a series of textAreaInput that each require a friendly reminder below the Next button to fill in the field. (In reality, each field and button is on a separate tab.) I use renderText to output the reminder below each button. I can copy and paste the renderText functions for each textOutput, but I have more than two so I feel like I'm violating a golden rule.

Purrr seems like an appropriate tool to use. I suspect the solution will be similar to here and here but I have been unable to apply those solutions to my problem.

How can I apply the same renderText function to each textOutput? I prefer purrr but I appreciate learning alternative solutions.

library(shiny)
library(purrr)

ui <- fluidPage(
  fluidRow(
    column(3,
      textAreaInput(inputId = "ta1", 
                    label = "Fill in this field."),
      actionButton(inputId = "btn_next_ta1", label = "Next"),
      textOutput("ta1_error")
      ),
    column(3,
      textAreaInput(inputId = "ta2",
                    label = "Fill in this field."),
      actionButton(inputId = "btn_next_ta2", label = "Next"),
      textOutput("ta2_error")
    ),
  )
)

server <- function(input, output) {
  
  # Is this even close to a suitable start?
  # walk(c("ta1", "ta2"), ~ observeEvent(input[[.x]], error_check(.x)))
                                              
  error_check <- function(x) {
    # Need to render the text string and assign
    # to each textOutput
    # if (x == "") {
    #   renderText("Please fill in the field."}
  }
  
  # ERROR CHECKS that I want to replace 
  # with a single function.
  output$ta1_error <- renderText({
    if (input$ta1 == "") {
      "Please fill in the field."
    }
  })
  
  output$ta2_error <- renderText({
    if (input$ta2 == "") {
      "Please fill in the field."
    }
  })
}

shinyApp(ui = ui, server = server)

Created on 2021-11-04 by the reprex package (v2.0.1)

Michael S Taylor
  • 425
  • 5
  • 16
  • This is not directly related to your code but you may want to check the [shinyFeedback](https://merlinoa.github.io/shinyFeedback/articles/shinyFeedback-intro.html) package, whose purpose is precisely to display messages according to user's inputs. – bretauv Nov 06 '21 at 19:23
  • Thank you for the suggestion @bretauv. I know the package name through the [User Feedback](https://mastering-shiny.org/action-feedback.html) chapter of Mastering Shiny but I haven't tried it yet. – Michael S Taylor Nov 09 '21 at 13:17

0 Answers0