更新(2015年12月18日) 此问题的当前最佳应用是:在Shiny App中从DataTable获取所选行
======================
我正在尝试使用用户提供的行选择来反复重新显示数据集.作为一个玩具的例子,
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel('Examples of DataTables'),
sidebarPanel(
checkboxGroupInput('show_vars', 'Columns to show:', names(mtcars),
selected = names(mtcars))
),
mainPanel(
dataTableOutput("mytable")
)
)
)
Run Code Online (Sandbox Code Playgroud)
server.R
library(shiny)
shinyServer(function(input, output) {
# sorted columns are colored now because CSS are attached to them
output$mytable = renderDataTable({
addRadioButtons <- paste0('<input type="radio" name="row', 1:nrow(mtcars), '" value="', 1:nrow(mtcars), '">',"")
#Display table with radio buttons
cbind(Pick=addRadioButtons, mtcars[, input$show_vars, drop=FALSE])
}, options = list(bSortClasses = TRUE, aLengthMenu = c(5, 25, 50), iDisplayLength = 25)) …Run Code Online (Sandbox Code Playgroud) 这可能是多余的,但我在SO上找不到类似的问题.
是否有一个快捷方式来获取向量或数组中的最后n个元素/条目而不使用计算中向量的长度?
foo <- 1:23
> foo
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Run Code Online (Sandbox Code Playgroud)
假设有人想要最后7个实体,我想避免这种繁琐的语法:
> foo[(length(foo)-6):length(foo)]
[1] 17 18 19 20 21 22 23
Run Code Online (Sandbox Code Playgroud)
Python有foo[-7:].R中有类似的东西吗?谢谢!
这基本上是一个跟进这个问题的详细例子(没有答案通过): conditionalPanel in shiny(似乎不起作用)
示例app:根据用户选择显示面板("list1","list2"等)."项目list3"未被选择,且应不显示.
ui.R
displayList <- c("list1", "list2", "list3")
shinyUI(pageWithSidebar(
headerPanel("Shiny Display List"),
sidebarPanel(
checkboxGroupInput('dlist', 'Display List:', displayList, selected = displayList[1:2])
),
mainPanel(
h4("Display List"),
conditionalPanel(condition = "length(intersect(input.dlist, displayList[1])) > 0",
p("Some List 1 entries")
),
conditionalPanel(condition = "length(intersect(input.dlist, displayList[2])) > 0",
p("Some List 2 entries")
),
conditionalPanel(condition = "length(intersect(input.dlist, displayList[3])) > 0",
p("Some List 3 entries") #WASN'T SELECTED, SHOULD NOT DISPLAY INITIALLY
)
)
))
Run Code Online (Sandbox Code Playgroud)
server.R
shinyServer(function(input, output) {
observe({cat(input$dlist, "\n")})
observe({cat(length(intersect(input$dlist, "list3")))})
})
Run Code Online (Sandbox Code Playgroud)
为了测试,如果条件被满足,我跑 …