2017-01-12 3 views
0

入力は "iris $ Petal.Width - iris $ Species"形式で表示されます。選択された入力、分割されるデータ、およびiris $ Petal.Widthだけが全体をフィルタリングするために使用されますデータ。 例:選択した値は画像の場合と同じです。 userIputselectizeInputを使用して動的なデータ出力を構築する

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の形式になっています。 c( 'A001'、 'A002')を形成するには、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問題ではありません、あなたが知る必要があるすべては、データフレームにその一部と一致する文字列を分割一致する方法です。データフレームで作業しているため、は最も洗練されたソリューションではないかもしれません。

あなたがdplyrに言及したように、私はあなたにtidyverseオプション提供します:

tidyrパッケージからseparate()を使用してみてくださいを。 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出力:これはversicolorvirginicaの両方に一致していることを

# 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 
# ... 

注意。あなたの光沢のあるスクリプトと組み合わせる

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'と 'Description'で終わってしまいます。どちらも数値でも論理的でも複雑ではありません。なぜこのエラーが出ているのでしょうか? –

関連する問題