简体   繁体   中英

How to make an interactive chart for varying input variables using highcharter in shiny

In R, I would like to create a highcharter chart which displays varying combinations based on the user input from a select input field. However, I keep on failing with this.

I have got two input fields where both input fields have two opportunities. The user can select either combination and the selected variable should be used for a scatter plot in Highcharter. I tried quite some opportunities however it does not work.

xvar <- input$varx
yvar <- input$vary

hc <-   chart_df %>%
    hchart('scatter',
       hcaes(x = xvar,
             y = yvar,
             names = xvar),
       dataLabels = list(enabled = TRUE,
                         format = '{point.names}')
)

However, it does not recognize the assigned xvar and yvar. The data frame chart_df contains the respective columns. How could I solve this? Any help would be appreciated!

The issue is that xvar and yvar are strings. To make your code work you have to convert them to symbols using eg !!sym() or use hcaes_string which like ggplot2::aes_string allows you to pass variables as characters.

Using mtcars as example data set try this:

library(shiny)
library(highcharter)
library(rlang)

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            selectInput("varx",
                        "varx",
                        choices = names(mtcars),
                        selected = "hp"),
            selectInput("vary",
                        "vary",
                        choices = names(mtcars),
                        selected = "mpg")
        ),

        # Show a plot of the generated distribution
        mainPanel(
            highchartOutput("highchart"),
            highchartOutput("highchart2")
        )
    )
)

server <- function(input, output) {

    output$highchart <- renderHighchart({
        
        xvar <- input$varx
        yvar <- input$vary
        
        hc <-   mtcars %>%
            hchart('scatter',
                   hcaes(
                       x = !!sym(xvar),
                       y = !!sym(yvar),
                       names = !!sym(xvar)),
                   dataLabels = list(enabled = TRUE,
                                     format = '{point.names}')
            )
    })
    
    output$highchart2 <- renderHighchart({
        
        xvar <- input$varx
        yvar <- input$vary
        
        hc <-   mtcars %>%
            hchart('scatter',
                   hcaes_string(
                       x = xvar,
                       y = yvar,
                       names = xvar),
                   dataLabels = list(enabled = TRUE,
                                     format = '{point.names}')
            )
    })
}

shinyApp(ui = ui, server = server)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM