Home > Back-end >  Something unwanted extra, when creating Shiny UI elements (Shiny Dashboard boxes) in using a server
Something unwanted extra, when creating Shiny UI elements (Shiny Dashboard boxes) in using a server

Time:10-06

I am testing out to way of creating Shiny UI elements dynamically with a loop from the server side in a way that the user could control how many elements are actually produced. In my case the element is Shiny Dashboard box with two dropdown menus and one button. Everything works fine, except something extra is printed out as you can see from the image: Unwanted extra text next to the boxes

My ui.r looks as follows:

library(shiny)
library(shinydashboard)
shinyUI(dashboardPage(
    dashboardHeader(title = 'The Box Experiment'),

    # Sidebar with a slider input for number of bins
    dashboardSidebar(
        
            sliderInput("numberOfBoxes",
                        "Number of boxes:",
                        min = 1,
                        max = 50,
                        value = 5)
        ),
    dashboardBody(uiOutput("boxes"))
    )        
    )

...and server.r looks as follows:


library(shiny)
library(shinydashboard)

shinyServer(function(input, output) {

    output$boxes <- renderUI({
        
        boxlist = c()
        for(i in 1:input$numberOfBoxes) {
            ddmenu1 <- selectInput(paste0("ddmenu1_in_box",i), "Animal", list('cat', 'dog', 'rabbit'))
            ddmenu2 <- selectInput(paste0("ddmenu2_in_box",i), "Color", list('red', 'blue', 'green'))
            button <- actionButton(paste0("justabutton_in_box",i), "Click me!")
            boxlist <- c(boxlist,column(1, box(ddmenu1, ddmenu2, button)))
        }
        
        boxlist

    })

})

So where does this "div col-sm-1" times the number of boxes crap come from, and how do I get rid of it?

CodePudding user response:

I'd recommend working with lapply rather than using a for-loop.

result

CodePudding user response:

Since, the "crap" lies somewhere in the list object, I decided to take a closer look at it.

So I developed this "hack" to overwrite the text with empty string:

ui.r (No modifications)

library(shiny)
library(shinydashboard)
shinyUI(dashboardPage(
    dashboardHeader(title = 'The Box Experiment'),

    # Sidebar with a slider input for number of bins
    dashboardSidebar(
        
            sliderInput("numberOfBoxes",
                        "Number of boxes:",
                        min = 1,
                        max = 50,
                        value = 5)
        ),
    dashboardBody(uiOutput("boxes"))
    )   
    )

server.r (This time includes a loop to overwrite unwanted strings)

library(shiny)
library(shinydashboard)

shinyServer(function(input, output) {

    output$boxes <- renderUI({
        
        boxlist = list()
        for(i in 1:input$numberOfBoxes) {
            ddmenu1 <- selectInput(paste0("ddmenu1_in_box",i), "Animal", list('cat', 'dog', 'rabbit'))
            ddmenu2 <- selectInput(paste0("ddmenu2_in_box",i), "Color", list('red', 'blue', 'green'))
            button <- actionButton(paste0("justabutton_in_box",i), "Click me!")
            boxlist <- append(boxlist,(column(1, box(ddmenu1, ddmenu2, button))))
        }
        
        #Let's go through every attribute
        for(i in 1:length(attributes(boxlist)$names)) {
            #If the attribute name is NOT "children"
            if(attributes(boxlist)$names[i] != "children") {
                #...and the length of corresponding variable "name" equals one (text string)...
                if(length(boxlist[i]$name) == 1) {
                    boxlist[i]$name <- ''
                }
                #...and the length of corresponding variable "attribs$class" equals one (text string)...
                if(length(boxlist[i]$attribs$class) == 1) {
                    boxlist[i]$attribs$class <- ''
                }
            }
        }
         
        boxlist

    })

})

I honestly think that this is a wrong way of doing this, and there has to be a better way to proceed, but until someone post it here, this seems to be the way to go. At least the crap is gone: No crap anymore.

  • Related