2017-08-25 5 views
0

データサブセットに基づいてグラフをフレックスダッシュボードにプロットしたいと考えています。任意のアイデアは持っているものは、オブジェクト(クラス「reactivevalues」)からスロット「selectInput1」を取得しようとしてフレックスダッシュボードのデータサブセットに基づいて簡単なグラフをプロットする

S4オブジェクトではありませんこと:ここではコードです:

--- 
title: "Test" 
output: 
    flexdashboard::flex_dashboard: 
    orientation: columns 
    social: menu 
    source_code: embed 
    runtime: shiny 
--- 

```{r global, include=FALSE} 
library(flexdashboard) 
library(ggplot2) 
library(shiny) 

A = c("A1", "A1", "A1", "A2", "A2", "A1", "A2", "A2") 
B = c("B1", "B2", "B1", "B2", "B1", "B2", "B1", "B2") 
x = c(1, 2, 3, 4, 5, 3, 3, 4) 
y = c(5, 4, 3, 2, 1, 5, 3, 4) 
df = data.frame(A, B, x, y, stringsAsFactors = FALSE) 
``` 

Column 
------------------------------- 

### Select options 
```{r} 
selectInput("selectInput1", "Select A:", 
      choices = unique(df$A)) 

selectInput("selectInput2", "Select B:", 
      choices = unique(df$B)) 
``` 

```{r} 
# Create a subset data frame 
selectedData = reactive({ 
    df[df$A == [email protected] & df$B == [email protected], c(3,4)] 
    }) 
``` 

Column 
------------------------------- 

###Chart 

```{r} 
renderPlot({ 
    ggplot(selectedData(), aes(x = x, y = y)) + 
     geom_line() + ggtitle(paste0("Selected ", [email protected], " and ", [email protected])) 
}) 
``` 

は、しかし、私はエラーを取得します間違った方法で行われていますか?そしてコードを動作させる方法は?ここで

答えて

0

は、ソリューションです:

--- 
title: "Test" 
output: 
    flexdashboard::flex_dashboard: 
    orientation: columns 
    social: menu 
    source_code: embed 
    runtime: shiny 
--- 

```{r global, include=FALSE} 
library(flexdashboard) 
library(ggplot2) 
library(shiny) 

A = c("A1", "A1", "A1", "A2", "A2", "A1", "A2", "A2") 
B = c("B1", "B2", "B1", "B2", "B1", "B2", "B1", "B2") 
x = c(1, 2, 3, 4, 5, 3, 3, 4) 
y = c(5, 4, 3, 2, 1, 5, 3, 4) 
df = data.frame(A, B, x, y, stringsAsFactors = FALSE) 
``` 

Column 
------------------------------- 

### Select options 
```{r} 
selectInput("selectInput1", "Select A:", 
      choices = unique(df$A)) 

selectInput("selectInput2", "Select B:", 
      choices = unique(df$B)) 
``` 

```{r} 
# Create a subset data frame 
selectedData = reactive({ 
    df[df$A == input$selectInput1 && df$B == input$selectInput2, c(3,4)] 
    }) 
``` 

Column 
------------------------------- 

###Chart 

```{r} 
renderPlot({ 
    ggplot(selectedData(), aes(x = x, y = y)) + 
     geom_line() + ggtitle(paste0("Selected ", input$selectInput1, " and ", input$selectInput2)) 
}) 
``` 

あなたが作った唯一の問題はここでは例と同様に、代わりに$@記号を使用することです:[email protected]。あなたのコードを編集した後 - 記号を$に変更すると、flexdashboardは完全に機能しました。

+0

ありがとうございます!だから愚かな間違い...しかし、私はまだサブセット化の段階で "&&"の代わりに "&"を使いたいと思っています。 – Sergiy

関連する問題