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

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

Generating multiple charts in Shiny with map2 is not working

问题

这是我的代码:

library(shiny)
library(gapminder)

ui <- fluidPage(
  highchartOutput(outputId = 'chart_1'),
  highchartOutput(outputId = 'chart_2'),
  highchartOutput(outputId = 'chart_3')
)

server <- function(input, output, session) {
  data <- gapminder::gapminder %>% filter(country == 'Chile')

  function_chart <- function(x, z) {
    output[[paste0('chart_', x)]] <- renderHighchart({
      hchart(
        data,
        'column',
        hcaes(x = year,
              y = data[[z]]),
        colorByPoint = TRUE
      )
    })
  }
  map2(1:3, c('pop', 'lifeExp', 'gdpPercap'), ~ function_chart(x = .x, z = .y))
}

shinyApp(ui, server)

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

英文:

This is my code:

library(shiny)
library(gapminder)   

ui &lt;- fluidPage(

  highchartOutput(outputId = &#39;chart_1&#39;),
  highchartOutput(outputId = &#39;chart_2&#39;),
  highchartOutput(outputId = &#39;chart_3&#39;)

)    
server &lt;- function(input, output, session) {

 data &lt;-  gapminder::gapminder %&gt;% filter(country == &#39;Chile&#39;)

  function_chart &lt;- function(x,z) {    
    output[[paste0(&#39;chart_&#39;, x)]] &lt;- renderHighchart({    
       hchart(
          data,
          &#39;column&#39;,    
          hcaes(x = year,
                y = data[[z]]),
          colorByPoint = TRUE
        )    
      })
  }    
  map2(1:3,c(&#39;pop&#39;,&#39;lifeExp&#39;,&#39;gdpPercap&#39;),~ function_chart(x = .x, z = .y))   

}

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"注入其中。试试:

server <- function(input, output, session) {
  
  data <-  gapminder::gapminder %>% filter(country == 'Chile')
  
  function_chart <- function(x,z) {    
    output[[paste0('chart_', x)]] <- renderHighchart({    
      hchart(
        data,
        'column',    
        hcaes(x = year,
              y = !!z),
        colorByPoint = TRUE
      )    
    })
  }    
  map2(1:3,c('pop','lifeExp','gdpPercap'),~ function_chart(x = .x, z = .y))   
  
}
英文:

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

server &lt;- function(input, output, session) {
  
  data &lt;-  gapminder::gapminder %&gt;% filter(country == &#39;Chile&#39;)
  
  function_chart &lt;- function(x,z) {    
    output[[paste0(&#39;chart_&#39;, x)]] &lt;- renderHighchart({    
      hchart(
        data,
        &#39;column&#39;,    
        hcaes(x = year,
              y = !!z),
        colorByPoint = TRUE
      )    
    })
  }    
  map2(1:3,c(&#39;pop&#39;,&#39;lifeExp&#39;,&#39;gdpPercap&#39;),~ function_chart(x = .x, z = .y))   
  
}

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:

确定