简体   繁体   English

如何在 shiny 中使用 highcharter 为不同的输入变量制作交互式图表

[英]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.在 R 中,我想创建一个 highcharter 图表,它根据来自 select 输入字段的用户输入显示不同的组合。 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.用户可以 select 任一组合,所选变量应用于 Highcharter 中的散点图 plot。 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.但是,它不识别分配的 xvar 和 yvar。 The data frame chart_df contains the respective columns.数据框 chart_df 包含相应的列。 How could I solve this?我该如何解决这个问题? Any help would be appreciated!任何帮助,将不胜感激!

The issue is that xvar and yvar are strings.问题是xvaryvar是字符串。 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.为了使您的代码工作,您必须使用例如!!sym()或使用hcaes_string将它们转换为符号,例如ggplot2::aes_string允许您将变量作为字符传递。

Using mtcars as example data set try this:使用mtcars作为示例数据集试试这个:

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 shiny 制作交互式图例 label? - How to make interactive legends label using shiny? 您如何使用 highcharter 包在 R 闪亮的折线图中提供多个系列而不对系列进行硬编码? - How could you supply multiple series to a line chart in R shiny using highcharter package and without hardcoding the series? 使用Shiny制作箱形图互动 - Make a boxplot interactive using Shiny 如何使用 highcharter 在 r 中创建工具提示图表? - How to create a tooltip chart in r using highcharter? 如何使用 Shiny 和 Rmarkdown 制作交互式 isoslides 演示文稿? - How to make an interactive isoslides presentation using Shiny and Rmarkdown? 如何制作用户在闪亮或 flexdahsboard 中选择的变量的图表? - How to make a plotly chart of variables selected by a user in shiny or flexdahsboard? 通过reactValues()使用highcharter和r shine - Using highcharter and r shiny with reactiveValues() 使用 plotly 和闪亮的颜色和后退按钮制作交互式条形图 - make a interactive bar chart using plotly and shiny have color and a back button 在Shiny App中呈现Highcharter饼图时出错 - Error in rendering Highcharter Pie chart in Shiny App selectInput 变量影响图表输出 - 组参数? 最好使用 highcharter 输出 [Shiny app] - selectInput variable to influence chart output - group argument? Preferably using highcharter output [Shiny app]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM