gpt4 book ai didi

r - 在 R : How can I fade-out my plotOutput from renderPlot if certain conditions are not met? 中 Shiny

转载 作者:行者123 更新时间:2023-12-02 00:52:40 25 4
gpt4 key购买 nike

这个问题很简单。首先,我在渲染图中尝试了一个 if-else 条件。有点像

if (input$Next > 0) {
plot(...)
}
else {
return()
}

这没有用。即使尚未满足条件,也会显示稍后放置地 block 的灰色区域。在下一步中,我尝试使用验证(请参阅 here )。我基本上复制了给定示例中的代码。但是,在实际不满足条件时,它仍然显示灰色区域。我目前的尝试如下所示:

ui.R

shinyUI(fluidPage(
sidebarPanel(
plotOutput("test"),
actionButton("Next", "Next")
))

服务器.R

shinyServer(function(input, output, session) {
function(input, output) {
output$test <- renderPlot({
validate(
need(input$Next > 0)
)
pt <- plot(input$Next,2)
print(pt)
})
}
})

plot 函数只是为了说明。我的看起来不一样。非常感谢任何帮助!

最佳答案

  1. 第一种可能性 - conditionalPanel

如果按下 actionButton,我们想显示 plotOutput。更具体地说,如果 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)

  1. "Slowing down the train"

在这个例子中,我们要“减慢”超速的plotOutput。为此,我们需要包 shinyjs

首先,我们要将 conditionalPanel 包装到一个带有 id 的 div 中,比如 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)

  1. 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)

关于r - 在 R : How can I fade-out my plotOutput from renderPlot if certain conditions are not met? 中 Shiny ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com