如何在R中的闪亮应用程序中创建相关提示

如何在R中的闪亮应用程序中创建相关提示,r,shiny,R,Shiny,我正在创建一个R Shining应用程序,我希望有两个selectInput,即数据集名称和列名。现在,我可以在第一个输入中获取数据集名称,但无法创建依赖列selectIput(其列表将取决于所选的数据集)。请导游 require(shiny) require(MASS) a <- data(package = "MASS")[3] b <- a$results[,3] ui <- fluidPage( sidebarPanel(width = 2, select

我正在创建一个R Shining应用程序,我希望有两个selectInput,即数据集名称和列名。现在,我可以在第一个输入中获取数据集名称,但无法创建依赖列selectIput(其列表将取决于所选的数据集)。请导游

require(shiny)
require(MASS)

a <- data(package = "MASS")[3]
b <- a$results[,3]

ui <- fluidPage(
  sidebarPanel(width = 2,

  selectInput(inputId = "dsname",label = "Select Dataset:",choices = c(b)),

  colnames <- names("dsname"), 

  selectInput(inputId = "columns", label = "Choose columns",
              choices  = c(colnames))                   
  )
)

server <- function(input,output) {}

shinyApp(ui <- ui, server <- server)
require(闪亮)
要求(质量)
a为了使“responsive”元素具有光泽,您需要在
reactive({…})
中包装用于计算响应元素的表达式

您可以在
server()
中使用
renderUI
,在
ui()
中使用
ui输出。下面是我为使用R的一些数据集(iris、mtcars和钻石)而构建的一个示例:

库(shinythemes)
图书馆(闪亮)
图书馆(GG2)
用户界面
library(shinythemes)
library(shiny)
library(ggplot2)

ui <- fluidPage(theme = shinytheme("superhero"),
   titlePanel("Welcome to Responisve Shiny"),
   sidebarLayout(
      sidebarPanel(
          selectInput("data", "Dataset:",
                      choices = c("mtcars", "iris", "diamonds")
                      ),
         uiOutput("x_axis"),
         uiOutput("y_axis"),
         uiOutput("color")
      ),
      mainPanel(
         plotOutput("distPlot")
      )
   )
)
server <- function(input, output) {
    output$x_axis <- renderUI({
        col_opts <- get(input$data)
        selectInput("x_axis2", "X-Axis:",
                    choices = names(col_opts))
    })
    output$y_axis <- renderUI({
        cols2 <- reactive({
            col_opts2 <- get(input$data)
            names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))]
        })
        selectInput("y_axis2", "Y-Axis:",
                    choices = cols2(),
                    selected = "hp")
    })
    output$color <- renderUI({
        col_opts <- get(input$data)
        selectInput("color", "Color:",
                    choices = names(col_opts),
                    selected = "cyl")
    })
   output$distPlot <- renderPlot({
       if(input$data == "mtcars"){
           p <- ggplot(mtcars, aes_string(input$x_axis2, input$y_axis2, color = input$color)) +
               geom_point()
       }
       if(input$data == "iris"){
           p <- ggplot(iris, aes_string(input$x_axis2, input$y_axis2, color = input$color)) +
               geom_point()
       }
       if(input$data == "diamonds"){
           p <- ggplot(diamonds, aes_string(input$x_axis2, input$y_axis2, color = input$color)) +
               geom_point()
       }
      print(p)
   })
}
shinyApp(ui = ui, server = server)