将变量名称传递给制作图表的用户定义函数
Passing variable name to user-defined function that makes a plotly chart
我在将变量名从共享数据集传递到我定义的函数时遇到问题。
我看过类似 R 问题的文档,并尝试实施一些解决方案但无济于事。
在下面的 reprex 中,我遇到错误“eval(expr, data, expr_env) 错误:找不到对象 'pct_increase'”
有人对此有解决方案吗?非常感谢。
代表:
library(tidyverse)
library(plotly)
library(crosstalk)
bar_tibble <- tibble(category = c("Cat 1", "Cat 1", "Cat 2", "Cat 2"),
pct_increase = c(0.17, 0.25, 0.64, 0.85),
week = c(1, 2, 1, 2))
bar_data_shared <- SharedData$new(bar_tibble, ~week)
SharedData$origData(bar_tibble)
make_bar_chart <- function(shared_data, xvar, yvar){
plot_ly(data = shared_data, hoverinfo = "none") %>%
add_trace(
x = ~xvar,
y = ~yvar,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~yvar,
aggregations = list(
list(
target = "x", func = "avg", enabled = T)))))
}
make_bar_chart(bar_data_shared, pct_increase, category)
当你调用你的函数时,你必须使用真实的对象或字符串。换句话说,您必须将列名作为字符串传递。
从那里你 运行 进入另一个问题。一旦你创建了列名字符串,Plotly 将不会在没有额外干预的情况下将列连接到 SharedData
对象。
因为我不认为你想要参数名称作为轴的标签,所以我也添加了标签。
您可以执行以下操作来完成这项工作。
make_bar_chart <- function(shared_data, xvar, yvar){
plot_ly(data = shared_data,
hoverinfo = "none") %>%
add_trace(
x = ~.data[[xvar]], # <- tell plotly 'connect to the data object!'
y = ~.data[[yvar]], # <- here, too
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~.data[[yvar]], # <- here, too
aggregations = list(
list(
target = "x", func = "avg", enabled = T))))
) %>%
layout(xaxis = list(title = xvar),
yaxis = list(title = yvar))
}
# pass non-environment variables as strings
make_bar_chart(bar_data_shared, "pct_increase", "category")
我在将变量名从共享数据集传递到我定义的函数时遇到问题。
我看过类似 R 问题的文档,并尝试实施一些解决方案但无济于事。
在下面的 reprex 中,我遇到错误“eval(expr, data, expr_env) 错误:找不到对象 'pct_increase'”
有人对此有解决方案吗?非常感谢。
代表:
library(tidyverse)
library(plotly)
library(crosstalk)
bar_tibble <- tibble(category = c("Cat 1", "Cat 1", "Cat 2", "Cat 2"),
pct_increase = c(0.17, 0.25, 0.64, 0.85),
week = c(1, 2, 1, 2))
bar_data_shared <- SharedData$new(bar_tibble, ~week)
SharedData$origData(bar_tibble)
make_bar_chart <- function(shared_data, xvar, yvar){
plot_ly(data = shared_data, hoverinfo = "none") %>%
add_trace(
x = ~xvar,
y = ~yvar,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~yvar,
aggregations = list(
list(
target = "x", func = "avg", enabled = T)))))
}
make_bar_chart(bar_data_shared, pct_increase, category)
当你调用你的函数时,你必须使用真实的对象或字符串。换句话说,您必须将列名作为字符串传递。
从那里你 运行 进入另一个问题。一旦你创建了列名字符串,Plotly 将不会在没有额外干预的情况下将列连接到 SharedData
对象。
因为我不认为你想要参数名称作为轴的标签,所以我也添加了标签。
您可以执行以下操作来完成这项工作。
make_bar_chart <- function(shared_data, xvar, yvar){
plot_ly(data = shared_data,
hoverinfo = "none") %>%
add_trace(
x = ~.data[[xvar]], # <- tell plotly 'connect to the data object!'
y = ~.data[[yvar]], # <- here, too
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~.data[[yvar]], # <- here, too
aggregations = list(
list(
target = "x", func = "avg", enabled = T))))
) %>%
layout(xaxis = list(title = xvar),
yaxis = list(title = yvar))
}
# pass non-environment variables as strings
make_bar_chart(bar_data_shared, "pct_increase", "category")