我使用带有 animate 参数的 sliderInput
函数在我的shinyApp中执行自动计算。但是,使用 animate
会影响我的应用中的其他 react 。我想阻止其他 react 中发生的这种副作用。
例如,我在 if
else
结构内有一个 dropdownMenu
。但是,由于 animate
参数,当我启动动画时,我无法单击右上角的选项。它随着动画执行的每次计算而消失。请参阅:
我尝试在 pred_1()
中使用 isolate()
但它停止了 valueBox
和条件结构 (if
else
结构)。
我想让 animate
不影响应用中的其他 react 性。
我的应用程序:
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
dropdownMenuOutput(
outputId = "drop1"
)
)
sidebar <- dashboardSidebar(
width = 300
)
body <- dashboardBody(
sliderInput(
inputId = "one",
label = "Registro 1",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "two",
label = "Registro 2",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "three",
label = "Sum 3",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
valueBoxOutput(
outputId = "box1"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(session, input, output) {
fx <- function(x, y) {
x + y
}
fy <- function(x) {
x
}
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
chuveiro <- reactive({
temp <- reac_0()
fx(
x = temp$one,
y = temp$two
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
fdrop <- function(x) {
if (x <= 5) {
dropdownMenu(
type = "notifications",
badgeStatus = NULL,
headerText = "Not 1",
notificationItem(
text = HTML(
"<text style='color:#020202;'>Without.</text>"
),
status = "danger",
icon = icon("times"),
href = NULL
)
)
} else (
dropdownMenu(
type = "notifications",
badgeStatus = "danger",
headerText = "Not 2",
notificationItem(
text = HTML(
"<a style='color:#020202;'
href='https://www.instagram.com' target='_blank' title='A'>
With</a>"
),
status = "success",
icon = icon("tags")
)
)
)
}
output$drop1 <- renderMenu({
expr = fdrop(x = luz())
})
output$box1 <- renderValueBox({
expr = valueBox(
value = chuveiro(),
subtitle = "Sum"
)
})
}
shinyApp(ui, server)
那么如何在 shinyApp
中使一种 react 影响另一种 react ?
我想使用与 animate
配合使用的 sliderInput
来正常单击下拉菜单通知。
我尝试隔离 sliderInput
的每个参数,但不起作用。
最佳答案
只要输入发生变化,reac0()
react 就会被触发。然后它会触发包含它的每个 react ,即使该 react 中使用的值没有改变。
您的代码:
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
每当一个
、两个
或三个
发生变化时,就会触发luz()
。
另一种方法是直接使用input$third
:
luz <- reactive({
fy(
x = input$three
)
})
这样, slider one
和two
的变化或动画就不会触发luz()
,从而不会触发菜单渲染。
当然,由于 slider 三
的值用于渲染菜单,因此其值的任何更新都必须触发菜单渲染。
关于r - 独立的 react 在 Shiny 的应用程序中进行交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71055617/