【问题标题】:How to create dependent prompts in Shiny App in R如何在 R 中的 Shiny App 中创建依赖提示
【发布时间】:2017-07-04 06:14:54
【问题描述】:

我想创建一个 R 闪亮的应用程序,我想要两个 selectInput,即数据集名称和列名称。现在,我能够在第一个 Input 中获取数据集名称,但我无法创建依赖列 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    为了在 Shiny 中拥有“响应式”元素,您需要包装表达式以计算 reactive({...}) 中的响应式元素。

    您可以在server() 中使用renderUI,在ui() 中使用uiOutput,类似这样。这是我为使用 R 的一些数据集(iris、mtcars 和 diamonds)而构建的示例:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 2020-07-13
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多