简体   繁体   English

重置动作按钮输出为闪亮

[英]Reset action button output in shiny

I am working on a prediction model using R Shiny. 我正在使用R Shiny建立预测模型。

I have as input some variables for the model, like sex, age, height.... I than have a action button saying "Generate Prediction". 我输入了该模型的一些变量,例如性别,年龄,身高...。然后我有一个操作按钮,上面写着“生成预测”。

When pressed, some text and figures appear based on the prediction made with the input variables. 按下时,会根据使用输入变量进行的预测显示一些文本和图形。

I also included a "reset" action button. 我还包括一个“重置”操作按钮。

I want when this button is pressed for all variables to go to original value (that already works) AND that the output generated after the "Generate Prediction" button disappears. 我希望在按下此按钮时所有变量都变为原始值(已经起作用),并且“ Generate Prediction”按钮生成后生成的输出消失。

My problem is with the second part of this wish. 我的问题是这个愿望的第二部分。

Is it possible and how can I remove the output after pressing "reset"? 是否可以?按“重置”后如何删除输出?

Find my script below as an example (the real script is more complex). 在下面找到我的脚本作为示例(实际脚本更加复杂)。 I would like the part stating from "Results of prediction" to disappear when reset is pressed. 我希望从“预测结果”中指出的部分在按下复位键时消失。

library(shiny)

# Define UI ----
ui <- fluidPage(
  titlePanel(title=div( "COPD risk prediction tool")),

  p("Chronic Obstructive Pulmonary Disease (COPD) is a lung problem that can affect people mainly as they get older. One of the main features of COPD is a change in the airways that alters how the air is held in the lungs and the ease with which breathing occurs (the airways become 'obstructed'). This may cause breathlessness, frequent coughing, production of sputum from your chest, and chest infections."),

  selectInput("sex", label=h4("What is your gender?"),
              choices=list("Female"=0, "Male"=1), selected=0),       
  selectInput("age", label=h4("What is your age?"),
              choices=list("18"=18, "19"=19, "20"=20, "21"=21, "22"=22, "23"=23, "24"=24, "25"=25, "26"=26, "27"=27, "28"=28, "29"=29, "30"=30), selected=20),
  bsTooltip("age",
            "What is your current age in years?","right"),
  selectInput("weight", label=h4("What is your weight?"),
              choices=list("50"=50, "51"=51, "52"=52, "53"=53, "54"=54, "55"=55, "56"=56, "57"=57, "58"=58, "59"=59, "60"=60, "61"=61, "62"=62, "63"=63, "64"=64, "65"=65, "66"=66, "67"=67, "68"=68, "69"=69, "70"=70, "71"=71, "72"=72, "73"=73, "74"=74, "75"=75, "76"=76, "77"=77, "78"=78, "79"=79, "80"=80, "81"=81, "82"=82, "83"=83, "84"=84, "85"=85, "86"=86, "87"=87, "88"=88, "89"=89, "90"=90, "91"=91, "92"=92, "93"=93, "94"=94, "95"=95, "96"=96, "97"=97, "98"=98, "99"=99, "100"=100), selected=75),
  bsTooltip("weight", 
            "What is your current weight in kg?", "right"),
  selectInput("height", label=h4("What is your height?"),
              choices=list("140"=140, "141"=141, "142"=142, "143"=143, "144"=144, "145"=145, "146"=146, "147"=147, "148"=148, "149"=149, "150"=150, "151"=151, "152"=152, "153"=153, "154"=154, "155"=155, "156"=156, "157"=157, "158"=158, "159"=159, "160"=160, "161"=161, "162"=162, "163"=163, "164"=164, "165"=165, "166"=166, "167"=167, "168"=168, "169"=169, "170"=170, "171"=171, "172"=172, "173"=173, "174"=174, "175"=175, "176"=176, "177"=177, "178"=178, "179"=179, "180"=180, "181"=181, "182"=182, "183"=183, "184"=184, "185"=185), selected=170),
  bsTooltip("height",
            "What is your current height in cm?", "right"),
  br(),

  h4("Medical Disclaimer", style = "color:blue"),
  p(strong("This risk prediction tool is for general information and should not replace advice from your GP who knows your individual history.", style = "color:blue")),
  p(strong("Although we have included major risk factors, COPD can affect anyone and if you have symptoms or concerns you should speak to your doctor.", style = "color:blue")),
  p(strong("This risk score is derived from Caucasian populations and may not be as accurate for other ethnic groups.", style = "color:blue")),

  actionButton("submit", label = "Generate Prediction"), actionButton("reset", label=("Reset")),

  h2(textOutput('title')),
  h4(textOutput('label1')),
  h5(textOutput('label2')),
  verbatimTextOutput("prediction")
)


# Define server logic ----
server <- function(input, output,session) {

  submit <- FALSE
  output$title <- eventReactive(input$submit, {
    'Results of prediction'
  })
  output$label1 <- eventReactive(input$submit, {
    'COPD risk prediction score'
  })
  output$label2 <- eventReactive(input$submit, {
    'Your predicted risk (%) of developing COPD in your lifetime is:'
  })
  output$prediction <- eventReactive(input$submit, {
  round((copdRisk(weight=input$weight, height=input$height, sex=input$sex)*100), 1)
  })

  output$label5 <- eventReactive(input$submit, {
    'This means that for every 100 people sharing your characteristics '
  })  
  output$label6 <- eventReactive(input$submit, {
    'would develop COPD in their lifetime.'
  })

    observe({
    input$reset
    updateSelectInput(session, "age", selected=20)
    updateSelectInput(session, "weight", selected=75)
    updateSelectInput(session, "height", selected=170)
    updateSelectInput(session, "sex", selected=0)
    #updateActionButton(session, "submit", selected=FALSE)
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)

In my answer below I will demonstrate how to achieve what you're asking for. 在下面的回答中,我将演示如何实现您的要求。 In my answer I use the shinyjs package, both for resetting input values and for hiding/showing the results. 在我的回答中,我使用了shinyjs包,用于重置输入值和隐藏/显示结果。 I cannot run your code because there are extra packages and functions that you are using that I don't know about, and the code itself is not a minimal isolated example, so instead I'll write my own small app that does something similar and achieves what you want. 我无法运行您的代码,因为有一些我不知道的额外包和函数正在使用,并且代码本身也不是一个最小的孤立示例,因此我将编写自己的小型应用程序,该程序执行类似的操作,实现您想要的。 Here is the code: 这是代码:

library(shiny)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  numericInput("num", "Enter a number", 7),
  actionButton("submit", "Square that number!"),
  actionButton("reset", "Reset"),
  shinyjs::hidden(
    div(
      id = "results",
      h3("The square is"),
      textOutput("square")
    )
  )
)

server <- function(input, output, session) {
  output$square <- renderText({
    input$submit
    isolate(input$num * input$num)
  })

  observeEvent(input$reset, {
    shinyjs::reset("num")
    shinyjs::hide("results")
  })

  observeEvent(input$submit, {
    shinyjs::show("results")
  })
}

shinyApp(ui = ui, server = server)

To address your two questions specifically and how they are solved above: 要具体解决您的两个问题以及上面的解决方法,请执行以下操作:

  1. To reset inputs to their original value, I use the shinyjs::reset() function. 要将输入重置为其原始值,请使用shinyjs::reset()函数。 This is a much better approach than updating the inputs to a particular value, because the reset() function will guarantee to reset it to whatever value it was originally, whereas your approach means that if you change the initial value in the UI, you must remember to change it in the server as well. 与将输入更新为特定值相比,这是一种更好的方法,因为reset()函数将保证将其重置为原始值,而您的方法意味着,如果您更改UI中的初始值,则必须记得还要在服务器中进行更改。

  2. To hide the results after pressing reset, I wrapped all the results UI inside a div(id = "results", ...) . 为了在按下reset键后隐藏结果,我将所有结果UI包裹在div(id = "results", ...) Then whenever the submit button is pressed, I use shinyjs to show it, and when reset is pressed I use shinyjs to hide it. 然后,每当按下“提交”按钮时,我都会使用Shinyjs来显示它,当按下重置按钮时,我会使用Shinyjs来隐藏它。 I also wrapped the UI in a shinyjs::hidden(...) because you want the results to start off as not showing. 我也将UI包裹在shinyjs::hidden(...)因为您希望结果以未显示开始。

Both of the above require a call to shinyjs::useShinyjs() in the UI. 以上两个都需要在UI中调用shinyjs::useShinyjs()

You should be able to build off this example and implement these techniques in your more complex app. 您应该能够构建此示例,并在更复杂的应用程序中实现这些技术。

Also note that my sample app above does a few other things differently than yours. 还要注意,我上面的示例应用程序所做的其他事情与您的有所不同。 For example, you should not use output$X <- eventReactive(...) . 例如,您不应使用output$X <- eventReactive(...) You should use the render functions (such as renderText() ) when assigning into outputs. 分配给输出时,应使用渲染函数(例如renderText() )。

For people who have the same problem, this might also be a solution: Restart Shiny Session . 对于有相同问题的人,这也可能是一个解决方案: 重新启动Shiny Session In this way all inputs and output are reset, but it takes more time. 这样,所有输入和输出都将重置,但需要更多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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