Mus
Mus

Reputation: 7530

Why is my chart not showing in RStudio's "Plots" pane?

I have written several functions that connect to a server and gather some data and mutates those data. One of the functions visualises the data also.

As I always need to run all six functions - and because I wrote them as and when I needed them - I decided it would be more efficient to combine them into a single, larger function (~400 lines, possibly going down to ~350 soon after some cleansing) rather than having to run six separate functions independently one after the other.

Before combing the functions, the one that produced the visualisation worked perfectly in that it showed up in the Plots window in RStudio.

Since combining the functions, the visualisation is not showing up.

I added a line to show whether or not the chart has been generated successfully, and it always is; however, I don't see the output any more.

Here is the code for the chart:

if(chart == TRUE){
    ggplot(plugin_displays[plugin_displays$date <= end_date, ], aes(fill = type)) +
      geom_col(aes(x = time, y = count),
               colour = "black",
               position = "stack") +
      geom_text(aes(x = time, y = total, label = total),
                hjust = -0.15,
                vjust = 0.15,
                size = 3,
                colour = "black",
                angle = 90) +
      scale_y_continuous(limits = c(0, max(plugin_displays$total) * 1.02)) +
      labs(title = paste0("Plugin Loads & Unloads ", start_date, " - ", end_date),
           x = "Time",
           y = "Count") +
      facet_grid(.~date) +
      scale_fill_manual(values=c("forestgreen", "red")) +
      theme(plot.title = element_text(size = 25,
                                      face = "bold",
                                      colour = "black"),
            panel.grid.minor.x = element_blank(),
            panel.grid.major.x = element_blank(),
            axis.text = element_text(colour = "black",
                                     angle = 90))
    print("Chart printed.")
  } else {
    print("Chart not shown by default.  Set chart = TRUE to display chart")
  }

By default, chart is set to FALSE as a chart isn't always needed. To generate the chart, a user specifies either TRUE or chart = TRUE when calling the function and it generates the graph (using an if/else); otherwise, it is simply a waste of processing power.

I thought that perhaps the order of processes occurring would have affected the output, so I moved the plot down to the bottom of the script and it still doesn't show up in the Plots pane, despite the "Chart printed." message showing.

The function as a whole executes perfectly and the desired output is precisely what I want it to be.

There are no errors at any point during the execution phase and the data frames are all as expected, with nothing wrong.

If I split the visualisation function out, it works independently with no issues. If I put it back into the function, it still executes successfully but there is no visualisation.

What could be causing this?

UPDATE (6TH JUNE 2018):

Somebody suggested wrapping the plot in print. This has worked in the sense that the plot now appears, albeit in a new window - this isn't what I want, which is for the plot to remain within the RStudio IDE.

Upvotes: 3

Views: 15056

Answers (2)

masher
masher

Reputation: 4096

I had the same issue. It turns out that I had some devices that were silently taking the plots.

dev.list()

will give a list of things that are open. I had pdf 8 pdf 9.

dev.off(8)
dev.off(9)

closed those devices

dev.list()

then said null device 1 and then my plots started appearing back in the view pane.

Upvotes: 8

Mus
Mus

Reputation: 7530

It seems that the error lies in R/RStudio and not in my code.

After some exploration across different forums, I tried reinstalling R (I have version 3.5.0) and that worked for me.

I simply downloaded the latest version (which I have/had anyway), then closed RStudio and installed R (without removing my previous/current version).

I then restarted RStudio and it worked for me; plots are now appearing in the Plots pane within RStudio.

Upvotes: 0

Related Questions