R 闪亮隔离反应 data.frame
R shiny isolate reactive data.frame
我正在努力理解如何在 R Shiny 中使用 isolate()
和 reactive()
。
我想实现以下目标:
每当单击 "Refresh" 操作按钮时:
在 data.frame 上执行 subset
并且,
将其输入我的函数以重新计算值。
该子集取决于用户勾选的一组复选框,其中大约有 40 个。我不能拥有这些复选框 "fully reactive",因为该函数执行大约需要 1.5 秒。相反,我想让用户有机会 select 多个框,然后才单击按钮以 (a) 子集和 (b) 再次调用该函数。
为此,我在 server.R 函数中加载了 data.frame:
df1 <- readRDS("D:/././df1.RData")
然后我有我的 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)
})
}
改用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()
})
在你的 UI 中你应该有这样的东西:
actionButton('updateButton',label = "Filter")
正在查看 ?shiny::eventReactive:
Use eventReactive to create a calculated value that only updates in
response to an event. 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.
怎么样
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$num
和 input$button
)。
但是,您希望它只对按钮做出反应。因此,您需要将要忽略的元素放在 isolate
函数中。
如果省略 isolate
函数,则 table 会在滑块移动后立即更新。
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 Shiny 中使用 isolate()
和 reactive()
。
我想实现以下目标:
每当单击 "Refresh" 操作按钮时:
在 data.frame 上执行
subset
并且,将其输入我的函数以重新计算值。
该子集取决于用户勾选的一组复选框,其中大约有 40 个。我不能拥有这些复选框 "fully reactive",因为该函数执行大约需要 1.5 秒。相反,我想让用户有机会 select 多个框,然后才单击按钮以 (a) 子集和 (b) 再次调用该函数。
为此,我在 server.R 函数中加载了 data.frame:
df1 <- readRDS("D:/././df1.RData")
然后我有我的 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)
})
}
改用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()
})
在你的 UI 中你应该有这样的东西:
actionButton('updateButton',label = "Filter")
正在查看 ?shiny::eventReactive:
Use eventReactive to create a calculated value that only updates in response to an event. 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.
怎么样
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$num
和 input$button
)。
但是,您希望它只对按钮做出反应。因此,您需要将要忽略的元素放在 isolate
函数中。
如果省略 isolate
函数,则 table 会在滑块移动后立即更新。
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))