使用selectizeInput构建动态数据输出

时间:2017-01-12 12:30:24

标签: r shiny shinyjs

输入显示在" iris $ Petal.Width - iris $ Species" format.Upon选择输入,要拆分的数据和iris $ Petal.Width单独用于过滤整个数据。 示例:所选值与图像中的值相同。 userIput

尝试获取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)

}

1 个答案:

答案 0 :(得分:0)

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

在您提到dplyr时,我会给您一个tidyverse选项:

尝试使用separate()包中的tidyrconvert = 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)

}