picador
picador

Reputation: 67

Make a modal in R shiny that only executes Rscript if you press "OK"

My Rshiny app currently takes in user input and when you press submit it writes these inputs to an external csv file, however in terms of error checking I would like a pop up to appear which states "You have inputted x, y, z, are you happy with this?", and if the user selects OK then (and only then) it runs the code to write to csv. If they press "Cancel", the external csv is unchanged. Any help with packages to do this? I've looked at shinyjs and shinyalert but I can't seem to find out how to do this!

Upvotes: 1

Views: 402

Answers (1)

A. Suliman
A. Suliman

Reputation: 13135

library(shiny)
library(shinyjs)
library(DT)

ui <- fluidPage(
          shinyjs::useShinyjs(),
          sidebarLayout(
               sidebarPanel(
                   numericInput(inputId = "num1", "Numeric 1", value = 10),
                   numericInput(inputId = "num2", "Numeric 2", value = 20),
                   actionButton("go", "GO")
           ),
mainPanel()
  )
)

server <- function(input, output, session) {
       observe({
           #The element will be enabled if the condition evaluates to TRUE
           shinyjs::toggleState("go",condition = isTruthy(input$num1) && isTruthy(input$num2))
         })

v1_r<- reactiveValues()

observeEvent(input$go, {

    v1_r$colm1 <-  input$num1
    v1_r$colm2 <- input$num2

    v1_r$df_check <-  data.frame("Variables"=c("Input one","Input two"),"Values"=c(v1_r$colm1,v1_r$colm2))

    v1_r$review = datatable(rownames=FALSE,selection = "none",options = list(dom='t',pageLength = nrow(v1_r$df_check), autoWidth = FALSE,columnDefs = list(list(width = '20%', targets = c(1)),list(className = 'dt-center',targets=c(1)))),v1_r$df_check)

     showModal(
         modalDialog(
  footer = tagList(modalButton("Update record"),actionButton("click",label="Proceed")),
  strong(em("Inputs Entered")),
  p("Please double check the inputs entered before you proceed"),
  DT::renderDataTable({v1_r$review})
     )
  )
 })


   observeEvent(input$click, {

   #Save to CSV file

    shinyjs::reset("num1");shinyjs::reset("num2")
    removeModal()

  })

 }

 shinyApp(ui,server)

Here how I solve this issue, I hope it helps ;)

Upvotes: 2

Related Questions