为什么 reactive({ }) 不依赖于不断变化的输入?
Why doesn't reactive({ }) take a dependency on a changing input?
在下面的 Shiny 应用程序代码中,我希望在用户单击数据表中的新行时执行打印行。当我这样做时,textOutput 会按预期通过 input$table_rows_selected 更新所选行。但为什么 change <- reactive({ }) 不依赖于对 input$table_rows_selected 的更改并触发打印消息?
我看到它可以与 observe({}) 一起使用,但最终我想在不同的地方使用一个反应 returns 的值(例如这里 return 和 return2 ).
library(shiny)
library(DT)
ui <- fluidPage(
DT::DTOutput("table"),
textOutput("selected"),
textOutput("return"),
textOutput("return2")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
data.frame(a = 1:3, b = 4:6)
}, selection = 'single')
output$selected <- renderText({
input$table_rows_selected
})
change <- reactive({
input$table_rows_selected
print("it changed!")
"return"
})
output$return <- renderText({
isolate(change())
})
output$return2 <- renderText({
paste0(isolate(change()), "_2")
})
}
# Run the application
shinyApp(ui = ui, server = server)
您的代码有 2 个问题:
- a
reactive
只是一个函数,因此它的 return 值是 reactive
中生成的最后一个值 -> 您需要将 input$table_rows_selected
放在最后
-
isolate(change())
意味着 reactives
不依赖于 input$table_rows_selected
-> 删除 isolate
library(shiny)
library(DT)
ui <- fluidPage(
DT::DTOutput("table"),
textOutput("selected"),
textOutput("return"),
textOutput("return2")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
data.frame(a = 1:3, b = 4:6)
}, selection = 'single')
output$selected <- renderText({
input$table_rows_selected
})
change <- reactive({
print("it changed!")
input$table_rows_selected
})
output$return <- renderText({
change()
})
output$return2 <- renderText({
paste0(change(), "_2")
})
}
# Run the application
shinyApp(ui = ui, server = server)
在下面的 Shiny 应用程序代码中,我希望在用户单击数据表中的新行时执行打印行。当我这样做时,textOutput 会按预期通过 input$table_rows_selected 更新所选行。但为什么 change <- reactive({ }) 不依赖于对 input$table_rows_selected 的更改并触发打印消息?
我看到它可以与 observe({}) 一起使用,但最终我想在不同的地方使用一个反应 returns 的值(例如这里 return 和 return2 ).
library(shiny)
library(DT)
ui <- fluidPage(
DT::DTOutput("table"),
textOutput("selected"),
textOutput("return"),
textOutput("return2")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
data.frame(a = 1:3, b = 4:6)
}, selection = 'single')
output$selected <- renderText({
input$table_rows_selected
})
change <- reactive({
input$table_rows_selected
print("it changed!")
"return"
})
output$return <- renderText({
isolate(change())
})
output$return2 <- renderText({
paste0(isolate(change()), "_2")
})
}
# Run the application
shinyApp(ui = ui, server = server)
您的代码有 2 个问题:
- a
reactive
只是一个函数,因此它的 return 值是reactive
中生成的最后一个值 -> 您需要将input$table_rows_selected
放在最后 -
isolate(change())
意味着reactives
不依赖于input$table_rows_selected
-> 删除isolate
library(shiny)
library(DT)
ui <- fluidPage(
DT::DTOutput("table"),
textOutput("selected"),
textOutput("return"),
textOutput("return2")
)
server <- function(input, output) {
output$table <- DT::renderDataTable({
data.frame(a = 1:3, b = 4:6)
}, selection = 'single')
output$selected <- renderText({
input$table_rows_selected
})
change <- reactive({
print("it changed!")
input$table_rows_selected
})
output$return <- renderText({
change()
})
output$return2 <- renderText({
paste0(change(), "_2")
})
}
# Run the application
shinyApp(ui = ui, server = server)