基于R中selectInput的子集数据帧

基于R中selectInput的子集数据帧,r,sh,R,Sh,我有一个闪亮的应用程序,其中我根据数据帧中变量之间的关系生成scagnostics,如下所示 library(binostics) scagnostics(df$x1, df$x2)$s 但是,我想从下拉列表中动态选择这些变量。但是,当我这样做时,我无法根据输入变量对数据帧进行子集划分 selectInput("v1", label = "Select Variable 1", choices = selection, sele

我有一个闪亮的应用程序,其中我根据数据帧中变量之间的关系生成scagnostics,如下所示

library(binostics)

scagnostics(df$x1,
            df$x2)$s
但是,我想从下拉列表中动态选择这些变量。但是,当我这样做时,我无法根据输入变量对数据帧进行子集划分

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

  
可复制示例:

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)
库(readr)
图书馆(二学)
图书馆(tidyverse)
图书馆(gridExtra)
大型环保车2019%
过滤器(年份==2019)

我的朋友们@Ben的回答在上面评论道是有效的

s <- scagnostics(big_epa_cars_2019[[input$v1]], big_epa_cars_2019[[input$v2]])$s

s请提供一个可复制的代码块,以便我们可以进一步帮助您。@MritiAgarwaladded@zstar要根据
input$v1
input$v2
提取变量,请尝试改用
[[
,例如:
s