Shiny: Automatic SelectInput Value Update Based on Previous Filter

若如初见. 提交于 2019-12-09 21:28:13

问题


I am creating a shiny app. The goal is to create a custom filter initially from a dataset. After creating that filter, I want the options from one of the columns to dynamically change in the selectInput tab. The problem I am having is that when I reference the new dataset in the ui section of the shiny app, it doesn't recognize that dataframe. Below are my ui and server parts that work but I need to manually add the selectInput values. I want it to change automatically.

Original App:

ui

# ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Language Selection"),
  sidebarLayout(
    sidebarPanel(
      helpText("The goal from this is to have the select tab automatically update with the language after selecting the artist"),

      helpText("Select your artist"),

      textInput("artistId", "Artist", value = "", width = NULL, 
            placeholder = NULL),

      actionButton("goButton", "Submit Artist"),

      helpText("Based on the artist you selected, now select the Language below to display the numberlist in the main panel."),

      selectInput("selectinputid", "Language to Select:", choices = c("English" = "English", "French" = "French", "German" = "German")),
      ##selectInput("selectinputid", "Language to Select:", choices = artist_filter_complete$Language),
      actionButton("goButton1", "Submit Language")),
mainPanel(
        tableOutput("result")
      )
    )
  )) 

server

##server
library(shiny)

Artist <- c("A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
            "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", 
            "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3")

Language <- c("Spanish", "Spanish", "Spanish", "Spanish", "Spanish", "German", "German", "German", "French", "French", "French", "French",
              "Italian", "Italian", "Italian", "Italian", "Italian", "Polish", "Polish", "Polish", "Israeli", "Israeli", "Israeli", "Israeli",
              "English", "English", "English", "English", "English", "Armenian", "Armenian", "Armenian", "Bengali", "Bengali", "Bengali", "Bengali")

NumberList <- c("uno", "dos", "tres", "cuatro", "cinco", "einz", "zwei", "drei", "un", "deux", "trois", "quatre",
                "uno", "due", "tre", "quattro", "cinque", "jeden", "dwa", "trzy", "achat", "shtaim", "shalosh", "arba",
                "one", "two", "three", "four", "five", "mek", "yerku", "yerek", "shoonno", "ek", "dui", "tin")

df <- data.frame(Artist, Language, NumberList)


shinyServer(function(input, output) {
  output$result <- renderTable({
    randomVals <- eventReactive(input$goButton, input$artistId)
    artist_filter <- c(randomVals())
    artist_filter_complete <- filter(df, Artist == artist_filter)
    randomVals2 <- eventReactive(input$goButton1, input$selectinputid)
    target <- c(randomVals2())
    result_final<-filter(artist_filter_complete, Language %in% target)
    result_final
  })
}
)

This is what my output looks like:

How do I make the language selectInput automatically/dynamically change with all the possible languages with only that artist that I type in initially? My attempt is commented out in the ui section but when I run it calling:

##selectInput("selectinputid", "Language to Select:", choices = artist_filter_complete$Language),

It states that it cannot find that dataframe: artist_filter_complete


回答1:


I changed a few things here and removed a few buttons, which I dont think are necessary here, have a look yourself and see if it makes sense to you:

library(shiny)

Artist <- c("A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
            "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", 
            "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3")

Language <- c("Spanish", "Spanish", "Spanish", "Spanish", "Spanish", "German", "German", "German", "French", "French", "French", "French",
              "Italian", "Italian", "Italian", "Italian", "Italian", "Polish", "Polish", "Polish", "Israeli", "Israeli", "Israeli", "Israeli",
              "English", "English", "English", "English", "English", "Armenian", "Armenian", "Armenian", "Bengali", "Bengali", "Bengali", "Bengali")

NumberList <- c("uno", "dos", "tres", "cuatro", "cinco", "einz", "zwei", "drei", "un", "deux", "trois", "quatre",
                "uno", "due", "tre", "quattro", "cinque", "jeden", "dwa", "trzy", "achat", "shtaim", "shalosh", "arba",
                "one", "two", "three", "four", "five", "mek", "yerku", "yerek", "shoonno", "ek", "dui", "tin")

df <- data.frame(Artist, Language, NumberList)

ui <- shinyUI(
  fluidPage(
    titlePanel("Language Selection"),
    sidebarLayout(
      sidebarPanel(
        helpText("The goal from this is to have the select tab automatically update with the language after selecting the artist"),

        helpText("Select artistId artist"),
        selectInput("artistId", "Artist", choices = unique(df$Artist)),
        helpText("Based on the artist you selected, now select the Language below to display the numberlist in the main panel."),

        selectInput("selectinputid", "Language to Select:", choices = unique(df$Language)),
        actionButton("goButton1", "Submit Language")),
      mainPanel(
        tableOutput("result")
      )
    )
  )
)

server <- function(input, output,session) {

  observeEvent(D1(),{
    updateSelectInput(session, "selectinputid", "Language to Select:",  choices = unique(D1()$Language),selected = unique(D1()$Language)[1])
  })

  D1  <- reactive({
    df[df$Artist %in% input$artistId,]
  })

  D2 <- eventReactive(input$goButton1,{
    D1()[D1()$Language %in% input$selectinputid,]
  })

  output$result <- renderTable({
    D2()
  })
}

shinyApp(ui, server)

Edit: modify to text input by request

library(shiny)

Artist <- c("A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
            "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", 
            "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3")

Language <- c("Spanish", "Spanish", "Spanish", "Spanish", "Spanish", "German", "German", "German", "French", "French", "French", "French",
              "Italian", "Italian", "Italian", "Italian", "Italian", "Polish", "Polish", "Polish", "Israeli", "Israeli", "Israeli", "Israeli",
              "English", "English", "English", "English", "English", "Armenian", "Armenian", "Armenian", "Bengali", "Bengali", "Bengali", "Bengali")

NumberList <- c("uno", "dos", "tres", "cuatro", "cinco", "einz", "zwei", "drei", "un", "deux", "trois", "quatre",
                "uno", "due", "tre", "quattro", "cinque", "jeden", "dwa", "trzy", "achat", "shtaim", "shalosh", "arba",
                "one", "two", "three", "four", "five", "mek", "yerku", "yerek", "shoonno", "ek", "dui", "tin")

df <- data.frame(Artist, Language, NumberList)

ui <- shinyUI(
        fluidPage(
                titlePanel("Language Selection"),
                sidebarLayout(
                        sidebarPanel(
                                helpText("The goal from this is to have the select tab automatically update with the language after selecting the artist"),

                                helpText("Select artistId artist"),
                                textInput("artistId", "Artist", value = "", width = NULL, placeholder = NULL),
                                helpText("Based on the artist you selected, now select the Language below to display the numberlist in the main panel."),

                                selectInput("selectinputid", "Language to Select:", choices = unique(df$Language)),
                                actionButton("goButton1", "Submit Language")),
                        mainPanel(
                                tableOutput("result")
                        )
                )
        )
)

server <- function(input, output,session) {

        observeEvent(D1(),{
                updateSelectInput(session, "selectinputid", "Language to Select:",  choices = unique(D1()$Language),selected = unique(D1()$Language)[1])
        })

        D1  <- reactive({
                df[df$Artist %in% input$artistId,]
        })

        D2 <- eventReactive(input$goButton1,{
                D1()[D1()$Language %in% input$selectinputid,]
        })

        output$result <- renderTable({
                D2()
        })
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/46348193/shiny-automatic-selectinput-value-update-based-on-previous-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!