Shiny: SelectInput subset based on input

后端 未结 1 1224
时光说笑
时光说笑 2021-01-22 09:34

Edit: Thanks for the help, there were multiple issues with my code but the main issue was that I was missing an Observe statement, the following solved the problem:



        
1条回答
  •  别那么骄傲
    2021-01-22 10:08

    There were some issues in your code, like using Fixtu, which did not refer to anything. Also, I think that levels() is probably a better choice than attributes() for getting the unique values in a factor variable.

    I find that it's helpful to use renderUI in the server.R file when you want the input into one widget to control the input into another. You can then put in return statements to prevent the widget from even showing up before it knows what options to offer. I do this by adding a "pick one" option that causes the next widget to not even show up. It would be better if you could make the selectInput default to NULL, but that is not an option.

    Here is what I did:

    server.R:

    library(shiny)
    library(ggplot2)
    
    poskick<-read.csv('poskicks.csv')
    
    shinyServer(function(input, output) {
    
      output$Box1 = renderUI(selectInput('player', 
                                         'Player', 
                                         c(levels(poskick$Name),"pick one"),
                                         "pick one")
      )
    
      output$Box2 = renderUI(
        if (is.null(input$player) || input$player == "pick one"){return() 
        }else selectInput('fixture', 
                          'Match', 
                          c(levels(poskick$Event[which(poskick$Name == input$player)]),"pick one"),
                          "pick one")
        )
    
      subdata1 = reactive(poskick[which(poskick$Name == input$player),])
      subdata2 = reactive(subdata1()[which(subdata1()$Event == input$fixture),])
    
      output$plot <- renderPlot({
        if (is.null(input$player) || is.null(input$fixture)){return()
        } else if(input$player == "pick one" || input$fixture == "pick one") { return()
        } else p <- ggplot(data = subdata2()) + geom_segment(aes(x = x_coord, y =   y_coord, xend = x_coord_end, yend = y_coord_end))
        print(p)    })
    
    })
    

    ui.R:

    library(shiny)
    library(ggplot2)
    shinyUI(pageWithSidebar(
      headerPanel("position map"),
      sidebarPanel(uiOutput("Box1"),uiOutput("Box2")),
      mainPanel(plotOutput('plot')
      )
    ))
    

    0 讨论(0)
提交回复
热议问题