传递标签以选择和返回服务器功能的值

时间:2015-04-30 19:56:59

标签: r shiny dplyr

我想在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())
    })
  }
)

1 个答案:

答案 0 :(得分:2)

好的,我认为这就是你想要的。我将过滤器比较更改为包含以及​​打印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())
    })
  }
)

enter image description here