R 添加两个选择器输入,其中包括应用程序中数据框的所有列

R 添加两个选择器输入,其中包括应用程序中数据框的所有列,r,shiny,R,Shiny,我使用的是与mtcars不同的数据库,但为了使这个示例具有可复制性,我将在这里使用它 我正在尝试创建一个带有滑块和两个选择器的应用程序 基本上,如果我只选择一列,我知道如何创建selectInput。在下面的代码中,我只需要selectInput(“cyInput”,“input”,choices=c(我所有的选择)并将cyl==input$cyInput添加到过滤器函数中 这就是说,如果我想为数据框中的所有列创建两个selectInputs,那么我不知道该怎么做,也不知道如何在服务器端执行一次

我使用的是与mtcars不同的数据库,但为了使这个示例具有可复制性,我将在这里使用它

我正在尝试创建一个带有滑块和两个选择器的应用程序

基本上,如果我只选择一列,我知道如何创建selectInput。在下面的代码中,我只需要
selectInput(“cyInput”,“input”,choices=c(我所有的选择)
并将
cyl==input$cyInput
添加到过滤器函数中

这就是说,如果我想为数据框中的所有列创建两个selectInputs,那么我不知道该怎么做,也不知道如何在服务器端执行一次

代码如下:

ui:

library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", names(mtcars)),
      selectInput("ycol", "Y Axis", names(mtcars))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>%
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered, aes(x = mpg, y = qsec)) +
      geom_point() 
  })
}

shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs")),
      selectInput("ycol", "Y Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs"))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>% 
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered) +
      geom_point(aes_string(x = input$xcol, y = input$ycol))
  })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(dplyr)
车头(mtcars)

ui我明白了。很抱歉这个措词不好的问题。老实说,我对Shiny很陌生,对代码不是很了解

基本上,我所做的只是使用了
choices=c()
,而不是
names(mtcars)
,这对我来说不起作用

然后在ggplot2图形中调用输入$xcol和输入$ycol

ui:

library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", names(mtcars)),
      selectInput("ycol", "Y Axis", names(mtcars))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>%
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered, aes(x = mpg, y = qsec)) +
      geom_point() 
  })
}

shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs")),
      selectInput("ycol", "Y Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs"))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>% 
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered) +
      geom_point(aes_string(x = input$xcol, y = input$ycol))
  })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(dplyr)
车头(mtcars)

ui我明白了。很抱歉这个措词不好的问题。老实说,我对Shiny很陌生,对代码不是很了解

基本上,我所做的只是使用了
choices=c()
,而不是
names(mtcars)
,这对我来说不起作用

然后在ggplot2图形中调用输入$xcol和输入$ycol

ui:

library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", names(mtcars)),
      selectInput("ycol", "Y Axis", names(mtcars))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>%
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered, aes(x = mpg, y = qsec)) +
      geom_point() 
  })
}

shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
library(dplyr)

head(mtcars)

ui <- fluidPage(
  titlePanel("My App"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mpginput", "mpg", min = 10.4, max = 33.9, value = c(10.4, 33.9)),
      selectInput("xcol", "X Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs")),
      selectInput("ycol", "Y Axis", choices = c("mpg","cyl","hp","drat","disp","wt","gear","carb","am","vs"))
    ),
    mainPanel(plotOutput("plot1"))
  )
)
server <- function(input, output){

  output$plot1 <- renderPlot({
    Filtered <- mtcars %>% 
      filter(
        mpg >= input$mpginput[1],
        mpg <= input$mpginput[2]
      )
    ggplot(Filtered) +
      geom_point(aes_string(x = input$xcol, y = input$ycol))
  })
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(dplyr)
车头(mtcars)

ui请澄清您希望在服务器代码中如何使用input$xcol和input$ycol。@熊秉锦因此,我希望图形以
mpg==input$xcol
qsec==input$ycol
开头。但是,当用户想要选择不同的列时,他们可以选择,比如说cyl,然后应用程序会做出反应并显示结果在任意轴上的数据列。请澄清您希望如何在服务器代码中使用input$xcol和input$ycol。@熊秉锦因此,我希望图形以
mpg==input$xcol
qsec==input$ycol
开头。但是,无论何时用户想要选择不同的列,他们都可以选择,比如说cyl,t然后,应用程序会做出反应,并在任意轴上显示数据列。