传递标签以选择并返回服务器函数的值

Passing labels to selectize and returning values for server functions

我想在 Shiny 中将不同的标签传递给 selectizeInput。然后我希望来自 selectize 的用户输入将编码的参数值传递给函数。我将参数代码和标签存储在数据框中。因此,我应该能够使用标签上的逻辑匹配语句访问数据框中的参数字段。但是,我似乎只得到行号作为输出——而不是实际的参数代码。另外,多选不显示。

请看下面的例子:

library(shiny)
library(dplyr)

dropdown_A<-as.data.frame( cbind(labels = c("red", "white", "blue"), parameter = c(800, 72, 9048)))
dropdown_B<-as.data.frame( cbind(labels = c("green", "purple", "orange"), parameter = c("xyz","def","abc")))

shinyApp(
  ui = fluidPage(
    fluidRow(
      wellPanel(
        selectizeInput("A", label = p("Select a color"), choices = as.character(dropdown_A$labels), multiple = TRUE),
        selectizeInput("B", label = p("Select another color"), choices = as.character(dropdown_B$labels), multiple = TRUE))),
    fluidRow(verbatimTextOutput("Value_A")),
    fluidRow(verbatimTextOutput("Value_B"))),
  server = function(input, output, session){
    A<-reactive({ 
      if (is.null(input$A))
        return ("Please select a color")
      else (dropdown_A %>% filter(labels == input$A)%>% select(parameter))
    })   
    B<-reactive({ 
      if (is.null(input$B))
        return ("Please select another color")
      else (dropdown_B %>% filter(labels == input$B)%>% select(parameter))
    })  
    output$Value_A<-renderText({
      as.character(A())
    })
    output$Value_B<-renderText({
      as.character(B())
    })
  }
)

好的,我想这就是你想要的。我将您的过滤器比较更改为包含以及​​您打印 data.frames.

的方式
library(shiny)
library(dplyr)

dropdown_A<-as.data.frame( cbind(labels = c("red", "white", "blue"), parameter = c(800, 72, 9048)))
dropdown_B<-as.data.frame( cbind(labels = c("green", "purple", "orange"), parameter = c("xyz","def","abc")))

shinyApp(
  ui = fluidPage(
    fluidRow(
      wellPanel(
        selectizeInput("A", label = p("Select a color"), choices = as.character(dropdown_A$labels), multiple = TRUE),
        selectizeInput("B", label = p("Select another color"), choices = as.character(dropdown_B$labels), multiple = TRUE))),
    fluidRow(verbatimTextOutput("Value_A")),
    fluidRow(verbatimTextOutput("Value_B"))),
  server = function(input, output, session){
    A<-reactive({ 
      if (length(input$A)==0)
        return ("Please select a color")
      else (dropdown_A %>% filter(labels %in% input$A)%>% select(parameter))
    })   
    B<-reactive({ 
      if (length(input$B)==0)
        return ("Please select another color")
      else (dropdown_B %>% filter(labels %in% input$B)%>% select(parameter))
    })  
    output$Value_A<-renderPrint({
      print(A())
    })
    output$Value_B<-renderPrint({
      print(B())
    })
  }
)

我可以获取要显示的参数代码,并通过以下任一方式进行多项选择:

  • 将参数更改为字符(而不是因子),并使用 %in% 而不是 ==,或
  • 通过使用 [ 而不是 %>%

在您的代码中,我已将 A() 更改为使用 dropdown_A 中的字符值,而 B() 使用 [

library(shiny)
library(dplyr)

dropdown_A<-as.data.frame( cbind(labels = c("red", "white", "blue"), parameter = c(800, 72, 9048)))
dropdown_B<-as.data.frame( cbind(labels = c("green", "purple", "orange"), parameter = c("xyz","def","abc")))

dropdown_A$parameter <- as.character(dropdown_A$parameter)

shinyApp(
  ui = fluidPage(
   fluidRow(
      wellPanel(
        selectizeInput("A", label = p("Select a color"), choices = as.character(dropdown_A$labels), multiple = TRUE),
        selectizeInput("B", label = p("Select another color"), choices = as.character(dropdown_B$labels), multiple = TRUE))),
    fluidRow(verbatimTextOutput("Value_A")),
    fluidRow(verbatimTextOutput("Value_B"))),

  server = function(input, output, session){
    A<-reactive({ 
      if (is.null(input$A))
        return ("Please select a color")
     else((dropdown_A %>% filter(labels %in% input$A) %>% select(parameter)))
    })   
    B<-reactive({ 
      if (is.null(input$B))
        return ("Please select another color")
      else (dropdown_B[dropdown_B$labels %in% input$B, "parameter"])
    })  
    output$Value_A<-renderText({
      as.character(A())
    })
    output$Value_B<-renderText({
      as.character(B())
    })
  }
)

这是输出的屏幕截图