在服务器中使用switchInput

时间:2019-02-04 10:30:07

标签: r shiny

与问题ConditionalPanel with sliderInput based on swithcInput有关:

我正在使用预测工具,并基于一些特征,我将计算风险预测。

由于它将成为一种全球性的工具,我希望人们可以选择以KG或LB来补充体重。

我在KG和LB之间进行了输入切换,基于此开关,人们可以将体重放在KG或LB中(因此不能同时使用两者)。 (默认为KG)

如果开关在LB上,我如何在服务器中将LB的重量转换为KG,如果开关在KG上,如何使用输入$ weightKG的输入?

以下内容无效:

    library(shiny)
    library(shinyWidgets)

     LBtoKG <- function(weightLB){ 
          round(0.45359237*weightLB,0)}

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

      p("Chronic Obstructive Pulmonary Disease (COPD) is a lung problem that can affect people mainly as they get older."),

      selectInput("sex", label=p("What is your gender?"),
                  choices=list("Female"=1, "Male"=0), selected=1), 

      sliderInput("age", label=p("What is your age?"), min = 18, max = 90, value = 35),

      strong("What is your weight?"),
      br(),
      switchInput("switchweight", value = TRUE , onLabel = "kg", offLabel = "lb"),

      conditionalPanel(condition = "input.switchweight == true",
              sliderInput("weightKG", label=NULL, min = 25, max = 200, value = 75, round=0)),
            conditionalPanel(condition = "input.switchweight == false",
              sliderInput("weightLB", label=NULL, min = 55, max = 440, value = 165, round=0))
    )

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

  weight <- eventReactive(input$switchweight, {
  switch(input$switchweight,
          "true" = as.numeric(as.character(input$weightKG)),
           "false" = LBtoKG(as.numeric(as.character(input$weightLB))))
  })

    }

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

3 个答案:

答案 0 :(得分:0)

在这种情况下,我将使用updateSliderInput而不是conditionalPanel: 编辑:引入了与用户选择无关的weightKG。

library(shiny)
library(shinyWidgets)

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

  p("Chronic Obstructive Pulmonary Disease (COPD) is a lung problem that can affect people mainly as they get older."),

  selectInput("sex", label=p("What is your gender?"),
              choices=list("Female"=1, "Male"=0), selected=1), 

  sliderInput("age", label=p("What is your age?"), min = 18, max = 90, value = 35),

  strong("What is your weight?"),
  br(),
  switchInput("switchweight", value = TRUE , onLabel = "kg", offLabel = "lb"),

  sliderInput("weight", label=NULL, min = 25, max = 200, value = 75, round=0)
)

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

  weightKG <- reactiveVal(isolate(input$weight))

  observeEvent(input$weight, {
    if(input$switchweight){
      weightKG(round(input$weight, digits = 0))
    } else {
      weightKG(round(input$weight/2.2046226, digits = 0))
    }
    print(weightKG())
  })

  observeEvent(input$switchweight, {
    if(input$switchweight){
      updateSliderInput(session, "weight", label=NULL, min = 25, max = 200, value = isolate({input$weight/2.2046226}))
    } else {
      updateSliderInput(session, "weight", label=NULL, min = 55, max = 440, value = isolate({input$weight*2.2046226}))
    }
  }, ignoreInit = TRUE)

}

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

答案 1 :(得分:0)

我认为这是您的caretaker.restore(originator); 语句的问题。试试:

        string xpath = "/ Root[1] / vehicleSpecifications[1] / Specifications[1] / General[1] / Average_&amp;Distance[1]";

        try
        {
            XPathExpression.Compile(xpath);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

答案 2 :(得分:-1)

终于找到了它的工作原理!

您的答案组合使我朝着正确的方向前进。

这是一个有效的示例:

library(shiny)
library(shinyWidgets)

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

  p("Chronic Obstructive Pulmonary Disease (COPD) is a lung problem that can affect people mainly as they get older."),

  selectInput("sex", label=p("What is your gender?"),
              choices=list("Female"=1, "Male"=0), selected=1), 

  sliderInput("age", label=p("What is your age?"), min = 18, max = 90, value = 35),

  strong("What is your weight?"),
  br(),
  switchInput("switchweight", value = TRUE , onLabel = "kg", offLabel = "lb"),
    sliderInput("weight", label=NULL, min = 25, max = 200, value = 75, round=0),

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

  p('Your predicted risk (%) of developing COPD in your lifetime is:'),
  verbatimTextOutput("prediction")

)


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

   copdRisk <- function(age, sex, weight) {
    (exp(-5.00   +(-0.004*(as.numeric(as.character(age)))) 
         +(0.40*(as.numeric(as.character(sex))))
         +(0.10*(as.numeric(as.character(weight)))) # in cm!
    ))}


  observeEvent(input$switchweight, {
    if(input$switchweight){
      updateSliderInput(session, "weight", label=NULL, min = 25, max = 200, value = isolate({input$weight/2.2046226}))
    } else {
      updateSliderInput(session, "weight", label=NULL, min = 55, max = 440, value = isolate({input$weight*2.2046226}))
    }
  }, ignoreInit = TRUE)


  prediction <- observe({ 

    ## some coding that will convert LB weight in to KG when switch is on LB
    weight <- 
      if(as.character(input$switchweight) == TRUE) {input$weight
      } else {round(input$weight/2.2046226)}


    risks <- (copdRisk(age = input$age, sex=input$sex, weight=weight #WHICH needs to always in CM!
                       ))

    output$prediction <- renderText({
      round(risks, 1)})
  }) 
}

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