lapply to plotlyoutput with FluidRow

Hi all,

I am trying to apply lapply function as shown below

Hardcoded


  fluidRow(column(6,plotlyOutput("A")),
           column(6,plotlyOutput("B")))

<div class="row">
  <div class="col-sm-6">
    <div id="A" style="width:100%; height:400px; " class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme"></div>
  </div>
  <div class="col-sm-6">
    <div id="B" style="width:100%; height:400px; " class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme"></div>
  </div>
</div>

Instead of hard coding as shown above, I am applying lapply as below, but I need as above

cname_pas <- c("A","B")
asd <- lapply(cname_pas, plotlyOutput)
asd
[[1]]
<div id="A" style="width:100%; height:400px; " class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme"></div>

[[2]]
<div id="B" style="width:100%; height:400px; " class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme"></div>

Like this:

cname_pas <- c("A","B")
fluidRow(
  lapply(cname_pas, function(id) {
    column(6, plotlyOutput(id))
  })
)

Or you can use purrr::map in place of lapply, and make the code more compact:

cname_pas <- c("A","B")
fluidRow(
  map(cname_pas, ~ column(6, plotlyOutput(.)))
)
1 Like