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
1 Answer
Reset to default 2You 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
版权声明:本文标题:r - How to reset value of numericInput when user clicks Default radio Button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744687405a2619786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论