简体   繁体   English

R Shiny:conditionalPanel具有基于textInput的条件(操作方法)

[英]R shiny: conditionalPanel with condition based on textInput (how to)

I would like to use a conditionPanel that would appear only if a preceding textInput has been completed (the initial value of it being ""). 我想使用仅在前面的textInput已完成(它的初始值为“”)时才会出现的conditionPanel。 What is the right way to define the condition in the conditionalPanel correctly. 在conditionalPanel中正确定义条件的正确方法是什么。 Here is a reproducible code: 这是可复制的代码:

ui <- pageWithSidebar(
headerPanel("TEST"),
sidebarPanel(
textInput('test', "","")),

mainPanel(               
conditionalPanel(condition = "input.test != "" ", #problem is here. I've tried many possibilities (such as input.test > 0) but none have worked
                 helpText("abc"))
)
)

server <- function(input,output){ 
}

runApp(list(ui=ui,server=server))

Any advice/suggestion would be greatly appreciated ! 任何建议/建议将不胜感激!

Cheers 干杯

As noted in the comments, changing the condition to "input.test != ''" or 'input.test != ""' works as it deals with the issue of having quotes within quotes. 如评论中所述,将条件更改为"input.test != ''"'input.test != ""'可以解决引号中包含引号的问题。

The other solution is to change the condition to "input.test.length > 0" which works because if you look at the source code for conditionalPanel (below) it's just creating a div where the data-display-if attribute is the condition. 另一种解决方案是将条件更改为"input.test.length > 0" ,这是"input.test.length > 0" ,因为如果您查看conditionalPanel的源代码(如下),则只是创建一个div ,其中data-display-if属性是条件。 Considering its going straight to html it was a good guess that using js for the condition would work. 考虑到直接使用html,可以很好地猜测使用js作为条件是可行的。

And I could have just read the docs... From ?conditionalPanel : 而且我本来可以阅读文档...来自?conditionalPanel

condition: A JavaScript expression that will be evaluated repeatedly to
          determine whether the panel should be displayed.

conditionalPanel
function (condition, ...) 
{
    div(`data-display-if` = condition, ...)
}

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

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