2015-05-09 28 views
0

我尝试构建一个Shiny应用程序以显示来自反应子集的散点图。其实,应用程序的作品,我有我想要的一切,但是,似乎我的子集功能不能完成这项工作。意思是我的图显示的是我的输入$ vlabel变量的NA点。涉及is.na函数的无功子集在Shiny(R)中不起作用

这里是我的base数据集

x=rnorm(100,1 , 5) 
y=rnorm(100, 5, 1) 
z=rnorm(100, 3, 7) 
lab_a=ifelse(as.logical(rbinom(100,1,1/6)),TRUE,NA) 
lab_b=ifelse(as.logical(rbinom(100,1,1/5)),TRUE,NA) 
base=as.data.frame(cbind(x,y,z,lab_a,lab_b)) 
names(base)=c("variable1","variable2","variable3","lab_a","lab_b") 

这里是我的server.R:

library(shiny) 
library(datasets) 
library(ggplot2) 

#load data 
my_data=base 

# Define server logic required to plot variables 
shinyServer(function(input, output) { 


    #subset data 
    newdata<- reactive({ 
    subset(my_data,is.na(input$vlabel)==FALSE) 
    }) 

    # Create a reactive text 
    text <- reactive({ 
    paste(input$variable, 'versus', input$variable2) 
    }) 

    # Return as text the selected variables 
    output$caption <- renderText({ 
    text() 
    }) 

    # Generate a plot of the requested variables 
    output$plot <- renderPlot({ 
    p <- ggplot(newdata(), aes_string(x=input$variable, y=input$variable2, colour=input$vlabel)) + geom_point() 
    print(p) 
    }) 


}) 

这里是我的ui.R:

library(shiny) 

# Define UI for iris application 
shinyUI(pageWithSidebar(

    # Application title 
    headerPanel("Seasonnality"), 

    # Sidebar with controls to select the variable to plot against mpg 
    # and to specify whether outliers should be included 
    sidebarPanel(
    selectInput("vlabel", "Label variable:", 
       list("Label A" = "lab_a", 
        "Label B" = "lab_b")), 

    selectInput("variable", "First variable:", 
       list("Variable 1" = "variable1", 
        "Variable 2" = "variable2", 
        "Variable 3" = "variable3")), 

    selectInput("variable2", "Second variable:", 
       list("Variable 1" = "variable1", 
        "Variable 2" = "variable2", 
        "Variable 3" = "variable3")) 
), 

    mainPanel(
    h3(textOutput("caption")), 
    plotOutput("plot") 
) 
)) 

正如你所看到的,每点显示在我的情节,而我想绘制点不是我的input$vlabel variab乐。有什么想法吗?

回答

0

好吧,我不知道为什么我的代码以前的版本不工作, 但这样一来,它的罚款:

newdata<- reactive({ 
    my_data=my_data[is.na(my_data[input$vlabel])==FALSE,] 
    my_data 
    }) 
相关问题