r - Shiny :基于先前过滤器的自动 SelectInput 值更新

标签 r shiny shiny-server

我正在创建一个 Shiny 的应用程序。目标是最初从数据集创建自定义过滤器。创建该过滤器后,我希望其中一列中的选项在 selectInput 选项卡中动态更改。我遇到的问题是,当我在 Shiny 应用程序的 ui 部分引用新数据集时,它无法识别该数据框。下面是我的 ui 和服务器部件,但我需要手动添加 selectInput 值。我想让它自动改变。

原始应用程序:

用户界面

# 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
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
  })
}
)

这是我的输出:

enter image description here

如何使语言 selectInput 自动/动态更改为所有可能的语言只有我最初输入的那个艺术家?我的尝试在 ui 部分被注释掉了,但是当我运行它时调用:

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

它声明找不到该数据框:artist_filter_complete

最佳答案

我在这里更改了一些东西并删除了一些我认为在这里不需要的按钮,你自己看看是否对你有意义:

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)

编辑:修改为请求输入的文本

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)

关于r - Shiny :基于先前过滤器的自动 SelectInput 值更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348193/

相关文章:

r - 鼠标悬停在 plotly 和 Shiny

r - R 中识别字符串中第一个字符的最佳方法是什么?

r - 如何在 Bookdown 中更改为另一种书目样式

R sink() 消息并输出到同一文件 - 完整性检查

r - 如果 Rmd 文件未更改,则页面不会呈现

r - 隐藏容器时操作按钮不起作用。 Shiny R

r - 如何在R中将30秒的数据聚合为15分钟的数据

R Shiny - 了解更新相互依赖的输入时观察和观察事件之间的区别

r - 如何从 postgresql 数据库中提取数据到我的 shinyapps.io

r - Centos 配置上的 Shiny 服务器