涉及 is.na 函数的反应子集在 Shiny (R) 中不起作用
reactive subset involving is.na function don't work in Shiny (R)
我尝试构建一个闪亮的应用程序以显示来自反应子集的散点图。实际上,该应用程序有效,我拥有我想要的一切,但是我的子集功能似乎无法完成这项工作。这意味着我的绘图显示的点对于我的输入 $vlabel 变量是 NA。
这是我的 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
变量不适用的点。有什么想法吗?
好吧,我不明白为什么我以前的代码版本不起作用,
但是这样就可以了:
newdata<- reactive({
my_data=my_data[is.na(my_data[input$vlabel])==FALSE,]
my_data
})
我尝试构建一个闪亮的应用程序以显示来自反应子集的散点图。实际上,该应用程序有效,我拥有我想要的一切,但是我的子集功能似乎无法完成这项工作。这意味着我的绘图显示的点对于我的输入 $vlabel 变量是 NA。
这是我的 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
变量不适用的点。有什么想法吗?
好吧,我不明白为什么我以前的代码版本不起作用, 但是这样就可以了:
newdata<- reactive({
my_data=my_data[is.na(my_data[input$vlabel])==FALSE,]
my_data
})