【问题标题】:reset animation in Shiny R Studio在 Shiny R Studio 中重置动画
【发布时间】:2014-11-28 19:28:11
【问题描述】:

我正在使用 R Studio 的 Shiny 构建一个动画图形项目。目前的“Go!”按钮启动动画。我想让“重置”按钮重新初始化变量并重新运行动画,但由于 Shiny 不允许对 input$button 值进行代码内更改,我被困在如何做到这一点上。实际项目在形式上与下面的示例块相似,但涉及更多。动画是传达信息的组成部分。项目完成后,我打算将其部署在 Shiny 服务器上,因此我希望用户能够重新运行具有不同选择的动画,而无需重新打开链接。

    #  ui.R
library(shiny)

shinyUI(fluidPage(

  # Application title
  headerPanel("Cost Explorer"),


  sidebarPanel(
    actionButton("goButton", "Go!"),
    actionButton("reset", "Reset"),

    sliderInput("myvar", label=h6("Variability of cost"),
                min=0, max=50, value=10)   
  ),

  mainPanel(

    plotOutput(outputId="tsplot")

  )
))

    # server.R
library(shiny)

shinyServer(function(input, output, session) {

  # initialize reactive values
  ts <- reactiveValues(cost=rep(NA,100), year=(2010:2109), counter=1)

  output$tsplot <- renderPlot({
    plot(ts$year, ts$cost, xlim=c(2010,2110), ylim=c(-200,200), xlab="Year",
         ylab="Cost (US Dollars)", type="l", main="Forecasted Cost Time series")
  })

  observe({

    isolate({


        if (ts$counter==1){
          ts$cost[ts$counter]=50 #initial cost
        }
        if (ts$counter > 1){
          ts$cost[ts$counter]=ts$cost[ts$counter-1]+rnorm(1,0,input$myvar)
        }
        ts$counter=ts$counter+1

    })

    if (((isolate(ts$counter) < 100)) & (input$goButton > 0)){
      invalidateLater(200, session)
    }
    if (input$reset > 0){
      # How do I add reset functionality?

    }
  }) 
})

【问题讨论】:

    标签: r animation shiny reset


    【解决方案1】:

    根据您的应用,添加另一个 observe 并使用全局赋值运算符 &lt;&lt;- 将计数器重置为 1 会更快。我也改变了情节,所以它是情节索引变量。看看人们遇到的类似问题,here。注意:在我的一些应用程序中,当用户按下开始按钮两次时,我也有暂停按钮,您可以通过检查按钮索引是否可被 2 整除来实现这一点,因为每次单击按钮时,它都会增加 1。

    我正在进一步研究您的应用程序,确保您正在收集未引用的观察者的垃圾,因为您可能会耗尽内存(通过任务管理器查看内存配置文件)。查看此示例here,或者您可以为每个会话设置注销功能,其中客户端将在 n 分钟后注销。

    rm(list = ls())
    library(shiny)
    
    ui <- (fluidPage(
      # Application title
      headerPanel("Cost Explorer"),
    
      sidebarPanel(
        actionButton("goButton", "Go!"),
        actionButton("reset", "Reset"),
        sliderInput("myvar", label=h6("Variability of cost"),min=0, max=50, value=10)   
      ),
      mainPanel(plotOutput(outputId="tsplot"))
    ))
    
    server <- (function(input, output, session) {
    
      # initialize reactive values
      ts <- reactiveValues(cost=rep(NA,100), year=(2010:2109), counter=1)
    
      output$tsplot <- renderPlot({
        plot(ts$year[1:ts$counter], ts$cost[1:ts$counter], xlim=c(2010,2110), ylim=c(-200,200), xlab="Year",
             ylab="Cost (US Dollars)", type="l", main="Forecasted Cost Time series")
      })
    
      observe({
        isolate({
          if (ts$counter==1){
            ts$cost[ts$counter]=50 #initial cost
          }
          if (ts$counter > 1){
            ts$cost[ts$counter]=ts$cost[ts$counter-1]+rnorm(1,0,input$myvar)
          }
          ts$counter=ts$counter+1    
        })
        if (((isolate(ts$counter) < 100)) & (input$goButton > 0)){
          invalidateLater(200, session)
        }
    
      })
    
      observe({
        if (input$reset > 0){
          ts$counter <<- 1
        }
      })
    })
    
    runApp(list(ui = ui, server = server))
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2020-02-13
      • 2021-01-25
      • 2015-04-15
      • 2020-04-26
      • 2015-07-30
      相关资源
      最近更新 更多