生成多个Shiny图表的问题在使用map2时无法解决。

huangapple go评论86阅读模式
英文:

Generating multiple charts in Shiny with map2 is not working

问题

这是我的代码:

  1. library(shiny)
  2. library(gapminder)
  3. ui <- fluidPage(
  4. highchartOutput(outputId = 'chart_1'),
  5. highchartOutput(outputId = 'chart_2'),
  6. highchartOutput(outputId = 'chart_3')
  7. )
  8. server <- function(input, output, session) {
  9. data <- gapminder::gapminder %>% filter(country == 'Chile')
  10. function_chart <- function(x, z) {
  11. output[[paste0('chart_', x)]] <- renderHighchart({
  12. hchart(
  13. data,
  14. 'column',
  15. hcaes(x = year,
  16. y = data[[z]]),
  17. colorByPoint = TRUE
  18. )
  19. })
  20. }
  21. map2(1:3, c('pop', 'lifeExp', 'gdpPercap'), ~ function_chart(x = .x, z = .y))
  22. }
  23. shinyApp(ui, server)

错误可能在'function_chart'函数中,特别是当调用参数z时。输出应该为3个Highchart图表。需要帮助吗?

英文:

This is my code:

  1. library(shiny)
  2. library(gapminder)
  3. ui &lt;- fluidPage(
  4. highchartOutput(outputId = &#39;chart_1&#39;),
  5. highchartOutput(outputId = &#39;chart_2&#39;),
  6. highchartOutput(outputId = &#39;chart_3&#39;)
  7. )
  8. server &lt;- function(input, output, session) {
  9. data &lt;- gapminder::gapminder %&gt;% filter(country == &#39;Chile&#39;)
  10. function_chart &lt;- function(x,z) {
  11. output[[paste0(&#39;chart_&#39;, x)]] &lt;- renderHighchart({
  12. hchart(
  13. data,
  14. &#39;column&#39;,
  15. hcaes(x = year,
  16. y = data[[z]]),
  17. colorByPoint = TRUE
  18. )
  19. })
  20. }
  21. map2(1:3,c(&#39;pop&#39;,&#39;lifeExp&#39;,&#39;gdpPercap&#39;),~ function_chart(x = .x, z = .y))
  22. }
  23. shinyApp(ui, server)

The error is in the function &#39;function_chart&#39; probably when I call the argument z. The output should give me 3 highchart charts.

Any help?

答案1

得分: 1

因为hcaes是惰性评估的,你需要用!!将当前值"z"注入其中。试试:

  1. server <- function(input, output, session) {
  2. data <- gapminder::gapminder %>% filter(country == 'Chile')
  3. function_chart <- function(x,z) {
  4. output[[paste0('chart_', x)]] <- renderHighchart({
  5. hchart(
  6. data,
  7. 'column',
  8. hcaes(x = year,
  9. y = !!z),
  10. colorByPoint = TRUE
  11. )
  12. })
  13. }
  14. map2(1:3,c('pop','lifeExp','gdpPercap'),~ function_chart(x = .x, z = .y))
  15. }
英文:

Because hcaes is lazy evaluated, you need to inject the current value of "z" in there with !!. Try

  1. server &lt;- function(input, output, session) {
  2. data &lt;- gapminder::gapminder %&gt;% filter(country == &#39;Chile&#39;)
  3. function_chart &lt;- function(x,z) {
  4. output[[paste0(&#39;chart_&#39;, x)]] &lt;- renderHighchart({
  5. hchart(
  6. data,
  7. &#39;column&#39;,
  8. hcaes(x = year,
  9. y = !!z),
  10. colorByPoint = TRUE
  11. )
  12. })
  13. }
  14. map2(1:3,c(&#39;pop&#39;,&#39;lifeExp&#39;,&#39;gdpPercap&#39;),~ function_chart(x = .x, z = .y))
  15. }

huangapple
  • 本文由 发表于 2023年2月13日 23:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定