admin管理员组

文章数量:1389863

In my app, the user can choose to apply either a Default or Custom Value. If the user selects 'Custom', the numericInput boxes are enabled and can be changed. If the user goes back and selects 'Default', I would like the values in the numericInput boxes to be reset to their default values. I would like to do this without having to add a separate action button if this is possible.

library(shiny)
library(shinydashboard)
library(shinyjs)

opts.adv <- c("Default","Custom")

ui <- dashboardPage(skin = "blue",
  dashboardHeader(title = "Survival Analysis",titleWidth = 450),
  dashboardSidebar(id="",
             sidebarMenu(id = "tabs",
                 menuItem("Survival",tabName = "menuSurvival", icon = shiny::icon("skull-crossbones")),
                 actionButton("btnQuit","Quit",icon = shiny::icon("xmark"),class="btn-lg btn-danger"))),
  dashboardBody(
    useShinyjs(),
     tabItems(
         tabItem(tabName = "menuSurvival",
                    fluidRow(column(4,style="width:25%;",
                                    box(title="Survival",status="primary",solidHeader=TRUE,width=NULL,
                                        radioButtons("adv.sel","Survival options",opts.adv),
                                        numericInput("mu.custom","Mean (default = 0.1)",0.1,min=0,max=2,step=0.01),
                                        numericInput("var.custom","Variance (default = 0.05)",0.05,min=0,max=0.5,step=0.01)))))
)))

server <- function(input, output, session) {

  observeEvent(input$adv.sel, {
    if (input$adv.sel == "Default") {
      shinyjs::disable(id = "mu.custom")
      shinyjs::disable(id = "var.custom")
    } else {
      shinyjs::enable(id = "mu.custom")
      shinyjs::enable(id = "var.custom")
    }
  })

  observeEvent(input$btnQuit, {
    stopApp()
  })

}

shinyApp(ui, server)enter code here

In my app, the user can choose to apply either a Default or Custom Value. If the user selects 'Custom', the numericInput boxes are enabled and can be changed. If the user goes back and selects 'Default', I would like the values in the numericInput boxes to be reset to their default values. I would like to do this without having to add a separate action button if this is possible.

library(shiny)
library(shinydashboard)
library(shinyjs)

opts.adv <- c("Default","Custom")

ui <- dashboardPage(skin = "blue",
  dashboardHeader(title = "Survival Analysis",titleWidth = 450),
  dashboardSidebar(id="",
             sidebarMenu(id = "tabs",
                 menuItem("Survival",tabName = "menuSurvival", icon = shiny::icon("skull-crossbones")),
                 actionButton("btnQuit","Quit",icon = shiny::icon("xmark"),class="btn-lg btn-danger"))),
  dashboardBody(
    useShinyjs(),
     tabItems(
         tabItem(tabName = "menuSurvival",
                    fluidRow(column(4,style="width:25%;",
                                    box(title="Survival",status="primary",solidHeader=TRUE,width=NULL,
                                        radioButtons("adv.sel","Survival options",opts.adv),
                                        numericInput("mu.custom","Mean (default = 0.1)",0.1,min=0,max=2,step=0.01),
                                        numericInput("var.custom","Variance (default = 0.05)",0.05,min=0,max=0.5,step=0.01)))))
)))

server <- function(input, output, session) {

  observeEvent(input$adv.sel, {
    if (input$adv.sel == "Default") {
      shinyjs::disable(id = "mu.custom")
      shinyjs::disable(id = "var.custom")
    } else {
      shinyjs::enable(id = "mu.custom")
      shinyjs::enable(id = "var.custom")
    }
  })

  observeEvent(input$btnQuit, {
    stopApp()
  })

}

shinyApp(ui, server)enter code here
Share Improve this question asked Mar 13 at 17:36 LauraLaura 1371 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can simply add reset

library(shiny)
library(shinydashboard)
library(shinyjs)

opts.adv <- c("Default","Custom")

ui <- dashboardPage(skin = "blue",
                    dashboardHeader(title = "Survival Analysis",titleWidth = 450),
                    dashboardSidebar(id="",
                                     sidebarMenu(id = "tabs",
                                                 menuItem("Survival",tabName = "menuSurvival", icon = shiny::icon("skull-crossbones")),
                                                 actionButton("btnQuit","Quit",icon = shiny::icon("xmark"),class="btn-lg btn-danger"))),
                    dashboardBody(
                      useShinyjs(),
                      tabItems(
                        tabItem(tabName = "menuSurvival",
                                fluidRow(column(4,style="width:25%;",
                                                box(title="Survival",status="primary",solidHeader=TRUE,width=NULL,
                                                    radioButtons("adv.sel","Survival options",opts.adv),
                                                    numericInput("mu.custom","Mean (default = 0.1)",0.1,min=0,max=2,step=0.01),
                                                    numericInput("var.custom","Variance (default = 0.05)",0.05,min=0,max=0.5,step=0.01)))))
                      )))

server <- function(input, output, session) {
  
  observeEvent(input$adv.sel, {
    if (input$adv.sel == "Default") {
      shinyjs::disable(id = "mu.custom")
      shinyjs::disable(id = "var.custom")
      reset(id = "mu.custom", asis = FALSE)
      reset(id = "var.custom", asis = FALSE)
    } else {
      shinyjs::enable(id = "mu.custom")
      shinyjs::enable(id = "var.custom")
    }
  })
  
  observeEvent(input$btnQuit, {
    stopApp()
  })
  
}

shinyApp(ui, server)

Or updateNumericInput to update both inputs to default everytime the user clicks back on default.

library(shiny)
library(shinydashboard)
library(shinyjs)

opts.adv <- c("Default","Custom")

ui <- dashboardPage(skin = "blue",
                    dashboardHeader(title = "Survival Analysis",titleWidth = 450),
                    dashboardSidebar(id="",
                                     sidebarMenu(id = "tabs",
                                                 menuItem("Survival",tabName = "menuSurvival", icon = shiny::icon("skull-crossbones")),
                                                 actionButton("btnQuit","Quit",icon = shiny::icon("xmark"),class="btn-lg btn-danger"))),
                    dashboardBody(
                      useShinyjs(),
                      tabItems(
                        tabItem(tabName = "menuSurvival",
                                fluidRow(column(4,style="width:25%;",
                                                box(title="Survival",status="primary",solidHeader=TRUE,width=NULL,
                                                    radioButtons("adv.sel","Survival options",opts.adv),
                                                    numericInput("mu.custom","Mean (default = 0.1)",0.1,min=0,max=2,step=0.01),
                                                    numericInput("var.custom","Variance (default = 0.05)",0.05,min=0,max=0.5,step=0.01)))))
                      )))

server <- function(input, output, session) {
  
  observeEvent(input$adv.sel, {
    if (input$adv.sel == "Default") {
      shinyjs::disable(id = "mu.custom")
      shinyjs::disable(id = "var.custom")
      
      updateNumericInput(session, "mu.custom", value = 0.1)
      updateNumericInput(session, "var.custom", value = 0.05)
    } else {
      shinyjs::enable(id = "mu.custom")
      shinyjs::enable(id = "var.custom")
    }
  })
  
  observeEvent(input$btnQuit, {
    stopApp()
  })
  
}

shinyApp(ui, server)

and both will give

本文标签: rHow to reset value of numericInput when user clicks Default radio ButtonStack Overflow