仅对特定的 rhandsontable 列更改做出反应
React only on specific rhandsontable column change
我有一个图只依赖于更大的 rhandsontable 的特定列。我只想在该列发生变化时触发情节的反应性。我尝试过的每个解决方案都会导致重新渲染绘图,即使我更改了其他列也是如此。有办法吗?
这是一个最小的例子:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
plotOutput("chart"),
rHandsontableOutput("hot")
)
server <- function(input, output, session) {
DF <- data.frame(
x1 = 1,
x2 = 2,
x3 = 3, # I want to rerender the chart only if this column changes
stringsAsFactors = F
)
output$hot = renderRHandsontable({
rhandsontable(DF)
})
output$chart = renderPlot({
print("change!")
hist(hot_to_r(input$hot)$x3, col = 'skyblue3')
})
}
shinyApp(ui, server)
这是通过 observeEvent
和 reactiveVal
的解决方案:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
plotOutput("chart"),
rHandsontableOutput("hot")
)
server <- function(input, output, session) {
DF <- data.frame(
x1 = 1,
x2 = 2,
x3 = 3, # I want to rerender the chart only if this column changes
stringsAsFactors = F
)
output$hot = renderRHandsontable({
rhandsontable(DF)
})
x3 <- reactiveVal()
observeEvent(input$hot, {
x3(hot_to_r(input$hot)$x3)
}, ignoreInit = TRUE)
output$chart = renderPlot({
req({x3()})
print("change!")
hist(x3(), col = 'skyblue3')
})
}
shinyApp(ui, server)
我有一个图只依赖于更大的 rhandsontable 的特定列。我只想在该列发生变化时触发情节的反应性。我尝试过的每个解决方案都会导致重新渲染绘图,即使我更改了其他列也是如此。有办法吗?
这是一个最小的例子:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
plotOutput("chart"),
rHandsontableOutput("hot")
)
server <- function(input, output, session) {
DF <- data.frame(
x1 = 1,
x2 = 2,
x3 = 3, # I want to rerender the chart only if this column changes
stringsAsFactors = F
)
output$hot = renderRHandsontable({
rhandsontable(DF)
})
output$chart = renderPlot({
print("change!")
hist(hot_to_r(input$hot)$x3, col = 'skyblue3')
})
}
shinyApp(ui, server)
这是通过 observeEvent
和 reactiveVal
的解决方案:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
plotOutput("chart"),
rHandsontableOutput("hot")
)
server <- function(input, output, session) {
DF <- data.frame(
x1 = 1,
x2 = 2,
x3 = 3, # I want to rerender the chart only if this column changes
stringsAsFactors = F
)
output$hot = renderRHandsontable({
rhandsontable(DF)
})
x3 <- reactiveVal()
observeEvent(input$hot, {
x3(hot_to_r(input$hot)$x3)
}, ignoreInit = TRUE)
output$chart = renderPlot({
req({x3()})
print("change!")
hist(x3(), col = 'skyblue3')
})
}
shinyApp(ui, server)