0

输入以“iris$Petal.Width - iris$Species”格式显示。在选定的输入上,要拆分的数据和 iris$Petal.Width 单独用于过滤整个数据。示例:所选值与图像中的一样。 用户输入

尝试获取像 dplyr::filter(iris,iris$Petal.Width %in% c('0.2','0.3','0.1','0.6','1.4')) 这样的数据如何形成 c(' 0.2','0.3','0.1','0.6','1.4') 动态。

为了便于理解,以这个例子为例,实际上输入是 A001 - Description1,A002 - Description2 格式。需要取A001、A002组成c('A001','A002')。

尝试使用以下代码:

## run in interactive R sessions
if (interactive()) {

  ui <- fluidPage(

    selectizeInput('ipdesc', label='Selector', 
                   choices = as.list(c(unique(paste(iris$Petal.Width,iris$Species,sep = " - ")))),
                   multiple = TRUE,
                   options = list(maxItems = 5)
    ),
    p("Select Codes (Max 5), then press 'Go'"),
    actionButton("go", label = "Go"),
    tableOutput("selected")
  )

  server <- function(input, output) {
    #
    output$selected <- renderTable({
      filterdata()
    })

    filterdata <- eventReactive(input$go,{
      x=c()
      cnt = length(input$ipdesc)
      for (i in 1:cnt){
        if (i != cnt) {
          x[i] = cat(sapply(strsplit(input$ipdesc[i], " - "), "[", 1),",")
        }
        else
        {x[i] = cat(x[1],sapply(strsplit(input$ipdesc[i], " - "), "[", 1))}

      } })


    #

  }

  shinyApp(ui, server)

}
4

1 回答 1

0

这不是一个真正的shinyapps 或shinyjs 问题,您需要知道的只是如何匹配拆分字符串以将其一部分与数据框匹配。因为您正在使用数据框,strsplit()所以可能不是最优雅的解决方案。

正如你提到dplyr的,我会给你一个tidyverse选择:

尝试separate()tidyr包中使用。convert = TRUE意味着如果可能,结果列应自动转换为数字/整数/逻辑。

library(dplyr)
library(tidyr)

input <- "1.8 - versicolor"

temp <- data.frame(input = input) %>%
          tidyr::separate(input, c("Petal.Width", "Species"), 
                          sep = " - ", convert = TRUE)
filtered <- dplyr::filter(iris, iris$Petal.Width %in% temp$Petal.Width)

filtered输出:

#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
# 1           5.9         3.2          4.8         1.8 versicolor
# 2           6.3         2.9          5.6         1.8  virginica
# 3           7.3         2.9          6.3         1.8  virginica
# 4           6.7         2.5          5.8         1.8  virginica
# 5           6.5         3.0          5.5         1.8  virginica
# ...

请注意,这同时匹配versicolorvirginica

结合你闪亮的脚本:

library(shiny)
if (interactive()) {

  ui <- fluidPage(
    selectizeInput('ipdesc', label='Selector', 
                   choices = as.list(c(unique(paste(iris$Petal.Width,iris$Species,sep = " - ")))),
                   multiple = TRUE,
                   options = list(maxItems = 5)
    ),
    p("Select Codes (Max 5), then press 'Go'"),
    actionButton("go", label = "Go"),
    tableOutput("selected")
  )

  server <- function(input, output) {

    output$selected <- renderTable({
      filterdata()
    })

    filterdata <- eventReactive(input$go, {
      temp <- data.frame(input = input$ipdesc) %>%
        tidyr::separate(input, c("Petal.Width", "Species"), sep = " - ", convert = TRUE)

       iris %>%
         dplyr::filter(iris$Petal.Width %in% temp$Petal.Width)

    })    
  }

  shinyApp(ui, server)

}
于 2017-01-13T14:47:41.530 回答