问题很简单.首先,我在渲染图中尝试了if-else条件.就像是
if (input$Next > 0) { plot(...) } else { return() }
这没用.即使尚未满足条件,也会显示稍后放置绘图的灰色区域.在下一步中,我尝试使用验证(请参阅 此处).我基本上复制了给定示例中的代码.但是,当实际上不满足条件时,它仍然显示灰色区域.我目前的尝试如下:
ui.R
shinyUI(fluidPage( sidebarPanel( plotOutput("test"), actionButton("Next", "Next") ))
server.R
shinyServer(function(input, output, session) { function(input, output) { output$test <- renderPlot({ validate( need(input$Next > 0) ) pt <- plot(input$Next,2) print(pt) }) } })
绘图功能仅用于说明.我看起来不一样.任何帮助都非常感谢!
第一种可能性 -conditionalPanel
我们想要显示plotOutput
是否actionButton
按下了.更具体地说,如果input.Next > 0
.这个条件被评估javaScript
,因此我们的语法略有不同 - 而不是$
我们.
在输入后使用,我们使用括号.
conditionalPanel( condition = "input.Next * 1 > 0", plotOutput("test") )
但是,我们乘以input.Next
1 是很奇怪的.这是必要的,因为input.Next
,期望一个数字,也返回属性.似乎JavaScript不知道如何处理这个......但是乘法可以解决问题.
[1] 0 attr(,"class") [1] "integer" "shinyActionButtonValue"
在这个例子中,立即plotOutput
出现......肯定太快了.
library(shiny) ui1 <- shinyUI(fluidPage( sidebarPanel( conditionalPanel( condition = "input.Next * 1 > 0", plotOutput("test") ), actionButton("Next", "Next") ) )) server1 <- shinyServer(function(input, output, session) { output$test <- renderPlot({ pt <- plot(input$Next, 2) print(input$Next) print(pt) }) }) shinyApp(ui1, server1)
"减速火车"
在这个例子中,我们将"减速"超速plotOutput
.为此,我们需要包装shinyjs
.
首先,我们要用一个id 包装conditionalPanel
成一个div
id,比方说,animation
div(id = "animation", conditionalPanel( condition = "input.Next * 1 > 0", plotOutput("test") ) )
然后,在服务器端,我们将以下列方式定义动画:条件在输入$ next,div应该显示幻灯片动画.
observe({ toggle(id = "animation", anim = TRUE, animType = "slide", time = 0.5, condition = input$Next > 0) })
完整示例:
ui2 <- shinyUI(fluidPage( # we need to include this function in order to use shinyjs functions useShinyjs(), sidebarPanel( actionButton("Next", "Next"), div(id = "animation", conditionalPanel( condition = "input.Next * 1 > 0", plotOutput("test"), sliderInput("manipulate", "slider", min = 0, max = 1, value = 1) ) ) ) )) server2 <- shinyServer(function(input, output, session) { # Introduce gently the div with an id = "animation" and its all content. observe({ toggle(id = "animation", anim = TRUE, animType = "slide", time = 0.5, condition = input$Next > 0) }) # We could animate only the plotOutput with "toogle(id = test")" # - it would work as well, but for the first time the plot is shown # way we would get an errors with margins. output$test <- renderPlot({ #plot(input$Next, 2) ggplot(iris, aes(x = Species)) + geom_bar(alpha = input$manipulate) }) }) shinyApp(ui2, server2)
renderUI
正如您所指出的,另一种可能性是使用该功能renderUI
.如果要一次渲染多个元素,则必须将它们包装list
为如下例所示:
library(shiny) library(ggplot2) ui3 <- shinyUI(fluidPage( sidebarPanel( uiOutput("dynamic"), actionButton("Next", "Next") ) )) server3 <- shinyServer(function(input, output, session) { output$dynamic <- renderUI({ if (input$Next > 0) { # if we want to render more element, we need the list list( plotOutput("test"), sliderInput("manipulate", "slider", min = 0, max = 1, value = 1) ) } }) output$test <- renderPlot({ #plot(input$Next, 2) ggplot(iris, aes(x = Species)) + geom_bar(alpha = input$manipulate) }) }) shinyApp(ui3, server3)