简体   繁体   English

R闪亮隔离反应数据框架

[英]R shiny isolate reactive data.frame

I am struggling to understand how isolate() and reactive() should be used in R Shiny. 我正在努力了解如何在R Shiny中使用isolate()reactive()

I want to achieve the following: 我要实现以下目标:

Whenever the "Refresh" action button is clicked: 每当单击“刷新”操作按钮时:

  • Perform a subset on a data.frame and, 在data.frame上执行subset ,然后,

  • Feed this into my function to recalculate values. 将其输入到我的函数中以重新计算值。

The subset depends on a group of checkboxes that the user has ticked, of which there are approximately 40. I cannot have these checkboxes "fully reactive" because the function takes about 1.5 sec to execute. 该子集取决于用户已选中的一组复选框,其中大约有40个复选框。我不能将这些复选框“完全响应”,因为该功能大约需要1.5秒才能执行。 Instead, I want to give the user a chance to select multiple boxes and only afterwards click a button to (a) subset and (b) call the function again. 相反,我想让用户有机会选择多个框,然后才单击按钮以(a)子集,然后(b)再次调用该函数。

To do so, I load the data.frame in the server.R function: 为此,我将data.frame加载到server.R函数中:

df1 <- readRDS("D:/././df1.RData")

Then I have my main shinyServer function: 然后,我有了主要的ShinyServer函数:

shinyServer(function(input, output) {

  data_output <- reactive({
    df1 <- df1[,df1$Students %in% input$students_selected] 

    #Here I want to isolate the "students_selected" so that this is only 
    #executed once the button is clicked
  })

  output$SAT <- renderTable({
    myFunction(df1)
  })
}

How about something like 怎么样

data_output <- eventReactive(input$button, {
    df1[,df1$Students %in% input$students_selected] 
})

Here is my minimal example. 这是我的最小示例。

library(shiny)
ui <- list(sliderInput("num", "rowUpto", min= 1, max = 10, value = 5), 
           actionButton("btn", "update"),
           tableOutput("tbl")) 
server <- function(input, output) {
  data_output <- eventReactive(input$btn, {
    data.frame(id = 1:10, x = 11:20)[seq(input$num), ]
  })

  output$tbl <- renderTable({
    data_output()})
}

runApp(list(ui = ui, server = server))

Edit 编辑

Another implementation, a bit more concise. 另一个实现,更加简洁。 renderTable by default inspects the changes in all reactive elements within the function (in this case, input$num and input$button ). 默认情况下, renderTable检查函数中所有反应式元素的更改(在本例中为input$numinput$button )。 But, you want it to react only to the button. 但是,您希望它仅对按钮做出反应。 Hence you need to put the elements to be ignored within the isolate function. 因此,您需要将要忽略的元素放入isolate函数中。 If you omit the isolate function, then the table is updated as soon as the slider is moved. 如果省略了isolate功能,则在移动滑块后立即更新表格。

library(shiny)
ui <- list(sliderInput("num", "rowUpto", min= 1, max = 10, value = 5), 
           actionButton("btn", "update"),
           tableOutput("tbl")) 
server <- function(input, output) {
  output$tbl <- renderTable({
    input$btn
    data.frame(id = 1:10, x = 11:20)[seq(isolate(input$num)), ]
  })
}

runApp(list(ui = ui, server = server))

Use eventReactive instead: 请改用eventReactive

data_output <- eventReactive(input$updateButton, {

    df1 <- df1[,df1$Students %in% input$students_selected] #I think your comments are messed up here, but I'll leave the filtering formatting to you

  })

  output$SAT <- renderTable({
    data_output()
  })

And in your UI you should have something like: 在您的用户界面中,您应该具有以下内容:

actionButton('updateButton',label = "Filter")

Looking at ?shiny::eventReactive: 看?shiny :: eventReactive:

Use eventReactive to create a calculated value that only updates in response to an event. 使用eventReactive创建仅响应事件而更新的计算值。 This is just like a normal reactive expression except it ignores all the usual invalidations that come from its reactive dependencies; 这就像一个普通的反应式表达式,只是它忽略了来自其反应式依赖关系的所有常规失效; it only invalidates in response to the given event. 它仅在响应给定事件时才无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM