我认为你可以简化你的应用程序。
您的食谱数据为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)