在 Shiny selectInput 中:有许多列 value et label 垂直对齐(和空格不合并)
In Shiny selectInput: to have many columns value et label with vertical align (and spaces not merged)
我想垂直对齐数据框中的一些文本。
因此我想在数据框中保留多个 space。
toto<-'A B'
toto
df <- data.frame(name=character(), stringsAsFactors=FALSE)
df[1,"name"]<-toto
df
但最后我只得到一个 space:
'A B'
name
A B
(我到处都找过了)
最初的目标是 selectInput()
。我稍微改一下标题。
这是我找到的快速解决方案。
我还没有找到像 DT::datatable(df,escape=1)
中的 escape
这样漂亮的解决方案。
与
ui.R
font-family 垂直对齐等宽。
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, " ")) +
"</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, " ")) +
"</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?(作为对
selectInput
数据框中标签和值的提醒)。
- How to use selectInput to return value of a key corresponding to a label 和
choices = setNames(df$label, df$value)
如果数据形式生成不需要的 optgroup
。 (reactive()
上的 isolate()
稍微破坏了数据的形式)。
我想垂直对齐数据框中的一些文本。
因此我想在数据框中保留多个 space。
toto<-'A B'
toto
df <- data.frame(name=character(), stringsAsFactors=FALSE)
df[1,"name"]<-toto
df
但最后我只得到一个 space:
'A B'
name
A B
(我到处都找过了)
最初的目标是 selectInput()
。我稍微改一下标题。
这是我找到的快速解决方案。
我还没有找到像 DT::datatable(df,escape=1)
中的 escape
这样漂亮的解决方案。
与
ui.R
font-family 垂直对齐等宽。
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, " ")) +
"</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, " ")) +
"</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?(作为对
selectInput
数据框中标签和值的提醒)。 - How to use selectInput to return value of a key corresponding to a label 和
choices = setNames(df$label, df$value)
如果数据形式生成不需要的optgroup
。 (reactive()
上的isolate()
稍微破坏了数据的形式)。