2016-01-06 110 views
0

快速的问题上conditionalPanel为发亮/ R。conditionalPanel在R /光泽

使用从RStudio稍微修改代码示例,请考虑下面这个简单的闪亮应用:

n <- 200 


# Define the UI 
ui <- bootstrapPage(
    numericInput('n', 'Number of obs', n), 
    conditionalPanel(condition = "input.n > 20", 
    plotOutput('plot')), 
    HTML("Bottom") 
) 

# Define the server code 
server <- function(input, output) { 
    output$plot <- renderPlot({ 
     if (input$n > 50) hist(runif(input$n)) else return(NULL) 
    }) 
} 

# Return a Shiny app object 
shinyApp(ui = ui, server = server) 

我的目标是隐藏图形和向上移动HTML文本,以避免差距。现在,你可以看到,如果输入的值低于20,图中是隐藏的文本“底”也相应上升。然而,如果所输入的值是大于20,但小于50,图表函数返回NULL,虽然没有显示图表,文本“底部”并不向上移动。

的问题是:有没有办法,我可以设置一个conditionalPanel,使得它出现/根据情节功能是否返回NULL隐藏?我问的原因是因为触发一个有点复杂(除其他事项外这取决于输入文件的选择,因此需要改变,如果一个不同的文件被加载),我想,以避免编写它在ui.R文件上。

任何建议表示欢迎,

菲利普

回答

2

嗨,你可以在这样的服务器创建conditionalPanel条件:

n <- 200 
library("shiny") 

# Define the UI 
ui <- bootstrapPage(
    numericInput('n', 'Number of obs', n), 
    conditionalPanel(condition = "output.cond == true", # here use the condition defined in the server 
        plotOutput('plot')), 
    HTML("Bottom") 
) 

# Define the server code 
server <- function(input, output, session) { 
    output$plot <- renderPlot({ 
    if (input$n > 50) hist(runif(input$n)) else return(NULL) 
    }) 
    # create a condition you use in the ui 
    output$cond <- reactive({ 
    input$n > 50 
    }) 
    outputOptions(output, "cond", suspendWhenHidden = FALSE) 
} 

# Return a Shiny app object 
shinyApp(ui = ui, server = server) 

不要忘记添加session在你的服务器功能和outputOptions在该函数中的某个地方调用。

+0

太好了!谢谢,Victorp。 – PMaier