【问题标题】:How to connect the output from shiny selectInput with a datatable如何将闪亮的 selectInput 的输出与数据表连接起来
【发布时间】:2020-02-28 16:09:54
【问题描述】:

我对 R 比较陌生,目前我正在尝试构建一个简单的 Shiny 应用程序。

我相信输入是好的,但是,我的输出似乎不能正常工作。

我的应用应该允许用户选择他们想要使用的成分数量,并且输出应该给出具有特定数量成分的食谱的所有名称。

如何将输入连接到所需的输出?

ui <- fluidPage(
  titlePanel("Foodify"),

  #Input
  selectInput("number_of_ingredients", "How many ingredients would you like to use?",
              choices = c(dt.ingredients.and.directions.recipe$dt.number.of.ingredients), selected = 5, selectize = TRUE),
  mainPanel(textOutput("ingredients")
))

server <- function(input, output){
  ingredients.data <- reactive({as.data.frame(dt.ingredients.and.directions.recipe)})

  recipes <- reactive(ingredients.data()[which(row.names(ingredients.data()) == input$number_of_ingredients),])

  output$ingredients <- renderPrint({ingredients.data()$Recipe_name})
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我认为你可以简化你的应用程序。

    您的食谱数据为reactive - 需要吗?如果您的数据已经存在于数据框中,则可以在单独的 reactive 块或 output 中对其进行过滤。

    这是一个简化事情的简短示例(在output 中过滤您的数据框)。如果您的input 更改(不同数量的配方),文本输出将自动更新。

    这能满足您的需求吗?

    dt.ingredients.and.directions.recipe <- data.frame(
      dt.number.of.ingredients = c(1,2,3),
      Recipe_name = c("First", "Second", "Third"),
      stringsAsFactors = F
    )
    
    ui <- fluidPage(
      titlePanel("Foodify"),
      #Input
      selectInput("number_of_ingredients", "How many ingredients would you like to use?",
                  choices = unique(dt.ingredients.and.directions.recipe$dt.number.of.ingredients), 
                  selected = 1, 
                  selectize = TRUE),
      mainPanel(textOutput("ingredients")
      )
    )
    
    server <- function(input, output){
      output$ingredients <- renderPrint({
        dt.ingredients.and.directions.recipe[dt.ingredients.and.directions.recipe$dt.number.of.ingredients == input$number_of_ingredients, "Recipe_name"]  
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    如果您想使用单独的reactive 块进行过滤,您还可以执行以下操作:

    server <- function(input, output){
      recipes <- reactive({
        dt.ingredients.and.directions.recipe[dt.ingredients.and.directions.recipe$dt.number.of.ingredients == input$number_of_ingredients,]
      })
      output$ingredients <- renderPrint({
        recipes()$Recipe_name  
      })
    }
    

    编辑(3/1/20)

    配方结果的显示方式具有灵活性。现在,这是使用renderPrint,它只捕获任何打印输出并将其转换为字符串。

    有多种显示数据的替代方法。一种方法是改用renderTable(并在您的ui 中替换为tableOutput 而不是textOutput。还可以查看闪亮的DT 包。

    这将在单列中显示配方结果:

    library(shiny)
    
    dt.ingredients.and.directions.recipe <- data.frame(
      dt.number.of.ingredients = c(7,2,7,8,6),
      Recipe_name = c("Jam Toaster Tarts", "Oven-Dried Strawberries", "Fried Whole Fish", "Veggie Italian Hoagies", "Buttered Tomatoes with Ginger"),
      stringsAsFactors = F
    )
    
    ui <- fluidPage(
      titlePanel("Foodify"),
      #Input
      selectInput("number_of_ingredients", "How many ingredients would you like to use?",
                  choices = sort(unique(dt.ingredients.and.directions.recipe$dt.number.of.ingredients)), 
                  selected = 1, 
                  selectize = TRUE),
      mainPanel(tableOutput("ingredients")
      )
    )
    
    server <- function(input, output){
      output$ingredients <- renderTable({
        data.frame(Recipe = dt.ingredients.and.directions.recipe[dt.ingredients.and.directions.recipe$dt.number.of.ingredients == input$number_of_ingredients, "Recipe_name"])  
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 嗨,本,感谢您的解决方案!它似乎有效,但是,食谱现在彼此相邻显示,有没有办法可以将它们显示在列表中?
    • @Mike 请查看已编辑的答案,例如在单列中显示配方结果。有很多显示结果的选项,看看哪些适合你。
    猜你喜欢
    • 2020-08-16
    • 2019-03-18
    • 2018-04-20
    • 2016-10-08
    • 1970-01-01
    • 2015-09-21
    • 2019-07-27
    • 2019-01-24
    • 2015-05-07
    相关资源
    最近更新 更多