R闪亮的隔离反应数据框 [英] R shiny isolate reactive data.frame

查看:46
本文介绍了R闪亮的隔离反应数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解 isolate()reactive() 应该如何在 R Shiny 中使用.

I am struggling to understand how isolate() and reactive() should be used in R Shiny.

我想实现以下目标:

每当点击刷新"操作按钮时:

Whenever the "Refresh" action button is clicked:

  • 对 data.frame 执行 subset 和,

将此输入到我的函数中以重新计算值.

Feed this into my function to recalculate values.

该子集取决于用户勾选的一组复选框,其中大约有 40 个.我不能让这些复选框完全响应",因为该函数需要大约 1.5 秒来执行.相反,我想让用户有机会选择多个框,然后才单击按钮以 (a) 子集和 (b) 再次调用该函数.

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

为此,我在 server.R 函数中加载 data.frame:

To do so, I load the data.frame in the server.R function:

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

然后我有我的主要 ShinyServer 功能:

Then I have my main shinyServer function:

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)
  })
}

推荐答案

怎么样

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

这是我的最小示例.

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

编辑

另一个实现,更简洁一点.renderTable 默认检查函数内所有反应元素的变化(在本例中,input$numinput$button).但是,您希望它只对按钮做出反应.因此,您需要将要忽略的元素放在 isolate 函数中.如果省略 isolate 函数,则只要移动滑块,表格就会更新.

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). But, you want it to react only to the button. Hence you need to put the elements to be ignored within the isolate function. If you omit the isolate function, then the table is updated as soon as the slider is moved.

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

这篇关于R闪亮的隔离反应数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆