【问题标题】:Passing labels to selectize and returning values for server functions传递标签以选择和返回服务器函数的值
【发布时间】:2015-07-10 16:27:45
【问题描述】:

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

【问题讨论】:

  • 将您的 else 更改为使用 [ 我得到了代码。例如。 else (dropdown_B[dropdown_B$labels %in% input$B, "parameter"])
  • 并且,将您的 parameter 更改为 character 而不是一个因素也可以。 dropdown_A$parameter &lt;- as.character(dropdown_A$parameter)

标签: r shiny dplyr


【解决方案1】:

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

【讨论】:

  • 谢谢 - 我喜欢上面 topsig 的解决方案,因为它将参数作为列表吐出,这就是我需要将它们输入到我的函数中的方式。感谢您的帮助。
  • 没问题,我也学到了一些东西。顺便说一句,关键是处理警告。 :)
【解决方案2】:

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

  • 将参数更改为字符(而不是因子),并使用%in% 而不是==,或者
  • 使用[ 而不是%&gt;%

在您的代码中,我已将 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())
    })
  }
)

这是输出的截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多