IndrajeetPatil / ggstatsplot

Enhancing {ggplot2} plots with statistical analysis 📊📣
https://indrajeetpatil.github.io/ggstatsplot/
GNU General Public License v3.0
2.02k stars 186 forks source link

Problem with Shiny Apps #475

Closed M-Colley closed 3 years ago

M-Colley commented 3 years ago

Hey, I'm trying to use ggwithinstats together with shiny.

set.seed(123)
library(ggstatsplot)
library(shiny)

ui <- fluidPage(
  headerPanel('Examples'),
  sidebarPanel(
    selectInput('xcol','X Variable', choices = names(iris_long)),
    selectInput('ycol','Y Variable', choices =  names(iris_long)),
  selected = names(iris_long)[[2]]),
  mainPanel(
    #plotlyOutput('plot'),
    plotOutput('plot1')
  )
)
server <- function(input, output) { 
  x <- reactive({
    iris_long[,input$xcol]
  })  
  y <- reactive({
    iris_long[,input$ycol]
  })  
  output$plot1 <- renderPlot({
    p <- ggstatsplot::ggwithinstats(
      data = iris_long,
      x = x(),
      y = y(),
      type = "np", # type of statistical test
      outlier.tagging = FALSE, # whether outliers should be flagged
      mean.point.args = list(size = 5, alpha = 0.5, color = "darkblue"),
      p.adjust.method = "bonferroni")
  })
}

shinyApp(ui,server)

The error is

Error in : Only strings can be converted to symbols

I assume that this is due to the ensym function used in the withinstats function (see here).

Do you know a workaround? Maybe something regarding the link?

IndrajeetPatil commented 3 years ago

Yeah, this is a long-standing issue in ggstatsplot. I will try to resolve this before Christmas 🤞

Same as https://github.com/IndrajeetPatil/ggstatsplot/issues/450 and https://github.com/IndrajeetPatil/ggstatsplot/issues/329

IndrajeetPatil commented 3 years ago

What if you do:

 library(rlang)
  output$plot1 <- renderPlot({
    p <- ggstatsplot::ggwithinstats(
      data = iris_long,
      x = !!x(),
      y = !!y(),
      type = "np", # type of statistical test
      outlier.tagging = FALSE, # whether outliers should be flagged
      mean.point.args = list(size = 5, alpha = 0.5, color = "darkblue"),
      p.adjust.method = "bonferroni")
  })

Does it work?

M-Colley commented 3 years ago

Hey, unfortunately, it does not work, I still get the same error:

Error in : Only strings can be converted to symbols

M-Colley commented 3 years ago

Hello, are there any updates on this issue? :) I updated everything (R and relevant packages), but it is still not working. I also tried to find solutions but no luck there....

Kind regards

IndrajeetPatil commented 3 years ago

Sorry, still haven't had time to look at this. I can have a look at the latest in April.

Apologies for not working on this sooner.

Aorus42 commented 3 years ago

It looks like you're passing in a data.frame to the x and y args, rather than the expected variable symbols. No need to create new dataframes, since you already pass in the data via the data argument, just pass in a list of strings as follows:

p <- ggstatsplot::ggwithinstats(
    data = iris_long,
    x = !!sym(input$xcol),
    y = !!sym(input$ycol),
    ...
    )

Does this work?

M-Colley commented 3 years ago

Thanks for the idea, but it does not work:

Warnung: Error in : Problem with `mutate()` input `isanoutlier`.
x (unordered) factors are not allowed
i Input `isanoutlier` is `ifelse(check_outlier(id, outlier.coef), TRUE, FALSE)`.
i The error occurred in group 1: id = "1".

I also tried it with ensym (Warnung: Error in :argmust be a symbol) and without sym (same problem as with sym).

Kind regards!

Aorus42 commented 3 years ago

Have you tried applying the data outside of Shiny? Could you provide a minimum reproducible example? That error sounds like a data type or data preprocessing issue.

M-Colley commented 3 years ago

Well, the problem specifically exists in context with Shiny, which I would want to use. Therefore, an example outside of shiny is not appropriate. The minimum reproducible example is given in the first post.

M-Colley commented 3 years ago

Hey there, I just wanted to quickly check in if there is already an update on this?

I again tried it and there still is the same problem :)

Kind regards!

IndrajeetPatil commented 3 years ago

Here is a minimally reproducible example of how to get this to work:

set.seed(123)
library(ggstatsplot)
library(shiny)
library(rlang)

ui <- fluidPage(
  headerPanel("Example - ggbetweenstats"),
  sidebarPanel(
    selectInput("x", "xcol", "X Variable", choices = names(iris)[5]),
    selectInput("y", "ycol", "Y Variable", choices = names(iris)[1:4])
  ),
  mainPanel(plotOutput("plot"))
)

server <- function(input, output) {
  output$plot <- renderPlot({
    ggbetweenstats(iris, !! input$x , !! input$y)
  })
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:5464

Created on 2021-04-26 by the reprex package (v2.0.0)

M-Colley commented 3 years ago

Thank you very much!!