ShinyApp:使用选定的输入对分类变量的某些级别进行子集化,并将其用作在服务器中绘图的输入
ShinyApp: Use a selected input to subset some levels of a categorical variable,and use that as input for plotting in the server
我正在尝试绘制一个连续变量 (y) 与另一个变量 (x) 的关系图,并根据第三个分类变量 (z) 为图中的数据点着色。对于 select 这三个变量,我使用 selectInput 函数,但是对于 select 我想绘制的可能类别(级别(input$z)),我使用 uiOutput 函数。我正在尝试在过滤后的数据帧 (dataf) 中对 selected 级别进行子集化,但这不起作用。一些反应式表达式正在工作,但我使用子集回滚了代码,因为当我在 renderPlot 函数中使用 ( dataf <- filter(data(), input$z %in% input$show_levels)) 我不'绘制任何数据点。
我已经使用钻石数据集准备了我需要的简化版本。例如,我需要我的 shinyApp 来绘制 prize vs carats,点的颜色取决于切割,并且只能代表具有特定切割的点(例如 cut == c("Fair", "Good").)
library(shiny)
library(ggplot2)
library(RColorBrewer)
library(dplyr)
cont_vars <- c("price", "carat", "x", "y", "z", "depth", "table")
discr_vars <- c("cut", "color", "clarity")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Analysis of 'diamonds' dataset"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = "Y-axis:",
choices = cont_vars,
selected = cont_vars[1]),
# Select variable for x-axis
selectInput(inputId = "x",
label = "X-axis:",
choices = cont_vars,
selected = cont_vars[2]),
# Select variable for color
selectInput(inputId = "z",
label = "Z-axis:",
choices = discr_vars,
selected = discr_vars[1]),
# Select level/s to show for the z category
uiOutput("selected_z")
),
# Show the plot
mainPanel(
plotOutput("scatterplot")
)
)
)
# Define server logic required to draw a scatterplot
server <- function(input, output) {
# Show levels for the discrete variable selected in input$selected_z
output$selected_z <- renderUI({
checkboxGroupInput(inputId = "show_levels",
label = "Select category/ies to represent:",
choices = choices_z(),
selected = choices_z())
})
choices_z <- reactive({
df <- select(diamonds, input$z)
return(levels(df[[1]]))
})
output$scatterplot <- renderPlot({
# generate df based on inputs selected
data <- select(diamonds, input$x, input$y, input$z)
# dataf <- filter(data(), input$z %in% input$show_levels)
ggplot(data, aes_string(x = input$x, y = input$y,
color = input$z)) +
geom_point(size = 4) +
scale_color_brewer(palette = "Paired") +
theme_light()
})
}
# Run the application
shinyApp(ui = ui, server = server)
我希望 select input$show_levels 分类变量(级别)我对 checkboxGroupInput (input$showlevels) 感兴趣,并且只显示散点图。现在我得到了显示级别的 checkboxGroupInput 函数(见下图),但我担心它没有连接到服务器,并且正在绘制 input$z 的所有级别。
how the shinyApp is now
当您尝试过滤数据时(您的注释行以 dataf
开头),您似乎已经设置好了。您需要让 filter()
认识到您希望它使用由 input$z
表示的列,而不是使用实际值 input$z
(即 "cut"
)。如果您更新 renderPlot
以便数据像这样过滤
data <-
select(diamonds, input$x, input$y, input$z) %>%
filter(!!(as.name(input$z)) %in% input$show_levels)
那么该应用程序应该会按您预期的方式运行。 This answer 有更多关于 why/how 处理将闪亮输入传递给 dplyr 函数的详细信息。
我正在尝试绘制一个连续变量 (y) 与另一个变量 (x) 的关系图,并根据第三个分类变量 (z) 为图中的数据点着色。对于 select 这三个变量,我使用 selectInput 函数,但是对于 select 我想绘制的可能类别(级别(input$z)),我使用 uiOutput 函数。我正在尝试在过滤后的数据帧 (dataf) 中对 selected 级别进行子集化,但这不起作用。一些反应式表达式正在工作,但我使用子集回滚了代码,因为当我在 renderPlot 函数中使用 ( dataf <- filter(data(), input$z %in% input$show_levels)) 我不'绘制任何数据点。
我已经使用钻石数据集准备了我需要的简化版本。例如,我需要我的 shinyApp 来绘制 prize vs carats,点的颜色取决于切割,并且只能代表具有特定切割的点(例如 cut == c("Fair", "Good").)
library(shiny)
library(ggplot2)
library(RColorBrewer)
library(dplyr)
cont_vars <- c("price", "carat", "x", "y", "z", "depth", "table")
discr_vars <- c("cut", "color", "clarity")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Analysis of 'diamonds' dataset"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = "Y-axis:",
choices = cont_vars,
selected = cont_vars[1]),
# Select variable for x-axis
selectInput(inputId = "x",
label = "X-axis:",
choices = cont_vars,
selected = cont_vars[2]),
# Select variable for color
selectInput(inputId = "z",
label = "Z-axis:",
choices = discr_vars,
selected = discr_vars[1]),
# Select level/s to show for the z category
uiOutput("selected_z")
),
# Show the plot
mainPanel(
plotOutput("scatterplot")
)
)
)
# Define server logic required to draw a scatterplot
server <- function(input, output) {
# Show levels for the discrete variable selected in input$selected_z
output$selected_z <- renderUI({
checkboxGroupInput(inputId = "show_levels",
label = "Select category/ies to represent:",
choices = choices_z(),
selected = choices_z())
})
choices_z <- reactive({
df <- select(diamonds, input$z)
return(levels(df[[1]]))
})
output$scatterplot <- renderPlot({
# generate df based on inputs selected
data <- select(diamonds, input$x, input$y, input$z)
# dataf <- filter(data(), input$z %in% input$show_levels)
ggplot(data, aes_string(x = input$x, y = input$y,
color = input$z)) +
geom_point(size = 4) +
scale_color_brewer(palette = "Paired") +
theme_light()
})
}
# Run the application
shinyApp(ui = ui, server = server)
我希望 select input$show_levels 分类变量(级别)我对 checkboxGroupInput (input$showlevels) 感兴趣,并且只显示散点图。现在我得到了显示级别的 checkboxGroupInput 函数(见下图),但我担心它没有连接到服务器,并且正在绘制 input$z 的所有级别。 how the shinyApp is now
当您尝试过滤数据时(您的注释行以 dataf
开头),您似乎已经设置好了。您需要让 filter()
认识到您希望它使用由 input$z
表示的列,而不是使用实际值 input$z
(即 "cut"
)。如果您更新 renderPlot
以便数据像这样过滤
data <-
select(diamonds, input$x, input$y, input$z) %>%
filter(!!(as.name(input$z)) %in% input$show_levels)
那么该应用程序应该会按您预期的方式运行。 This answer 有更多关于 why/how 处理将闪亮输入传递给 dplyr 函数的详细信息。