IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 103 forks source link

ggplot graphs don't display when run from the script window? #8677

Open rdstern opened 10 months ago

rdstern commented 10 months ago

I was trying some code for a data presentation. It is from the moderndive package that has added 2 useful geoms.

@Patowhiz this maybe more a question for you, than @lloyddewit because it runs perfectly, but the results, namely the ggplot graphs, run perfectly but no results appear. It is clealy working, because I can save the graph (that works fine) and then the use graph also works and shows the graph fine.

The code is from Patrick's and Antoine's insert dialog for moderndive package, with the geom_parallel_slopes examples. Here it is, but will be quicker to load it yourself.

### Name: geom_parallel_slopes
### Title: Parallel slopes regression model
### Aliases: geom_parallel_slopes

### ** Examples

library(dplyr)
library(ggplot2)
# Load library "moderndive"
library(package="moderndive")

ggplot(evals, aes(x = age, y = score, color = ethnicity)) +
  geom_point() +
  geom_parallel_slopes(se = FALSE)

# Basic usage
ggplot(evals, aes(x = age, y = score, color = ethnicity)) +
  geom_point() +
  geom_parallel_slopes()
ggplot(evals, aes(x = age, y = score, color = ethnicity)) +
  geom_point() +
  geom_parallel_slopes(se = FALSE)

# Supply custom aesthetics
parallel1 <- ggplot(evals, aes(x = age, y = score, color = ethnicity)) +
  geom_point() +
  geom_parallel_slopes(se = FALSE, size = 4)
# Save graph "parallel1"

data_book$add_object(data_name="evals", object_name="parallel1", object_type_label="graph", object_format="image", object=check_graph(graph_object=parallel1))

# Fit non-linear model
example_df <- house_prices %>%
  slice(1:1000) %>%
  mutate(
    log10_price = log10(price),
    log10_size = log10(sqft_living)
  )
ggplot(example_df, aes(x = log10_size, y = log10_price, color = condition)) +
  geom_point(alpha = 0.1) +
  geom_parallel_slopes(formula = y ~ poly(x, 2))

# Different grouping
ggplot(example_df, aes(x = log10_size, y = log10_price)) +
  geom_point(alpha = 0.1) +
  geom_parallel_slopes(aes(fill = condition))
lloyddewit commented 9 months ago

@rdstern I tested this today in PR #8707. Line 32 raises an error because R-Instat tries to create an output object from the line which it cannot do (this is part of a more general problem with handling output from the script window). The rest all seems to work fine (as far as I can see).

image

image

lloyddewit commented 9 months ago

@rdstern It may be quite straightforward to add an extra check in R-Instat so that it doesn't attempt to generate output from databook statements. @Patowhiz could advise on this.

Patowhiz commented 9 months ago

@lloyddewit the error you have posted above seems to suggest that a data frame with the name "evals" data does not exist in the databook. You can only add output objects of data frames that exist in the data book.

@rdstern, adding and getting objects from the databook requires the correct name of an existing data frame to be supplied. Looking at the script you have supplied, I'm cannot see the line that adds "evals" data frame into the data book You could decide to omit the data name parameter so that the object is just added to the data book without any attachment to the data sheet (something which I wouldn't recommend to a user who doesn't have advanced knowledge of the data book). This will mean you can retrieve it without specifying the data frame name.

On Wed, 10 Jan 2024 at 12:10, lloyddewit @.***> wrote:

@rdstern https://github.com/rdstern It may be quite straightforward to add an extra check in R-Instat so that it doesn't attempt to generate output from databook statements. @Patowhiz https://github.com/Patowhiz could advise on this.

— Reply to this email directly, view it on GitHub https://github.com/africanmathsinitiative/R-Instat/issues/8677#issuecomment-1884451975, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFBSU72QBK2DJJ5D77GGRQ3YNZLGRAVCNFSM6AAAAABAEFH74WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBUGQ2TCOJXGU . You are receiving this because you were mentioned.Message ID: @.***>

Patowhiz commented 9 months ago

@lloyddewit and @rdstern, the script window has no knowledge of data book statements that relate to output objects, that is only checked by the R link when you run the scripts. Basically, if you don't have the data book R statements, the R link may call print or capture R functions.

I've written about this somewhere, I would recommend users to use the data book R statements because they align well with using the data book R library. If you use the data book statements the dialogs and different R-Instat windows will help you manage your R objects (data and output objects). That's the only power that R-instat has over R Studio when it comes to executing scripts.

@rdstern I have noticed that most issues you have faced are because of passing incorrect R parameters to the custom data book R functions. Which reminds me that we probably need good documentation of the common data book statements for R users.

On Wed, 10 Jan 2024 at 14:07, Conloop Technologies < @.***> wrote:

@lloyddewit the error you have posted above seems to suggest that a data frame with the name "evals" data does not exist in the databook. You can only add output objects of data frames that exist in the data book.

@rdstern, adding and getting objects from the databook requires the correct name of an existing data frame to be supplied. Looking at the script you have supplied, I'm cannot see the line that adds "evals" data frame into the data book You could decide to omit the data name parameter so that the object is just added to the data book without any attachment to the data sheet (something which I wouldn't recommend to a user who doesn't have advanced knowledge of the data book). This will mean you can retrieve it without specifying the data frame name.

On Wed, 10 Jan 2024 at 12:10, lloyddewit @.***> wrote:

@rdstern https://github.com/rdstern It may be quite straightforward to add an extra check in R-Instat so that it doesn't attempt to generate output from databook statements. @Patowhiz https://github.com/Patowhiz could advise on this.

— Reply to this email directly, view it on GitHub https://github.com/africanmathsinitiative/R-Instat/issues/8677#issuecomment-1884451975, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFBSU72QBK2DJJ5D77GGRQ3YNZLGRAVCNFSM6AAAAABAEFH74WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBUGQ2TCOJXGU . You are receiving this because you were mentioned.Message ID: @.***>

rdstern commented 8 months ago

I am closing this issue. I still occasionally have the problem that graphs don't appear from script windows. But it is general, and I don't know (yet) what changes the behavior, so the graphs are not displayed.

They are always displayed when I start an R-Instat session - and I must be doing something to switch them off.

However, the discussion here is not helping. Hence I am closing.

Patowhiz commented 8 months ago

@rdstern did you have a look at my explanations above