我正在尝试从R Shiny应用程序中生成的临时表中引用特定值。
这个想法是用户可以通过提示创建输入,输入将用作更大数据帧的过滤器,然后过滤后的结果将显示为表格。
显示过滤后的表格,我想引用此临时表格中的特定列并打印/呈现值。
我当前的尝试显示指定列的值,但由于它引用了完整的数据帧(dfIris),因此正在打印所有值,而不是仅显示临时表中显示的值。
在下面显示的例子中(setosa / 5.3),我只希望在“匹配的花瓣宽度”框中显示“0.20”:
我试图使用'requestedData','flowerFilter'和'flowerTable'但没有成功。
以下是可重现的Shiny应用程序示例:
library(shiny)
library(shinydashboard)
dfIris <- iris
########
# UI #
########
ui <- fluidPage(
# Copy the line below to make a select box
selectInput("species_value", label = h3("Select Species"),
choices = list("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
selected = 1),
selectInput("sepal_length", "Select Sepal Length", choices = NULL),
fluidRow(column(width = 8, box(title = "Matching Flowers", width = NULL, status = "primary",
div(style = 'overflow-x: scroll', tableOutput("flowerTable"))))),
br(),
br(),
fluidRow(column(width = 4, box(title = "Matching Petal Width", width = NULL, status = "primary",
verbatimTextOutput("width"))))
)
############
# Server #
############
server <- shinyServer(function(session,input, output) {
observe({
print(input$species_value)
validLengths <- dfIris$Sepal.Length[dfIris$Species == input$species_value]
updateSelectInput(session, "sepal_length", "Select Sepal Length", choices = unique(validLengths))
})
requestedData <- reactive(subset(dfIris, dfIris$Species == input$species_value
& dfIris$Sepal.Length == input$sepal_length))
# Generate a table with matching Flowers ----
output$flowerTable <- renderTable({
flowerFilter <- subset(dfIris, dfIris$Species == input$species_value
& dfIris$Sepal.Length == input$sepal_length)},
striped = TRUE , bordered = TRUE, hover = TRUE, align = 'c')
output$width <- renderPrint({ dfIris$Petal.Width})
})
###############
# Shiny App #
###############
shinyApp(ui = ui, server = server)
任何帮助都将不胜感激!
答案 0 :(得分:0)
您需要将flowerFilter
对象提供给表达式中的output$width
并过滤您的输出:
output$width <- renderPrint({
## recreate filtered table
flowerFilter <- subset(dfIris, dfIris$Species == input$species_value
& dfIris$Sepal.Length == input$sepal_length)
## filter output based on what's in the filtered table
dfIris$Petal.Width[dfIris$Petal.Width %in% flowerFilter$Petal.Width]
})