有没有办法根据向量的特定值在 R 中为仪表制作动画?
Is there a way to animate a gauge in R, according to specific values from a vector?
我正在尝试创建一个根据向量中定义的特定值而变化的动画仪表图。这是针对 R 中的闪亮应用程序。我目前正在使用 C3 库,但没有任何限制。我确实希望在 R 中用 shiny 编写代码。
下面的代码做了类似的事情,但动画以随机值运行。我想为动画的每一帧设置特定的值。
runApp(list(
ui = bootstrapPage(
# example use of the automatically generated output function
column(6, C3GaugeOutput("gauge1"))
),
server = function(input, output) {
#riskList <- c(10,20,30,40,50,60,70,80,90,100)
# reactive that generates a random value for the gauge
value = reactive({
invalidateLater(1000)
round(runif(1,min=0,max=100),2)
})
# example use of the automatically generated render function
output$gauge1 <- renderC3Gauge({
# C3Gauge widget
C3Gauge(value())
})
}
))
输出应该类似于我们在 plotly 中使用带 frame 参数的动画图表时得到的输出。我应该有一个输入向量(例如 c(10,20,30,40,50))、一个播放按钮和一个仪表图作为输出。单击按钮后,我希望仪表显示并输出 10、20、30 等等。
使用应用程序状态变量和不断 re-validating gauge_value.
尝试这种方法
library(c3)
library(shiny)
ui <- fluidPage(
c3Output("gauge1"),
actionButton("start_button", "Start Engines")
)
server <- function(input, output, session) {
gauge_breaks <- seq(0, 100, 10)
is_animating <- FALSE
current_index <- 1
observeEvent(input$start_button, {
current_index <<- 0
is_animating <<- TRUE
})
gauge_value = reactive({
invalidateLater(1000)
if(is_animating)
current_index <<- current_index + 1
if(current_index == length(gauge_breaks))
is_animating <<- FALSE
data.frame(rpm = as.numeric(gauge_breaks[current_index]))
})
output$gauge1 <- renderC3({
c3_gauge(c3(gauge_value()))
})
}
虽然我不喜欢使用 <<-
运算符,但我发现这个版本比另一个版本更容易理解,后者将当前索引存储在 numericInput
中,该 numericInput
隐藏在始终隐藏的 conditionalPanel
。该方法需要 isolate()
以遵守 invalidateLater()
,而不是每次索引更改时立即更新 gauge_value()
。
我正在尝试创建一个根据向量中定义的特定值而变化的动画仪表图。这是针对 R 中的闪亮应用程序。我目前正在使用 C3 库,但没有任何限制。我确实希望在 R 中用 shiny 编写代码。
下面的代码做了类似的事情,但动画以随机值运行。我想为动画的每一帧设置特定的值。
runApp(list(
ui = bootstrapPage(
# example use of the automatically generated output function
column(6, C3GaugeOutput("gauge1"))
),
server = function(input, output) {
#riskList <- c(10,20,30,40,50,60,70,80,90,100)
# reactive that generates a random value for the gauge
value = reactive({
invalidateLater(1000)
round(runif(1,min=0,max=100),2)
})
# example use of the automatically generated render function
output$gauge1 <- renderC3Gauge({
# C3Gauge widget
C3Gauge(value())
})
}
))
输出应该类似于我们在 plotly 中使用带 frame 参数的动画图表时得到的输出。我应该有一个输入向量(例如 c(10,20,30,40,50))、一个播放按钮和一个仪表图作为输出。单击按钮后,我希望仪表显示并输出 10、20、30 等等。
使用应用程序状态变量和不断 re-validating gauge_value.
尝试这种方法library(c3)
library(shiny)
ui <- fluidPage(
c3Output("gauge1"),
actionButton("start_button", "Start Engines")
)
server <- function(input, output, session) {
gauge_breaks <- seq(0, 100, 10)
is_animating <- FALSE
current_index <- 1
observeEvent(input$start_button, {
current_index <<- 0
is_animating <<- TRUE
})
gauge_value = reactive({
invalidateLater(1000)
if(is_animating)
current_index <<- current_index + 1
if(current_index == length(gauge_breaks))
is_animating <<- FALSE
data.frame(rpm = as.numeric(gauge_breaks[current_index]))
})
output$gauge1 <- renderC3({
c3_gauge(c3(gauge_value()))
})
}
虽然我不喜欢使用 <<-
运算符,但我发现这个版本比另一个版本更容易理解,后者将当前索引存储在 numericInput
中,该 numericInput
隐藏在始终隐藏的 conditionalPanel
。该方法需要 isolate()
以遵守 invalidateLater()
,而不是每次索引更改时立即更新 gauge_value()
。