htmlrshinyshinyalert

How to send user input HTML to R function


I've some shiny app with button ("btn"), when I click on it I get shintalert:

observeEvent(input[["btn"]], {
    shinyalert(
      title = "some title",
      type = "info",
      showConfirmButton = FALSE,   
      html = TRUE,
      text = div(HTML(
             "<form>                                                 
             <textarea id='my_txt' name='my_txt' rows='8' cols='100'></textarea>
             <input type='text' id='my_txt2' name='my_txt2'  style='display: block;'/>
             <button onclick= ",SOME_R_FUNCTION(my_txt,my_txt2 ),">click me</button>
             </form>"
             ))
             )})
     

In the form I've 2 text boxes and one button. When the user click on the button I want to send the user input of both text-boxes to my R function - SOME_R_FUNCTION. I cant find how I can do it.


Solution

  • library(shiny)
    library(shinyalert)
    
    ui <- basicPage(
      useShinyalert(),
      actionButton("btn", "Click me")
    )
    
    server <- function(input, output){
      observeEvent(input[["btn"]], {
        shinyalert(
          title = "some title",
          type = "info",
          showConfirmButton = FALSE,   
          html = TRUE,
          text = "<div>
              <textarea id='my_txt' name='my_txt' rows='8' cols='100'></textarea>
              <input type='text' id='my_txt2' name='my_txt2' style='display: block;'/>
              <button>click me</button>
            </div>",
          callbackJS = "function(){Shiny.setInputValue('userInputs', [$('#my_txt').val(), $('#my_txt2').val()]);}"
        )
      })
      
      observeEvent(input[["userInputs"]], {
        # apply your R function here
        print(input[["userInputs"]])
      })
      
    }
    
    shinyApp(ui, server)