在Shiny中,选择输入:使多列值和标签垂直对齐(空格不合并)



我想垂直对齐数据帧中的一些文本。

因此,我希望在数据帧中保留多个空间。

toto<-'A    B'
toto
df <- data.frame(name=character(), stringsAsFactors=FALSE)
df[1,"name"]<-toto
df

但我最后只有一个空位:

'A    B'
name
A B

(我到处找过(

最初的目标是selectInput()。我把标题改了一点。

这是我很快找到的解决方案。

我还没有找到像DT::datatable(df,escape=1)escape那样漂亮的解决方案。

ui.R

用于垂直对齐的字体系列单空格。

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

服务器.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
  • 在Shiny中,数据帧可以用作selectizeInput中的选项吗?(作为关于selectInput的数据帧中的标签和值的提醒(
  • 如果数据形式生成不需要的optgroup,如何使用selectInput返回与具有choices = setNames(df$label, df$value)的标签对应的键的值。(reactive()上的isolate()稍微破坏了数据的形式(

最新更新