neves
neves

Reputation: 846

How do I save input values inside shinyglide?

I have this code:

library(shiny)
library(shinyglide)

ui <- fixedPage(
  
  titlePanel("shinyglide modal example"),
  
  actionButton(
    inputId = "actionlatex1",
    label = "click"
  ),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("mean", "Mean", value = 0),
      numericInput("sd", "Standard deviation", value = 1, min = 0),
      numericInput("n", "n", value = 100, min = 1),
      p(
        tags$a(
          href = "https://github.com/juba/shinyglide/blob/master/inst/examples/03_modal/app.R",
          "Source code on GitHub"
        )
      )
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
  
)

server <- function(input, output, session) {
    
    modal_controls <- glideControls(
      list(
        prevButton(),
        firstButton(
          class = "btn btn-danger",
          `data-dismiss` = "modal",
          "No, thanks!"
        )
      ),
      list(
        nextButton(),
        lastButton(
          class = "btn btn-success",
          `id` = "save",
          `data-dismiss` = "modal",
          "Done"
        )
      )
    )
  
  observeEvent(input$actionlatex1, {
    
    showModal(
      
      modalDialog(
        
        title = "Startup assistant",
        easyClose = FALSE,
        footer = NULL,
        
        glide(
          
          custom_controls = modal_controls,
          
          screen(
            next_label = 'Yes, please ! <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>',
            p("Let's initialize some values, would you ?")
          ),
          
          screen(
            p("First, please select a mean value"),
            numericInput("mean_modal", "Mean", value = 0)
          ),
          
          screen(
            p("Next, please select a standard deviation value"),
            numericInput("sd_modal", "Standard deviation", value = 1, min = 0)
          ),
          
          screen(
            p("Thanks, we're all set !")
          )
        
        )
          
      )
      
    )
    
  })
  
  output$plot <- renderPlot({
    hist(rnorm(req(input$n), req(input$mean), req(input$sd)),
         main = "Such wow",
         xlab = "Wonderful x values",
         ylab = "Incredible frequencies")
  })
  
  observe({
    updateNumericInput(session, "mean_modal", value = input$mean)
    updateNumericInput(session, "sd_modal", value = input$sd)
  })
  
}

shinyApp(ui, server)
 

I would like to store the values of the inputs (mean, sd and n) so that when I reopen and close the modal the values remain there when I modify them.

I tried using a javascript but it got confusing, so I decided to remove it from the current code. I also tried to create a modal with jquery, but I didn't succeed either. I need to save the R inputs when closing and reopening the modal.

Upvotes: 1

Views: 40

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33510

You can use reactiveValues to initialize the inputs once the modal is shown:

library(shiny)
library(shinyglide)

ui <- fixedPage(
  
  titlePanel("shinyglide modal example"),
  
  actionButton(
    inputId = "actionlatex1",
    label = "click"
  ),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("mean", "Mean", value = 0),
      numericInput("sd", "Standard deviation", value = 1, min = 0),
      numericInput("n", "n", value = 100, min = 1),
      p(
        tags$a(
          href = "https://github.com/juba/shinyglide/blob/master/inst/examples/03_modal/app.R",
          "Source code on GitHub"
        )
      )
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
  
)

server <- function(input, output, session) {
  
  modal_controls <- glideControls(
    list(
      prevButton(),
      firstButton(
        class = "btn btn-danger",
        `data-dismiss` = "modal",
        "No, thanks!"
      )
    ),
    list(
      nextButton(),
      lastButton(
        class = "btn btn-success",
        `id` = "save",
        `data-dismiss` = "modal",
        "Done"
      )
    )
  )
  
  modal_inputs <- reactiveValues(mean_modal = 0L, sd_modal = 1L)
  
  observe({
      lapply(names(modal_inputs), function(x){
        if(!is.null(input[[x]])){
          modal_inputs[[x]] <- input[[x]]
        }
      })
  })
  
  observeEvent(input$actionlatex1, {
    
    showModal(
      
      modalDialog(
        
        title = "Startup assistant",
        easyClose = FALSE,
        footer = NULL,
        
        glide(
          
          custom_controls = modal_controls,
          
          screen(
            next_label = 'Yes, please ! <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>',
            p("Let's initialize some values, would you ?")
          ),
          
          screen(
            p("First, please select a mean value"),
            numericInput("mean_modal", "Mean", value = modal_inputs$mean_modal)
          ),
          
          screen(
            p("Next, please select a standard deviation value"),
            numericInput("sd_modal", "Standard deviation", value = modal_inputs$sd_modal, min = 0)
          ),
          
          screen(
            p("Thanks, we're all set !")
          )
          
        )
        
      )
      
    )
    
  })
  
  output$plot <- renderPlot({
    hist(rnorm(req(input$n), req(input$mean), req(input$sd)),
         main = "Such wow",
         xlab = "Wonderful x values",
         ylab = "Incredible frequencies")
  })
  
  observe({
    updateNumericInput(session, "mean_modal", value = input$mean)
    updateNumericInput(session, "sd_modal", value = input$sd)
  })
  
}

shinyApp(ui, server)

Upvotes: 1

Related Questions