2017-01-12 32 views
0

輸入以「iris $ Petal.Width - iris $ Species」格式顯示。在所選輸入中,要拆分的數據以及單獨使用$ Petal.Width的虹膜僅用於過濾整個數據。例如:選定的值與圖像中的一樣。 userIput閃亮使用selectizeInput構建動態數據輸出

嘗試獲得像dplyr ::濾波數據(光圈,光圈$ Petal.Width%以%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) 

} 

回答

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) 

} 
+0

感謝斯特芬LaZerte,你的做法非常接近解決我的問題。輸入爲「A00 - 描述」格式。因此,當我運行它給出錯誤信息爲「錯誤:操作只能用於數字,邏輯或複雜類型」。請提供任何建議。 – SPS

+0

你會想省略'convert = TRUE'參數。該參數告訴'separate()'將結果轉換爲數字(在上面的例子中)。但在你的情況下,你最終會得到'A00'和'描述',它們都不是數字(也不合邏輯也不復雜),我懷疑是你爲什麼會收到這個錯誤。 –