R functions to HTML

Hi all,

Not sure why there is an issue here. I am trying to convert R functions to HTML and then get the output, But no luck

  output$box <- renderUI({
    fluidRow(column(6,plotlyOutput("p1")),
    column(6,plotlyOutput("p2")))
})

The above code gives me the output, But below HTML code is not though they are same

  output$box <- renderUI({    
    HTML(cat('<div class="row">\n',paste(sapply(c("p1","p2"),function(i) as.character(tags$div(class="col-sm-6", tags$div(id=i, style="width:100%; height:400px;",
                               class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme")))),collapse="\n")))
  })

The reason I am doing like this, these (p1 and p2) are dynamic and it can go on like (p1, p2, p3 and so on) so I created a html out of this

Looks like you have some sort of shinyapp you're building? It is going to be really tuff with only this bit of code to know what is going on. Try and make a reprex and we can try and help ( FAQ: How to do a minimal reproducible example ( reprex ) for beginners )

Sure.

Below reprex. Not sure why method 2 is not working

Method 1 : Working

library(shiny)
library(plotly)
ui <- fluidPage(
  
  htmlOutput("res")
)

server <- function(input, output, session) {
  
  output$res <- renderUI({
    
    ## method 1
    fluidRow(column(3,plotlyOutput("p1")),
    column(3,plotlyOutput("p2")))
    
    ## method 2
    
    # HTML(cat('<div class="row">\n',paste(sapply(c("p1","p2"),
    #                                             function(i) as.character(tags$div(class="col-sm-3",
    #                                                                               tags$div(id=i, style="width:100%; height:400px;",
    #                                                                                class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme")))),
    #                                      collapse="\n")))
  })
  
  output$p1 <- renderPlotly({
    plot_ly()
  })
  
  output$p2 <- renderPlotly({
    plot_ly()
  })
}

shinyApp(ui, server)

Method 2 : Not working (Though both are same)

library(shiny)
library(plotly)
ui <- fluidPage(
  
  htmlOutput("res")
)

server <- function(input, output, session) {
  
  output$res <- renderUI({
    
    ## method 1
    # fluidRow(column(3,plotlyOutput("p1")),
    # column(3,plotlyOutput("p2")))
    
    ## method 2
    
    HTML(cat('<div class="row">\n',paste(sapply(c("p1","p2"),
                                                function(i) as.character(tags$div(class="col-sm-6",
                                                                                  tags$div(id=i, style="width:100%; height:400px;",
                                                                                   class="plotly html-widget html-widget-output shiny-report-size shiny-report-theme")))),
                                         collapse="\n")))
  })
  
  output$p1 <- renderPlotly({
    plot_ly()
  })
  
  output$p2 <- renderPlotly({
    plot_ly()
  })
}

shinyApp(ui, server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.