Kid_Learning_C
Kid_Learning_C

Reputation: 3623

How to iteratively form text lines to display in R shiny app?

In my Rshiny app, i need to display sentences(strings) on a dashboard. The sentences are created in another process and stored in a variable named sentences. I need to form multiple lines according to the number of strings stored in sentences and display them on the visualization dashboard. As a silly coding practice I did the following:

if (length(sentences) == 1){
  lines_to_display <- helpText(tags$ol(tags$li(sentences[[1]])))  
} else if (length(sentences) == 2){
  lines_to_display <- helpText(tags$ol(tags$li(sentences[[1]]), 
                                    tags$li(sentences[[2]])
                                    ))  
} else if (length(sentences) == 3){
  lines_to_display <- helpText(tags$ol(tags$li(sentences[[1]]), 
                                    tags$li(sentences[[2]]),
                                    tags$li(sentences[[3]])
                            ))  
} else if (length(sentences) == 4){
  lines_to_display <- helpText(tags$ol(tags$li(sentences[[1]]), 
                                    tags$li(sentences[[2]]),
                                    tags$li(sentences[[3]]),
                                    tags$li(sentences[[4]])
                            ))  
  }

So the lines_to_display would contain the strings to be displayed and they are used in:

conditional_panel_citys <- conditionalPanel(  "$('li.active a').first().html()==='city data visualization'",
                                              sidebarPanel( 
                                                            width = 3,
                                                            sidebar_style,
                                                            helpText("Our key observations:"),
                                                            lines_to_display)

This is very silly as length(sentences) may get up to 10. Is there a smart way to iterate through sentences and load all strings into lines_to_display in a line-by-line manner?

Upvotes: 0

Views: 38

Answers (1)

Chabo
Chabo

Reputation: 3000

A for loop may be your friend here, see below

Test<-list("AAAAAAA",
           "BBBBBBBB",
           "CCCCCCC",
           "DDDDDDD",
           "EEEEEEEE",
           "FFFFFFFF")

lines_to_display<-list()

Length<-length(Test)

for( i in 1:Length){
  Testy<-Test[[i]]
  lines_to_display[[i]]<- helpText(tags$ol(tags$li(Testy)))}  


> lines_to_display
[[1]]
<span class="help-block">
  <ol>
    <li>AAAAAAA</li>
  </ol>
 </span>

[[2]]
<span class="help-block">
  <ol>
    <li>BBBBBBBB</li>
  </ol>
</span>

[[3]]
<span class="help-block">
  <ol>
    <li>CCCCCCC</li>
  </ol>
</span>

[[4]]
<span class="help-block">
  <ol>
    <li>DDDDDDD</li>
  </ol>
 </span>

And so on...

Upvotes: 1

Related Questions