在Shiny selectInput中:具有垂直对齐(且未合并空格)的许多列值和标签 [英] In Shiny selectInput: to have many columns value et label with vertical align (and spaces not merged)

查看:139
本文介绍了在Shiny selectInput中:具有垂直对齐(且未合并空格)的许多列值和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据框中垂直对齐某些文本。

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

(我到处搜索过)

推荐答案

最初的目标是 selectInput()。我稍微改变了标题。

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

这是我找到的解决方案。

Here is quickly the solution I've found.

我已经找到了找不到像 DT :: datatable(df,escape = 1) escape 这样漂亮的解决方案。

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

使用

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>"
       }
    }'))
)

与我的数据框类似,

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

可重现的示例:


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)

链接:

  • 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).

这篇关于在Shiny selectInput中:具有垂直对齐(且未合并空格)的许多列值和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆