How to capture shinyalert input field as variable

可紊 提交于 2019-12-07 05:04:25

问题


I'm trying to use the relatively new shinyAlert package to see if it offers better results than the sweetalert package but I'm unable to figure out how to get this:

Myvar <-  shinyalert input text 

from this minimal example.

library(shiny)
library(shinyjs)
library(shinyalert)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  useShinyalert(),
     actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {
  shinyEnv <- environment()

  observeEvent(input$run, {

    shinyalert('hello', type='input')
  })
}
shinyApp(ui = ui, server = server)

My thanks for any help from you geniouses out there.


回答1:


Here is how you do it:

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert(),
  actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {

  observeEvent(input$run, {
    shinyalert('hello', type='input', callbackR = mycallback)
  })

  mycallback <- function(value) {
    cat(value)
  }
}
shinyApp(ui = ui, server = server)

It's done using callbacks. You can assign the value to a reactive variable if you'd like.

I had fully documented the package last month and was about to release it, and then my computer crashed before I had a chance to push to github, and lost all progress. I haven't had a chance to work on it again. So sorry that the documentation isn't great yet, the package is still un-released so use at your own risk for now :)

(Notice that you don't need shinyjs for this)




回答2:


I have no experience with the package shinyalert, but you can achieve what you want with the widely used and well documented modal dialogs from shiny. Maybe you there is a reason for you to stick with shinyalert that I am unaware off, but if not, example code for achieving what you want with modal dialogs:

ui <- fluidPage(
  shinyjs::useShinyjs(),
  actionButton("run", "Run", class = "btn-success"),
  textOutput("output1")
)
server <- function(input, output, session) {

  dataModal <- function(failed = FALSE) {
    modalDialog(
      textInput("input1", "Enter text:",
                placeholder = 'Enter text here'
      )
    )
  }

  # Show modal when button is clicked.
  observeEvent(input$run, {
    showModal(dataModal())
  })

  output$output1 <- renderText({
    input$input1
  })
}
shinyApp(ui = ui, server = server)

Let me know if this helps!



来源:https://stackoverflow.com/questions/45129355/how-to-capture-shinyalert-input-field-as-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!