0

I am trying to show navigation in R plot. The current status in time one or (t1) is set as val$currentstatus and next status in (t2) wants to be shown in the graph based on the action that the user choose from the checkbook. then I want to draw a line to show this path. The code that I wrote is as following

output$navigation<-renderPlot({



          #initial state of X and Y
          if(is.element("Within", vals$currentstatus))
            x <- 1
          y <- 2
          if(is.element("Out", vals$currentstatus)) {
            x <- 1
            y <- 1
          }
          action<-c(input$action1,input$action2)
          x<-1:4
          y<-1:2
          rewards<-matrix(rep(0,8),nrow=2)
          rewards[1,4]<- -1
          rewards[2,4]<- 1
          values<-rewards#initial Values
          states<-expand.grid(x=x,y=y)
          #Transition probability
          transition <- list( input$action1= c("within" = 0.8, "out" = .2), 
                              input$action2= c("within" = 0.3, "out" = .7))
          # The value of an action (e.g. move toward benchmark )
          action.values <- list(input$action1 = c("x" = 1, "y" = 0), 
                                input$action1 = c("x" = 1, "y" = 0))                   
          # act() function serves to move the agent to go to new position
          act <- function(action, state) {
            action.value <- action.values[[action]]
            new.state <- state
            #
            if(state["x"] == 4 && state["y"] == 1 || (state["x"] == 4 && state["y"] == 2))
              return(state)
            #
            new.x = state["x"] + action.value["x"]
            new.y=if(transition["within">"out"]){state["y"==2]}
            if(transition["within"<"out"]){state["y"==1]} 

          }
          plot(x, y, xaxt='n',yaxt='n',cex=10,pch=19,
               col=ifelse(y==1,"red","green"),ylab="status",xlab="period",
               xlim=c(0,4), ylim=c(0,3))
          axis(1,at=1:4,labels=c("t1","t2","t3","t4"))
          axis(2,at=1:2,labels=c("out bench","within bench"))

if next position is within bench it should be green and connect to the previous state and if it is out of bench should be red and connect to previous state. Also I want to see the name of chosen action on the connection line between two states.Moreover I want to know how can I update the new position and use it for calculating next state in next period (t3) and so force.Similar to the following graph: Navigation Graph

user
  • 592
  • 6
  • 26
  • Maybe have a look at the `graphviz` package? – RmIu Oct 21 '15 at 10:03
  • Thank you so much I will look at it and let you know the result. – user Oct 21 '15 at 10:25
  • Sure, could you create some mock data to create the graph from? – RmIu Oct 21 '15 at 10:30
  • Yes, But I stuck in one section , I want to update the transition probability matrix in each state. I am thinking if I use Empirical distribution to update the probability matrix for each state it will be useful, but I am not sure. I try to use this method for updating. If you have any Idea Regarding this issue please give me any advice. – user Oct 21 '15 at 10:53
  • I'm not really sure how to achieve this without functioning sample code unfortunately, maybe post a small functioning example? if you want to update plot outputs from loops you could try invalideLater – RmIu Oct 21 '15 at 11:01
  • Can you please show me the link for invalideLater to see some examples of that. – user Oct 21 '15 at 11:15
  • If I understand you correctly, you want to update the plot multiple times in a loop, here's some examples on how to do that [link](http://stackoverflow.com/questions/33241733/force-rendering-of-already-computed-reactive-elements/33246189#33246189) – RmIu Oct 21 '15 at 11:17
  • Yes ,Exactly you are right. Thanks I will check this link too. thanks for your help. – user Oct 21 '15 at 11:22
  • Sure thing! and if you can put together a small self-contained code sample I could take a look at that – RmIu Oct 21 '15 at 11:26
  • Ok I will do it now. – user Oct 21 '15 at 11:30
  • I have updated my question I explain everything that I want with in the #lines – user Oct 21 '15 at 11:49

0 Answers0