R 在不从0开始的闪亮应用程序中设置移动v_线
我有以下闪亮的应用程序,它允许您过滤图形的一部分,并允许您启动计数器:R 在不从0开始的闪亮应用程序中设置移动v_线,r,shiny,R,Shiny,我有以下闪亮的应用程序,它允许您过滤图形的一部分,并允许您启动计数器: library(shiny) UI <- fluidPage( div(style="display:inline-block",numericInput("start_time", "Starting time:", 1, min = 1, max = 100)), div(style="display:inline-block",numericInput("stop_time", "Stop time
library(shiny)
UI <- fluidPage(
div(style="display:inline-block",numericInput("start_time", "Starting time:", 1, min = 1, max = 100)),
div(style="display:inline-block",numericInput("stop_time", "Stop time:", 5, min = 1, max = 100)),
actionButton("start_counter","Start the counter"),
plotOutput("plot_timeseries", width = "500px", height = "300px"),
plotOutput("plot_timeseries2", width = "500px", height = "300px")
)
Server <- function(input, output, session) {
counter <- reactiveVal(1)
action <- reactiveVal(FALSE)
# When goButton is clicked, set action() from FALSE to TRUE or the other way around.
observeEvent(input$start_counter,
{
action(!action())
})
# Add an oberserver that invalidates every second, and increments the counter if action()==TRUE
observe({ invalidateLater(1000, session)
isolate({
if(action())
{
# Add 1 to our counter
counter(counter() + 1)
}
})
})
pp <- eventReactive(c(input$start_time, input$stop_time, counter()), {
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
scale_x_continuous(limits = c(input$start_time, input$stop_time)) +
geom_vline(xintercept = counter())
})
output$plot_timeseries <- renderPlot({
pp()
})
output$plot_timeseries2 <- renderPlot({
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
geom_vline(xintercept = counter())
})
}
shinyApp(ui = UI, server = Server)
关于我应该做什么来实现我的目标有什么想法吗?正如错误所说,您只能在反应上下文中使用
input$start\u time
,即reactive()
或observe()
或类似的内容
最简单的方法是将计数器(输入$start\u time)
移动到observe()
函数中,如下所示:
observe({
counter(input$start_time)
})
与反应式表达式不同,观察者使用急切求值,这意味着当依赖项发生变化时,会立即重新执行。这意味着此代码块将在任何
renderPlot
函数之前立即运行并更新计数器的值,并且每当input$start\u time
的值发生变化时,它将立即运行。这里是实现所需的一种方法。我修改了你的代码,只是在代码上写着modified
或ADDED
的地方
现在,我已将vline设置为在您按下重置按钮时移动到开始时间。只要开始时间改变,您也可以重置该行,如果这是您想要的行为。只需将observeEvent
更改为:
# ADDED - if start time changes, reset the counter.
observeEvent(input$reset_counter,
{
counter(input$start_time)
})
希望这有帮助
库(闪亮)
图书馆(GG2)
用户界面
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do
something that can only be done from inside a reactive expression or observer.)
observe({
counter(input$start_time)
})
# ADDED - if start time changes, reset the counter.
observeEvent(input$reset_counter,
{
counter(input$start_time)
})
library(shiny)
library(ggplot2)
UI <- fluidPage(
div(style="display:inline-block",numericInput("start_time", "Starting time:", 1, min = 1, max = 100)),
div(style="display:inline-block",numericInput("stop_time", "Stop time:", 5, min = 1, max = 100)),
actionButton("start_counter","Start the counter"),
actionButton("reset_counter","Reset the counter"), # ADDED
plotOutput("plot_timeseries", width = "500px", height = "300px"),
plotOutput("plot_timeseries2", width = "500px", height = "300px")
)
Server <- function(input, output, session) {
counter <- reactiveVal(1)
action <- reactiveVal(FALSE)
# When goButton is clicked, set action() from FALSE to TRUE or the other way around.
observeEvent(input$start_counter,
{
action(!action())
})
# Add an oberserver that invalidates every second, and increments the counter if action()==TRUE
observe({ invalidateLater(1000, session)
isolate({
if(action() & counter() < input$stop_time) # MODIFIED - stop at stop time.
{
# Add 1 to our counter
counter(counter() + 1)
}
else # ADDED - to stop the timer from running when we modify the start or stop time.
{
action(FALSE)
}
})
})
# ADDED - if start time changes, reset the counter.
observeEvent(input$reset_counter,
{
counter(input$start_time)
})
pp <- eventReactive(c(input$start_time, input$stop_time, counter()), {
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
scale_x_continuous(limits = c(input$start_time, input$stop_time)) +
geom_vline(xintercept = counter())
})
output$plot_timeseries <- renderPlot({
pp()
})
output$plot_timeseries2 <- renderPlot({
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) +
geom_vline(xintercept = counter())
})
}
shinyApp(ui = UI, server = Server)