Rstudio manipulate command

velappan periasamy's Avatar

velappan periasamy

28 Apr, 2011 02:52 PM

Sir,
require(quantmod) getSymbols("GLD") library(manipulate) manipulate( plot(as.matrix(Cl(GLD)), xlim=c(0,x.max)), x.max=slider(10,200))

I am not getting the plot of closing price of GLD.
Let me know how to get plot of GLD.
With regards,
veepsirtt

  1. Support Staff 2 Posted by Josh Paulson on 28 Apr, 2011 05:46 PM

    Josh Paulson's Avatar

    Veepsirtt,

    I think this is just an issue with the R commands you are calling. It does display the closing price of GLD, but only a small window of it. The following should work:

    manipulate(
      plot(as.matrix(Cl(GLD)), type="l", xlim=c(0,x.max)),
      x.max=slider(0,1000, step=25)
    )
    

    type="l"makes it a line instead of points and step=25 makes it easier to see the change as you move the manipulate slider. Try using the arrow keys and it works rather nice.

    Let me know if you have any additional questions,

    Josh

  2. 3 Posted by Liviu on 01 May, 2011 03:56 AM

    Liviu's Avatar

    A bit OT, but in the scope of the subject: wouldn't it be more practical if you published 'manipulate' on CRAN? It feels strange for a package to get shipped with an IDE, and there are people not using RStudio that might want to try it out.

    Thanks.

  3. 4 Posted by velappan periasamy on 01 May, 2011 04:50 AM

    velappan periasamy's Avatar

    Hello Josh,
    Why the slider point is not matching with the plot's x-axis?. Kindly check this.

    require(quantmod)
    getSymbols("GLD")
    library(manipulate)
    manipulate(

     plot(as.matrix(Cl(GLD)), type="l", xlim=c(0,x.max)),
     x.max=slider(0,length(GLD), step=1000)
    

    )

    thanks
    veepsirtt

  4. Support Staff 5 Posted by Josh Paulson on 02 May, 2011 12:39 PM

    Josh Paulson's Avatar

    Veepsirtt,

    This is because you would need to change the length() call to the following:

    length(as.matrix(Cl(GLD)))
    

    Note: If you do this, you should also change the step as the max is now 1090.

    Josh

  5. Support Staff 6 Posted by Josh Paulson on 02 May, 2011 12:41 PM

    Josh Paulson's Avatar

    liv.public,

    Currently the manipulate package only works with RStudio and would require more development before being allowed on CRAN.

    Thanks for the feedback,

    Josh

  6. 7 Posted by velappan periasamy on 03 May, 2011 05:15 PM

    velappan periasamy's Avatar

    Hello Josh,
    Now I want plot the mean along with the closing price of GLD.

    require(quantmod)

    tckr<-"GLD"
    start<-"2011-03-01"
    end<- format(Sys.Date(),"%Y-%m-%d") # yyyy-mm-dd

    Pull tckr index data from Yahoo! Finance

    getSymbols(tckr, from=start, to=end)

    library(manipulate)
    manipulate(

     plot(as.matrix(Cl(GLD)), type="l", xlim=c(0,x.max)),
     x.max=slider(0,length(as.matrix(Cl(GLD))), step=1)
     y1=mean(Cl(GLD))
     abline(h=y1,col="blue",lty=2)
    

    )

    veepsirtt

  7. Support Staff 8 Posted by Josh Paulson on 03 May, 2011 05:49 PM

    Josh Paulson's Avatar

    Since you are calling multiple commands, you would need to put this into a wrapper function and call that from manipulate. For example:

    example <- function(x.max){
      plot(cars, xlim=c(0,x.max))
      abline(h=mean(cars$dist))
    }
    
    manipulate(
      example(x.max),
      x.max=slider(0,25, step=5)
    )
    

    Josh

  8. 9 Posted by velappan periasamy on 04 May, 2011 12:45 AM

    velappan periasamy's Avatar

    Hello Josh,

    Why the mean value h is not changing as the slider moves from 0 to 25 ?.
    It remains always constant.

    library(manipulate)

    example <- function(x.max){
    plot(cars, xlim=c(0,x.max)) abline(h=mean(cars$dist),col="blue",lty=2) }

    manipulate(
    example(x.max), x.max=slider(0,25, step=5) )

    veepsirtt

  9. Support Staff 10 Posted by Josh Paulson on 04 May, 2011 01:21 PM

    Josh Paulson's Avatar

    The way mean is defined, it is a constant (the mean value of the entire dataset). If you wish to limit it to the current view, you would need to use the slider value and index the data appropriately.

    example <- function(x.max){
      plot(cars, xlim=c(0,x.max))
      abline(h=mean(cars$dist[1:x.max]), col="blue", lty=2)
    }
    
    manipulate(
      example(x.max),
      x.max=slider(0,25, step=5)
    )
    

    All the best,

    Josh

  10. Josh Paulson closed this discussion on 14 May, 2011 07:52 AM.

Comments are currently closed for this discussion. You can start a new one.