r语言 - 在 Shiny 中启用数据帧的某些部分的关键字?



一个数据帧有 5 列,其中一列是这样的:

colors
12 red
12 red
34 grey
32 cyan
14 black

我只想返回列颜色中包含红色单词的数据。所有列。

法典:

df[df$colors %like% "red", ]

我正在制作一个闪亮的应用程序,它将从某个位置读取 csv(固定(。用户将无法更改读取的文件,只能通过源代码。

我想在人们开始使用该应用程序之前要求一个类似密码的单词。 根据他使用的单词,数据集的一部分将可供他们使用。

例如,如果他们传递单词:

pass1 

只有df[df$colors %like% "red", ]对他们可见。

我该如何解决这个问题?

您可以使用键值对。在 R 中,这是通过定义两个向量来完成的 - 一个作为键的向量,另一个作为值的向量,并使用 names(( 来"命名"值。下面是基于您提供的示例数据的解决方案:

library(shiny)
library(dplyr)
# Dummy data
val <- c(12, 12, 34, 32, 14)
color <- c("red", "red", "grey", "cyan", "black")
foo <- cbind.data.frame(val, color)
ui =  fluidPage(
textInput("pswd", "Enter password:"),
tableOutput("table1")
)
server = function(input, output, session) {
# Keys
passwords <- c("pass1", "pass2", "pass3", "pass4") #....and so on
# Values
colors.filter <- c("red", "grey", "cyan", "black") #....and so on
# Assign names to make key-value pairs
names(colors.filter) <- passwords
# Subset data - select only corresponding value for input key
bar <- reactive({
filter(foo, color %like% colors.filter[input$pswd])
})
output$table1 <- renderTable({
bar()
})
} 
shinyApp(ui,server)

最新更新