【问题标题】:Numeric output value as condition for conditionalPanel数值输出值作为 conditionalPanel 的条件
【发布时间】:2015-04-24 05:57:59
【问题描述】:

我真正想做的是传递一个数字输出条件来生成条件面板。不过,我似乎无法将输出呈现为数字。

我的解决方法是使用感兴趣的数字条件生成反应,并将其作为文本传递给条件面板。但是,我也在为此苦苦挣扎。

下面是一个例子:

目标是在用户输入乘以 5 大于 100 的数字时打开条件面板 是 - 我知道我可以通过设置 input$number >20 来做到这一点 这是我正在研究的一个简化示例。 问题是使用数字输出作为条件面板

谢谢!

山姆

library(shiny)

shinyApp(
  ui = fluidPage(
    fluidRow(
            wellPanel(
              numericInput("number", label = "Choose a Number between -100 and 100", 
                           min = -100, max = 100, value = 0))),
    fluidRow(wellPanel(textOutput("text_out"))),
    fluidRow(conditionalPanel(
      condition = "output.big == '1' ", 
      fluidRow(h1("You've selected a number larger than 20!"))))),
  server = function(input, output, session){
    # I want to execute a function on a numeric input
    times_5<-reactive({ 
      input$number*5
      })   
    # I can't get the condition to work with a numeric query
    # So, I run the query on the server side and generate an if/else statement 
    # to pass a text variable
    over_100<-reactive({
      if(times_5()>100){
        return(1)
      } else{
        return(0)
      }
    })

    output$text_out<-renderText({
       paste("This is text out put of the number ", input$number, "multiplied by 5 to equal ", times_5(), over_100())
    })
    # I can't find a render function that generates numeric output
    # Even still this outputtext doesn't seem to work in the conditional statement.
    output$big<-renderText({
      over_100
    })
  }
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一开始似乎Shiny needs to have the value displayed在一个条件下使用它,但实际上并非如此:

    outputOptions(output, 'big', suspendWhenHidden=FALSE)
    

    当它不可见时,您会告诉 Shiny 考虑该值。

    即使你在你的服务器函数中添加了这一行,你还需要另一个改变:需要计算 big 的值,所以这样:

    output$big<-renderText({
      over_100
    })
    

    必须变成这样:

    output$big<-renderText(over_100())
    

    希望对你有帮助。

    【讨论】:

    • 谢谢!当我调用响应式时,我总是忘记 ()。 outputOptions 正是我所需要的。
    • outputOptions(output, 'big', suspendWhenHidden=FALSE) 是个大问题。谢谢@mucio。
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2020-01-19
    • 2017-07-05
    • 1970-01-01
    • 2021-09-28
    • 2019-05-15
    相关资源
    最近更新 更多