小编MSR*_*MSR的帖子

在闪亮的if语句中使用反应式表达式

我正在编写一个闪亮的应用程序,其中输出应该取决于变量的值,该变量在闪亮的反应式表达式中计算.而不是复制实际的应用程序,我相信我用以下简单的应用程序重新创建了我的问题:

ui.R file:

   library(shiny)

   shinyUI(pageWithSidebar(

   headerPanel("Illustrating problem"),

   sidebarPanel(

   numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step        =5)

  ),

  mainPanel(

    h4('You entered'),

    verbatimTextOutput("oid1"),

    verbatimTextOutput("odic")

  )
))

server.R file


library(shiny)

shinyServer(

  function(input, output) {

    output$oid1 <- renderPrint({input$id1})

    x<-reactive({5*input$id1})

    if (x()>50) output$oid2<-renderPrint({"You entered a number greater than 10"})

    else output$oid2<-renderPrint({"You entered a number less than or equal to 
10"})

  }
)
Run Code Online (Sandbox Code Playgroud)

如果我像这样运行它然后我得到错误: 错误 .getReactiveEnvironment()$currentContext():`

没有活动的反应上下文,不允许操作.(你试图做一些只能在反应式表达式或观察者内部完成的事情.)

如果我将if语句更改为:if (x>50)...然后我收到错误:

x> 50时出错:比较(6)仅适用于原子和列表类型

当我将if语句更改为:if (reactive({(x>50)}))...然后我收到错误消息:

if(反应({:参数不能解释为逻辑错误)时出错

我非常感谢任何帮助

r shiny

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

r ×1

shiny ×1