[英]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.问题是
xvar
和yvar
是字符串。 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.