例如:library(plotly)
library(shiny)
library(htmlwidgets)
js <- c(
"function(el, x, inputName){",
" var id = el.getAttribute('id');",
" var d3 = Plotly.d3;",
" el.on('plotly_restyle', function(evtData) {",
" var out = {};",
" d3.select('#' + id + ' g.legend').selectAll('.traces').each(function(){",
" var trace = d3.select(this)[0][0].__data__[0].trace;",
" out[trace.name] = trace.visible;",
" });",
" Shiny.setInputValue(inputName, out);",
" });",
"}")
YNElement <- function(idx){sprintf("YesNo_button-%d", idx)}
ui <- fluidPage(
fluidRow(
column(2,
h5("Keep/Drop choices linked to colorscheme 1"),
uiOutput('YNbuttons')
),
column(8,
plotlyOutput("plot1")
),
column(2,
h5('Switch grouping'),
actionButton(inputId = 'Switch', label = icon('refresh'), style = "color: #f7ad6e; background-color: white; border-color: #f7ad6e;
height: 40px; width: 40px; border-radius: 6px; border-width: 2px; text-align: center; line-height: 50%; padding: 0px; display:block; margin: 2px")
), style = "margin-top:150px"
),
verbatimTextOutput("tracesPlot1")
)
server <- function(input, output, session) {
values <- reactiveValues(colors = T, NrOfTraces = length(unique(mtcars$cyl)))
output$plot1 <- renderPlotly({
if(values$colors) { colors <- c('red', 'blue', 'green') } else {colors <- c('black', 'orange', 'gray')}
p1 <- plot_ly()
p1 <- add_trace(p1, data = mtcars, x = ~disp, y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl), colors = colors)
p1 <- layout(p1, title = 'mtcars group by cyl with switching colors')
p1 %>% onRender(js, data = "tracesPlot1")
})
observeEvent(input$Switch, { values$colors <- !values$colors })
observeEvent(values$NrOfTraces, {
values$dYNbs_cyl_el <- rep(T,values$NrOfTraces)
names(values$dYNbs_cyl_el) <- sapply(1:values$NrOfTraces, function(x) {YNElement(x)})
})
output$YNbuttons <- renderUI({
req(values$NrOfTraces)
lapply(1:values$NrOfTraces, function(el) {
YNb <- YNElement(el)
if(values$dYNbs_cyl_el[[YNb]] == T ) {
div(actionButton(inputId = YNb, label = icon("check"), style = "color: #339FFF; background-color: white; border-color: #339FFF;height: 34px; width: 34px; border-radius: 6px; border-width: 2px; text-align: center; line-height: 50%; padding: 0px; display:block; margin: 2px"))
} else {
div(actionButton(inputId = YNb, label = icon("times"), style = "color: #ff4d4d; background-color: white; border-color: #ff4d4d;height: 34px; width: 34px; border-radius: 6px; border-width: 2px; text-align: center; line-height: 50%; padding: 0px; display:block; margin: 2px"))
}
})
})
observeEvent(input$tracesPlot1, {
listTraces <- input$tracesPlot1
#values$tracesPlot1 <- input$tracesPlot1
listTracesTF <- gsub('legendonly', FALSE, listTraces)
lapply(1:values$NrOfTraces, function(el) {
if(el <= length(listTracesTF)) {
YNb <- YNElement(el)
if(values$dYNbs_cyl_el[[YNb]] != listTracesTF[el]) {
values$dYNbs_cyl_el[[YNb]] <- listTracesTF[el]
}
}
})
})
output$tracesPlot1 <- renderPrint({ unlist(input$tracesPlot1) })
}
shinyApp(ui, server)
的长度为18,包括世界和宽幅之间的距离。
我尝试过:
["hello","world wide","1","2","3"]
但会删除空格。
我正在考虑将其用于以下用途: 我正在创建一个函数,该函数将返回尽可能多的单词的列表,但将其保持在指定的长度以下。
示例:
string:len(lists:flatten([X|Y]))
答案 0 :(得分:4)
平整并取总长度的效果很好:
1> L = ["hello","world wide","1","2","3"].
["hello","world wide","1","2","3"]
2> string:length(lists:flatten(L)).
18
您也可以将每个部分的长度相加:
3> lists:sum([length(S) || S <- L ]).
18
或者您可以像这样实现limit_word
函数:
-module(hello).
-export([limit_word/2]).
limit_word(L, Max) ->
limit_word(L, Max, {0, []}).
limit_word([H|T], Max, {Size, Acc}) ->
NewSize = Size + length(H),
case NewSize > Max of
false ->
limit_word(T, Max, {NewSize, [H|Acc]});
true->
lists:reverse(Acc)
end;
limit_word([], _, {_, Acc}) ->
lists:reverse(Acc).
limit_word/2
是导出到调用方的内容(与您的原始示例相同)。它只是调用limit_word/3
函数,该函数需要一个额外的参数:当前总长度Size
和累积单词Acc
的元组。我们以列表的开头,将其长度添加到Size
,如果总数小于Max
,则递归调用limit_word/3
,并传递NewSize
和一个新列表以新单词为开头的单词,以现有单词列表为结尾的单词列表。但是,如果NewSize
超过Max
,我们将返回反向的Acc
列表-之所以反向,是因为我们通过在头部添加新单词来形成列表。正如您所期望的那样工作:
4> hello:limit_word(["Hello", "there my friend", "wassup!"],10).
["Hello"]
5> hello:limit_word(["Hello", "there my friend", "wassup!"],22).
["Hello","there my friend"]
更新:如果所有列表元素的总长度小于Max
,我们将永远不会遇到true
的情况,因此最后的limit_word/3
子句通过返回以下内容来处理空的输入列表:反向蓄能器。
答案 1 :(得分:0)
您可以使用iolist_size/1
:
> iolist_size(["hello","world wide","1","2","3"]).
18