[英]Subset dataframe based on selectInput in R Shiny
I have a shiny app in which I generate scagnostics based on the relationship between variables in a dataframe, as follows我有一个 shiny 应用程序,我在其中根据 dataframe 中变量之间的关系生成 scagnostics,如下所示
library(binostics)
scagnostics(df$x1,
df$x2)$s
However, I want to dynamically select these variables from a drop down list.但是,我想从下拉列表中动态地 select 这些变量。 But when I do so I'm not able to subset the data frame based on the input variables但是当我这样做时,我无法根据输入变量对数据框进行子集化
selectInput("v1", label = "Select Variable 1", choices = selection, selected = "x1"),
selectInput("v2", label = "Select Variable 2", choices = selection, selected = "x2")
scagnostics(df$input$v1,
df$input$v2)$s
Reproducible Example:可重现的例子:
library(readr)
library(binostics)
library(tidyverse)
library(gridExtra)
big_epa_cars_2019 <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-10-15/big_epa_cars.csv") %>%
filter(year == 2019)
my_vars <- c("barrels08", "cylinders", "city08", "highway08", "feScore", "fuelCost08", "co2TailpipeGpm", "youSaveSpend")
ui <- fluidPage(
fluidRow(
column(1),
column(3, selectInput("v1", label = "Select x Variable", choices = my_vars, selected = "barrels08")),
column(3, selectInput("v2", label = "Select y Variable", choices = my_vars, selected = "city08"))
),
fluidRow(
column(1),
column(10, plotOutput("scagnosticsplots")),
column(1))
)
server <- function(input, output, session) {
output$scagnosticsplots <- renderPlot({
p1 <- ggplot(big_epa_cars_2019,
aes(x = get(input$v1),
y = get(input$v2))) +
geom_point() +
theme_bw() +
labs(x = input$v1,
y = input$v2)
s <- scagnostics(big_epa_cars_2019$input$v1,
big_epa_cars_2019$input$v2)$s
df_s <- tibble(scag = names(s), value = s) %>%
mutate(scag = fct_reorder(scag, value))
p2 <- ggplot(df_s, aes(x=value, y=scag)) +
geom_point(size=4, colour="orange") +
geom_segment(aes(x=value, xend=0,
y=as.numeric(scag),
yend=as.numeric(scag)), colour="orange") +
theme_bw() +
labs(x = "Scagnostic value",
y = "")
grid.arrange(p1, p2, ncol=2)
})
}
shinyApp(ui, server)
@Ben's answer commented above worked.上面评论的@Ben 的回答有效。
s <- scagnostics(big_epa_cars_2019[[input$v1]], big_epa_cars_2019[[input$v2]])$s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.