The following code does not produce a plot (even plots > save as Image in RStudio doesn’t work):
library(plotly)
library(ggplot2)
norme2 = function(x){
return(sqrt(sum(x^2)))
}
simuNormale = function(n) {
u = runif(n)
v = runif(n)
x = sqrt(-2log(u))sin(2piv)
return(x)
}
simuSphere = function(d, n){
X = matrix(nrow = n, ncol = d)
for(i in 1:n){
N = simuNormale(d)
X[i,] = N/norme2(N)
}
return(X)
}
Y = simuSphere(3, 10000)
Y = as.data.frame(Y)
names(Y) <- c(“x”, “y”, “z”)
plot_ly(data = Y, x = ~x, y = ~y, z = ~z, type = “scatter3d”, mode = “markers”)
My specs are platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 5.2
year 2018
month 12
day 20
svn rev 75870
language R
version.string R version 3.5.2 (2018-12-20)
nickname Eggshell Igloo
You use only plotly functions here. ggplot2 doesn't seem to be useful.
I get it to work by adding some * in x = sqrt(-2*log(u))*sin(2*pi*v) and replacing “ by "
library(plotly)
norme2 = function(x) {
return(sqrt(sum(x ^ 2)))
}
simuNormale = function(n) {
u = runif(n)
v = runif(n)
x = sqrt(-2*log(u))*sin(2*pi*v)
return(x)
}
simuSphere = function(d, n) {
X = matrix(nrow = n, ncol = d)
for (i in 1:n) {
N = simuNormale(d)
X[i, ] = N / norme2(N)
}
return(X)
}
Y = simuSphere(3, 10000)
Y = as.data.frame(Y)
names(Y) <- c("x", "y", "z")
plot_ly(data = Y, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers")
Thanks for the help, it now works but I have another issue now: this code is part of my shiny app and the 3D plot is now displayed on my RStudio session but not on the local app, opened in my browser if that makes sense.
So when you click "run app" if you run it in the Rstudio pop-up window the plot displays but not if you click "open in browser"? If that is the case, it may be the browser you are using. Some html widgets do not work well in older browsers...
If it does not show up at all when you click "Run App" either in the RStudio pop-up window or in the browser, then you need to do what @cderv suggested
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: