datastorm-open / rAmCharts

API for Amcharts
48 stars 16 forks source link

amBoxplot with formula in shiny not working #84

Closed phineas-pta closed 5 years ago

phineas-pta commented 5 years ago

Hello, I'm writing a shiny app with rAmCharts interactive plot When I use amBoxplot with a formula, the plot is completely wrong

A reproducible example :

library(shiny)
library(rAmCharts)

# generate dataset
set.seed(0)
glass <- data.frame(
    Type = as.factor(sample(1:7, size = 50, replace = TRUE)),
    x = rnorm(50, mean = 5, sd = 2),
    y = rnorm(50, mean = .2, sd = 1),
    z = rnorm(50, mean = -5, sd = .3),
    t = rnorm(50, mean = 0, sd = 2)
)

# user side
ui <- fluidPage(sidebarLayout(
    sidebarPanel(
        radioButtons(
            inputId = "choice",
            choices = c("x", "y", "z", "t"),
            label = "Select a variable:",
            selected = "x"
        )
    ),
    mainPanel(
        amChartsOutput(outputId  = "boxplot1"),
        amChartsOutput(outputId  = "boxplot2")
    )
))

# server side
server <- function(input, output) {
    # 1st plot : no problem
    output$boxplot1 <- renderAmCharts({
        amBoxplot(glass[input$choice])
    })

    # 2nd plot : strange things happen
    output$boxplot2 <- renderAmCharts(
        amBoxplot(input$choice ~ Type, data = glass, export = TRUE)
    )
}

# run app
shinyApp(ui = ui, server = server)

I have also tried get(input$choice) but it's still the same problem

Do you have any idea ?

bthieurmel commented 5 years ago

You have to use a formula, and input$choice is a character, and so :

as.formula(paste0(input$choice, " ~ Type"))

phineas-pta commented 5 years ago

Thank you very much. That's a nice workaround. I can't find any help on the internet about it; Although I still prefer to have the ability to directly use the formula like with the built-in function boxplot. Could I make it a pull request or would you like me to close the issue now ?