Plotly doesn't show anything when running from R source file.

Beginner is here, please be gentle :slight_smile:

The following code produces no plotly charts in the Viewer window if I run it from an R-file script. The same code copy&pasted into the Console window, shows chart in the Viewer as it should.

Any help will be greatly appreciated.

library(plotly)

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

fig

I tried reinstalling all from scratch, no difference.

RStudio ver: Win Desktop 1.2.5033
R ver: 3.6.3
Plotly ver: 4.9.2

if you do
fig
after sourcing your script, fig would go the viewer

also if you changed the source call to use parameter echo it would show.

source("myplotly.R",echo = TRUE)
1 Like

Thanks, seems to be working, no clue though why, so counter-intuitive ... :slight_smile:

Can you please explain why the last fig doesn't work from the "source" but the manually issued in the console does? And why "echo = TRUE" solves this...

Theres no reason beyond thats what the source function was designed to behave like, an options like 'echo' are provided to manipulate that behaviour further.

From the documentation on source, (which you can read by typing ?source in the console)

Note that running code via source differs in a few respects from entering it at the R command line. Since expressions are not executed at the top level, auto-printing is not done. So you will need to include explicit print calls for things you want to be printed (and remember that this includes plotting by lattice, FAQ Q7.22)

Well, normal plotting (not plotly) does work via source (into the Plot window though), so why the heck plotly (or Viewer window?) was designed differently!?!?!

I guess, I'll use this opportunity to let the steams out.. I have plenty of experience with C/C++/Java/Matlab/Octave/VB scriptis in Excell, picking up a new scripting language never required more than a couple of hours till I met R... Jeezus... I'll keep trying though, may be the benefits of R outweigh it's awkward semantics and inconsistent design concepts...

Thanks a lot, anyway. you really helped :slight_smile:

/Alex

the plotly package is an r wrapper around an underlying javascript implementation, that might go some way to explaining differences. But you are right, R has plenty of quirks to adapt to.
Vis a vis, the source function, that will give pretty consistent results when you are doing console/terminal things, and want to run things in batch, but as you can imagine things get way more complex when integrating to an IDE, and setting up default behaviours for the interactions therein.

1 Like

on second thought the difference is a bit deeper. plot() function which you can call debug() on and step through, you can see it producing side-effects in the plot window as it goes, and it returns no object , i.e. you could fig<-plot(c(1,2,3) and have anything but fig be NULL, unlikely plot_ly function, which causes no side effects as its stepped through, but will produce a return object, such that you can assign it to fig.
At this point autoprinting defaults become relevant, because in console, stating the name of an object is enought to call a default print method on it, and this is not the default for the source(), although source can be passed params like echo=TRUE to make it behave, like the console... so ... theres that :smiley:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.