简体   繁体   中英

How to create a popup window for user to input information in R shiny?

Here is an example. What I want is that users can run the demo as many time as they want by DEMO button. However when they click Browse for uploading local data (not reset button as I demonstrated in the example), I would popup a window to let users input their name and state in two input boxes. In the below example, by click RESET , a single popup box will launch (may be not a proper way).

library(shiny)
library(shinyWidgets)
if (interactive()) {
  # Display an important message that can be dismissed only by clicking the
  # dismiss button.
  shinyApp(
    ui <- fluidPage(
      tabsetPanel(
        ##tabPanel-Input
        tabPanel("Input", fluid = TRUE,

                 # tab title ----
                 titlePanel("Upload data"),

                 # sidebar layout with input and output tables ----
                 sidebarLayout(

                   # sidebar panel for inputs ----
                   sidebarPanel(
                     #show ct demo
                     actionBttn("runexample", "DEMO", style="simple", size="sm", color = "primary"),

                     # input1: Select a file ----
                     fileInput("file1", "Count matrix File (.xlsx)",
                               multiple = TRUE,
                               accept = c("text/csv",
                                          "text/comma-separated-values,text/plain",
                                          ".csv")),
                     #action run
                     actionBttn("runbutton", "GO", style="simple", size="sm", color = "primary"),

                     actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
                     verbatimTextOutput(outputId = "reset"),
                   ),
                   # Main panel for displaying outputs ----
                   mainPanel(
                     # Output: Data file ----
                     span(textOutput("nrows"),style="color:blue"),
                     span(textOutput("ncols"),style="color:blue"),
                     tableOutput("matrix"),
                   )
                 )
        )
      )

    ),
    server = function(input, output, session) {
      ###display demo count matrix
      observeEvent(input$runexample, {
        #ngenes
        output$nrows <- renderText({paste("Number of genes: ", dim(mtcars)[1], " [First 10 rows displayed]")})
        #nsamples
        output$ncols<- renderText({paste("Number of genes: ", (dim(mtcars)[2]), " [First 10 rows displayed]")})
        #display 10rows count matrix
        output$matrix <- renderTable({
          mtcars
        })
      }
      )
      observeEvent(input$reset, {
        inputSweetAlert(
          session = session, inputId = "mytext", input = "text",
          title = "This is a free program, please leave your email:"
        )
      })
      output$text <- renderPrint(input$mytext)
    }
  )
}

Here is an example using the modal dialog option provided by shiny . I trimmed your example down to the bits that mattered:

library(shiny)
if (interactive()) {
  shinyApp(
    ui <- fluidPage(
      actionButton("reset", "RESET", style="simple", size="sm", color = "warning"),
      verbatimTextOutput(outputId = "text")
    ),
    server = function(input, output, session) {
      l <- reactiveValues()
      observeEvent(input$reset, {
        # display a modal dialog with a header, textinput and action buttons
        showModal(modalDialog(
          tags$h2('Please enter your personal information'),
          textInput('name', 'Name'),
          textInput('state', 'State'),
          footer=tagList(
            actionButton('submit', 'Submit'),
            modalButton('cancel')
          )
        ))
      })
      
      # only store the information if the user clicks submit
      observeEvent(input$submit, {
        removeModal()
        l$name <- input$name
        l$state <- input$state
      })
      
      # display whatever is listed in l
      output$text <- renderPrint({
        if (is.null(l$name)) return(NULL)
        paste('Name:', l$name, 'and state:', l$state)
      })
    }
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM