0

I would like to vertical align some text in my dataframe.

Therefore I would like kept more than one space in dataframe.

toto<-'A    B'
toto

df <- data.frame(name=character(), stringsAsFactors=FALSE)

df[1,"name"]<-toto
df

but I get only one space at the end:

'A    B'
name
A B

(I have searched everywhere)

phili_b
  • 885
  • 9
  • 27
  • that code works fine. Try using the reprex package to post your code https://github.com/tidyverse/reprex – hrbrmstr Nov 07 '18 at 11:05
  • It's a reprex : I get one space and I would like keep the spaces, like my code copy-pasted here :) I don't understand your comment – phili_b Nov 07 '18 at 11:20
  • It occures in Shiny but also in Jupyter. It's for a select input with value and label, but It's also a general question : why spaces are stripped in dataframe by default ? So this simple code. – phili_b Nov 07 '18 at 11:24
  • R version 3.4.4 – phili_b Nov 07 '18 at 11:27
  • 1
    I see multiple spaces in the output. – user2554330 Nov 07 '18 at 12:07
  • spaces aren't stripped "by default" in data frames. You have not provided a true reproducible example. Anyone that runs the code in the first block gets the string with the spaces – hrbrmstr Nov 07 '18 at 12:07
  • When I print I get the spaces, when I `View(df)` spaces are removed. – Andre Elrico Nov 07 '18 at 12:08
  • try `toto <- gsub(" "," ",toto)` before running your shiny code (and edit your post to reflect that this happens in your shiny app but not in interactive mode). – moodymudskipper Nov 07 '18 at 12:49
  • ah yes ?!. With `R -i` and RStudio I have the spaces, with Jupyter and Shiny I have only one space. So it's html issue oO. I look forward. – phili_b Nov 07 '18 at 13:13

1 Answers1

2

The initial goal was for selectInput(). I change a little the title.

Here is quickly the solution I've found.

I've not found a beautiful solution like escape of DT::datatable(df,escape=1).

With

ui.R

font-family monospace for vertical align.

fluidRow(
  div(selectInput("outputmyselectlist",
      label=c("Filtre"),
      choices=NULL,
      width = '75%'
      )
  ,style='font-family: Consolas,monospace;')
) 

server.R

updateSelectizeInput(session, "outputmyselectlist",
   server = TRUE, 
   choices = df,
   options = list(render = I(
   '{
       option: function(item, escape) {
         return "<div    data-value=\\""+ 
         escape(item.value)+
         "\\"   data-selectable=\\"\\" class=\\"option\\"   >" +
         (item.label.replace(/ /g, "&nbsp;")) +
         "</div>"
       }
    }'))
)

with my dataframe like that:

df0<-data.frame(value=c("a","b"), label=c("AAA","BBB"),stringsAsFactors = FALSE)
df<-df0 %>% mutate ( label=paste0(value,strrep(' ',14-nchar (value)),'|',label))

Reproducible example :


library("shiny")
library("dplyr")
library("tidyr")
ui <- fluidPage(
  tags$style(type = "text/css", 
             HTML(".label_non_fixe_items_fixes .selectize-control {font-family: Consolas,monospace;})")),
  div(
    uiOutput("myselectinputUI"),
    class='label_non_fixe_items_fixes')
)


server <- function(input, output, session) {
  mydata.list <- reactive  ({
    (mtcars 
     %>% mutate (
       myid = row_number(), 
       myname = rownames(mtcars)
     ) %>% select (myid, myname,hp)
    )
  })


  output$myselectinputUI <- renderUI({
    res <-( mydata.list() 
            %>% transmute (value=myid, 
                           label= paste0(myid,
                                         strrep(' ',2-nchar (myid)),
                                         '|',myname,
                                         strrep(' ',20-nchar (myname)),
                                         '|',hp
                           )
            )
    )
    list_label_value = setNames(res$value, res$label)

    selectizeInput(
      inputId="myselectinputUI",
      label= "my select input",
      choices = list_label_value,
      options = list(
        render = I(
          '{
                       option: function(item, escape) {
                           return "<div    data-value=\\""+
                           escape(item.value)+
                           "\\" data-selectable=\\"\\" class=\\"option\\" >" +
                           (item.label.replace(/ /g, "&nbsp;")) +
                           "</div>"
                       }
                      }'
        )
      )
    )
  })



}

shinyApp(ui = ui, server = server)

Links :

phili_b
  • 885
  • 9
  • 27