In Shiny selectInput: to have many columns value et label with vertical align (and spaces not merged)

℡╲_俬逩灬. 提交于 2020-01-06 07:27:13

问题


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)


回答1:


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 :

  • https://shiny.rstudio.com/articles/selectize.html
  • In Shiny can a data frame be used as choices in selectizeInput? (as a reminder about label and value in the dataframe of a selectInput).
  • How to use selectInput to return value of a key corresponding to a label with choices = setNames(df$label, df$value) if the form of data generates an unwanted optgroup. (An isolate() on a reactive() breaks a little the form of the data).


来源:https://stackoverflow.com/questions/53188200/in-shiny-selectinput-to-have-many-columns-value-et-label-with-vertical-align-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!