簡體   English   中英

重置動作按鈕輸出為閃亮

[英]Reset action button output in shiny

我正在使用R Shiny建立預測模型。

我輸入了該模型的一些變量,例如性別,年齡,身高...。然后我有一個操作按鈕,上面寫着“生成預測”。

按下時,會根據使用輸入變量進行的預測顯示一些文本和圖形。

我還包括一個“重置”操作按鈕。

我希望在按下此按鈕時所有變量都變為原始值(已經起作用),並且“ Generate Prediction”按鈕生成后生成的輸出消失。

我的問題是這個願望的第二部分。

是否可以?按“重置”后如何刪除輸出?

在下面找到我的腳本作為示例(實際腳本更加復雜)。 我希望從“預測結果”中指出的部分在按下復位鍵時消失。

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)

在下面的回答中,我將演示如何實現您的要求。 在我的回答中,我使用了shinyjs包,用於重置輸入值和隱藏/顯示結果。 我無法運行您的代碼,因為有一些我不知道的額外包和函數正在使用,並且代碼本身也不是一個最小的孤立示例,因此我將編寫自己的小型應用程序,該程序執行類似的操作,實現您想要的。 這是代碼:

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)

要具體解決您的兩個問題以及上面的解決方法,請執行以下操作:

  1. 要將輸入重置為其原始值,請使用shinyjs::reset()函數。 與將輸入更新為特定值相比,這是一種更好的方法,因為reset()函數將保證將其重置為原始值,而您的方法意味着,如果您更改UI中的初始值,則必須記得還要在服務器中進行更改。

  2. 為了在按下reset鍵后隱藏結果,我將所有結果UI包裹在div(id = "results", ...) 然后,每當按下“提交”按鈕時,我都會使用Shinyjs來顯示它,當按下重置按鈕時,我會使用Shinyjs來隱藏它。 我也將UI包裹在shinyjs::hidden(...)因為您希望結果以未顯示開始。

以上兩個都需要在UI中調用shinyjs::useShinyjs()

您應該能夠構建此示例,並在更復雜的應用程序中實現這些技術。

還要注意,我上面的示例應用程序所做的其他事情與您的有所不同。 例如,您不應使用output$X <- eventReactive(...) 分配給輸出時,應使用渲染函數(例如renderText() )。

對於有相同問題的人,這也可能是一個解決方案: 重新啟動Shiny Session 這樣,所有輸入和輸出都將重置,但需要更多時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM