重置动画闪亮ř工作室 [英] reset animation in Shiny R Studio

查看:143
本文介绍了重置动画闪亮ř工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构造,使用R Studio的闪亮的一个动画图形项目。目前,走!按钮启动动画。我想有重置按钮重新初始化变量,然后重新运行动画,但由于闪亮不允许批内code更改输入$按钮值,我被困在如何做到这一点。真正的项目是在形式上与下面的示例块类似,但要更多地参与。向被输送的信息的动画是一体的。当项目完成后,我打算将它部署在闪亮的服务器上,所以我想用户能够重新运行不同的选择,动画,而无需重新打开链接。

 #ui.R
库(闪亮)shinyUI(fluidPage(  #应用程序标题
  headerPanel(成本资源管理器),
  sidebarPanel(
    actionButton(goButton,走!)
    actionButton(复位,复位),    sliderInput(myvar的标签= H6(成本可变性),
                分钟= 0,上限= 50,值= 10)
  )  mainPanel中(    plotOutput(outputId =tsplot)  )
))    #server.R
库(闪亮)shinyServer(功能(输入,输出,会话){  #初始化抗值
  TS< - reactiveValues​​(成本=代表(NA,100),年=(2010:2109),计数器= 1)  输出$ tsplot< - renderPlot({
    积(TS $一年,TS $的成本,XLIM = C(2010,2110),ylim = C(-200,200),xlab =年,
         ylab =成本(美元),键入=L,主要=预测成本时间序列)
  })  观察({    隔离({
        如果(TS $计数器== 1){
          TS $成本[TS $计数器] = 50的初次费用
        }
        如果(TS $计数器→1){
          TS $成本[TS $计数器] = $ TS成本[TS $反-1] + RNORM(1,0,输入$ MYVAR)
        }
        TS $计数器= $ TS专柜+ 1    })    如果(((分离物(TS $计数器)小于100))及(输入$ goButton大于0)){
      invalidateLater(200届)
    }
    如果(输入$复位&0){
      #如何添加重置功能?    }
  })
})


解决方案

根据您的应用程序是更快地添加其他观察和使用全局计数器重置为1赋值运算符<< - 。此外,我改变了剧情所以它是图索引变量。看看类似的问题,人们不得不,这里。注:在我的一些应用程序我也有暂停按钮,当用户presses启动按钮两次,则可以通过检查按钮指数整除由两个或没有,因为每次按钮被点击它增加实现这个由之一。

我进一步寻找到你的应用程序,请确保您有垃圾回收未引用的观察员,因为你可能会耗尽内存(看通过任务管理器的存储配置文件)。看看这个例子这里 ,交替即可建立每个会话在客户端后会被注销一个注销功能的 N 量分钟。

  RM(名单= LS())
库(闪亮)UI< - (fluidPage(
  #应用程序标题
  headerPanel(成本资源管理器),  sidebarPanel(
    actionButton(goButton,走!)
    actionButton(复位,复位),
    sliderInput(myvar的标签= H6(成本可变性),最小值为0,上限= 50,值= 10)
  )
  mainPanel中(plotOutput(outputId =tsplot))
))服务器16; - (功能(输入,输出,会话){  #初始化抗值
  TS< - reactiveValues​​(成本=代表(NA,100),年=(2010:2109),计数器= 1)  输出$ tsplot< - renderPlot({
    积(TS $年[1:TS $计数器],TS $成本[1:TS $计数器],XLIM = C(2010,2110),ylim = C(-200,200),xlab =年,
         ylab =成本(美元),键入=L,主要=预测成本时间序列)
  })  观察({
    隔离({
      如果(TS $计数器== 1){
        TS $成本[TS $计数器] = 50的初次费用
      }
      如果(TS $计数器→1){
        TS $成本[TS $计数器] = $ TS成本[TS $反-1] + RNORM(1,0,输入$ MYVAR)
      }
      TS $计数器= $ TS专柜+ 1
    })
    如果(((分离物(TS $计数器)小于100))及(输入$ goButton大于0)){
      invalidateLater(200届)
    }  })  观察({
    如果(输入$复位&0){
      TS $计数器<< - 1
    }
  })
})runApp(名单(UI = UI,服务器=服务器))

I am constructing an animated graph project using R Studio's Shiny. Currently the "Go !" button initiates the animation. I would like to have the "Reset" button re-initialize the variables and re-run the animation, but since Shiny does not allow within-code changes to the input$button values, I am stuck on how to do this. The real project is similar in form to the sample blocks below, but much more involved. Animation is integral to the information being conveyed. When the project is completed, I intend to deploy it on the Shiny server, so I would like users to be able to re-run the animation with different selections without having to re-open the link.

    #  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?

    }
  }) 
})

Based on your app it was quicker to add another observe and reset the counter to 1 using the global assignment operator <<-. Also I changed the plot so it is plots indexed variables. Have a look at similar problem people had, here. NB: In some of my apps I also have the pause button when a user presses the start button twice, you can achieve this by checking if the button index is divisible by two or not since every time the button is clicked it increments by one.

I was further looking into your app, make sure you are garbage collecting unreferenced observers, as you might run out of memory (look at the memory profile via Task manager). Look into this example here, alternately you can set-up a log-off functionality per session where the client will be logged off after n amount of minutes.

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))

这篇关于重置动画闪亮ř工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆