How to display Markov Chain plots in Rstudio (markovchain package) - r

Latest Rstudio and latest R ( in Ubuntu Linux 12.04 - 32bit ).
Most plots will display OK in my Rstudio [Plots] pane.
But some Markov Chain plots throw an Error and don't display.
Here's a very simple example code:
library(markovchain) #loading the package
myMatr<-matrix(c(0,.2,.8,.1,.8,.1,.3,0,.7),byrow=TRUE,nrow = 3)
## defining a transition matrix
rownames(myMatr)<-colnames(myMatr)<-c("a","b","c")
myMc<-as(myMatr, "markovchain")
plot(myMc) # <== Error message starts after this...
gives this Rstudio Console Error:
(and no plot is displayed in Rstudio)
"Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE, : 'x' must be numeric"
Yet, exactly the same R commands (as above)
work fine in "stand-alone" R 3.2.3! (outside of Rstudio).
And the Markov Chain plot is displayed ok in a new R-window...
Q:
- Is there an easy way/command to tell Rstudio
that I want certain plots
to open in a new R window (just like in pure R),
and NOT in the Rstudio Plot pane?
- Or some other easy solution?

Related

how do I create a probability plot in R using R-studio

I want to create a lognormal (or other distribution) probability plot in R (for R-studio). I have looked around on the web for an example but none of the examples tell me what package I need to install in order to use the function.
logn_prob_plot <- function()
{
x<-rlnorm(10,5,1)
x
probplot(x,qdist=qlnorm,xlab="failure time",ylab="lognormal probability")
}
Error in probplot(x, qdist = qlnorm, xlab = "failure time", ylab = "lognormal probability") : could not find function "probplot"
Writing up the comment thread as an answer:
The error (could not find function "probplot") is showing up because a necessary package is not installed. It's not specifically related to creating a probability plot.
Googling "r probplot" turns up the documentation for the package e1071, which is available in CRAN.
The package can be installed by entering install.packages("e1071") in your terminal or by selecting Tools -> Install Packages in the RStudio GUI. You can then load that package using library("e1071").

Rstudio - plots not showing and plots menu grayed out

I'm using Rstudio Version 1.0.136. I'm trying to plot some data and when I execute the code nothing shows up in the 'Plot' pane and the 'Plots' drop down menu is grayed out. Any suggestions?
EDIT:
> dev.off()
null device
1
> dev.cur()
null device
1
I tried restarting R and then ran this code and got this error message:
> library(ggplot2);qplot(mpg, hp, data = mtcars)
Warning message:
R graphics engine version 12 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.

Trying to make movie of 3D PCA plot (rgl) using movie3d

I have made a rgl 3D PCA plot in R using the pca3d package, and I am trying to make a gif file containing a movie of the rotating plot using movie3d. There is a function in the pca3d package (makeMoviePCA), that passes its arguments to movie3d. Here is the command I am using:
pca3d(pc, components = 1:3, title=TRUE, col=col_conditions, radius=2)
makeMoviePCA(dir="/Users/workdir", clean=TRUE, type = "gif", movie = "movie", convert=TRUE)
This plays the movie like I want to, but does not save it into a gif file, and displays the following error:
Writing 'movie100.png'
...
Writing 'movie120.png'
Error in system("convert --version", intern = TRUE) :
error in running command
sh: convert: command not found
EDIT: I fixed this by telling R where "convert" is by using
Sys.setenv(PATH=paste("/opt/local/bin", Sys.getenv("PATH"), sep=":"))
Thanks!
I fixed this by telling R where "convert" is by using
Sys.setenv(PATH=paste("/opt/local/bin", Sys.getenv("PATH"), sep=":"))

R not producing a figure in jupyter (IPython notebook)

I am very excited about using python and R together and tried using R in Jupyter (ipython notebbok), however, I could not generate figures in the R kernel.
The error message shown below is displayed when I try to plot figures. I highly appreciate your help.
I am using Anaconda and windows 8.
x<-1:10
plot(x)
Error in png(tf, width, height, "in", pointsize, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
Error in jpeg(tf, width, height, "in", pointsize, quality, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
The following is displayed in the Anaconda command prompt:
Unable to load 'C:/Users/aa/Anaconda/R/library/grDevices/libs/x64/winCairo.dll'
I found a solution from the post on this group.
Solution I did:
I had the most release of R (R 3.2.0) and following the discussion in the above link, I installed R 3.1.3 and copied winCairo.dll from C:\Program Files\R\R-3.1.3\library\grDevices\libs\x64 to C:\Anaconda\R\library\grDevices\libs\x64.
Copying winCairo.dll from R 3.2.0 does not work for my case.
With this, it plots a figure but with an error message that says "Error in replayPlot(obj): invalid graphics state"
Then, thanks to this link,
I added 'options(jupyter.plot_mimetypes = 'image/png')' and it works fine.
options(jupyter.plot_mimetypes = 'image/png')
x=1:10
plot(x)

RStudio-only error with tcl/tk?

When I compute an ROC plot with the package pROC, I get the following error in RStudio v. 99.9.9:
## Loading required package: tcltk
## Warning: couldn't connect to display ":0"
## Error: [tcl] invalid command name "toplevel".
I thought this was an issue in my OS (Debian with r-base 3.1 installed from APT depending on tcl/tk lib version 8.5), but when evaluating the same code through the terminal, I get the plot rendered without the same error.
Here is a simple example following some pROC example code that will generate the error when knitted in RStudio, but works in vanilla R:
```{r markdown.test}
library(pROC)
features <- runif(25)
labels <- as.factor(runif(25) < 0.75)
lda.model <- lda(as.matrix(features),labels)
lda.pred <- predict(lda.model,as.matrix(features))
lda.roc <- plot.roc(lda.pred$posterior[,1],
c(labels),
ci=TRUE, of="se", #specificities = seq(0,100,5),
ci.type="shape",
ci.col="#1c61b6AA")
```
The error occurs when the confidence interval parameters are added to the call.
Is there something missing from my RStudio install/config, or is this a bug?
Blast from the past! I see that same error today on an instance of RStudio installed in the IBM computing environment called CP4D. Did you ever find an answer?
I think the way to fix on your Macintosh is to install Quartz and let it serve as the X11 server to display output from programs like tcltk or maybe even hist() or quartz() devices. (my team wrote install instructions https://pj.freefaculty.org/guides/crmda_guides/47.mac_R_setup).
I'm not sure what I'll do on the CP4D thing, since it is running inside a Web browser.

Resources