【问题标题】:Trying to have a table update based on user input in Shiny App尝试根据 Shiny App 中的用户输入更新表格
【发布时间】:2021-08-28 13:24:47
【问题描述】:

我试图让它在用户选择标本范围和物种时,表格将只显示该范围。

下面的代码创建表和输入,但两者之间似乎没有通信。我错过了什么?

加载库

library(shiny)
library(tidyverse)
library(tidytext)
library(glue)
library(plotly)

读取数据

iris <- read_rds("iris.rds")

iris

ui <- fluidPage(
  titlePanel("Flower dimensions by species"),
  sidebarLayout(
   sidebarPanel(
      selectInput(
        inputId = "species",
        label = "Select species",
        choices = c("setosa", "versicolor", "virginica")
      ),
      sliderInput(
        inputId = "specimen_selection",
        label = "Select specimen range",
        min = min(iris$id),
        max = max(iris$id),
        value = 50
      )
    ),
    mainPanel(
      dataTableOutput(outputId = "Specimen_details")
    )
  )
)

server <- function(input, output) {
  output$Specimen_details <- renderDataTable(iris)
}

闪亮的应用程序

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny datatable


    【解决方案1】:

    我不知道 iris$id 是什么,但问题是 renderDataTable() 没有“听”的输入,所以表是静态的。

    library(shiny)
    library(tidyverse)
    
    data(iris)
    
    ui <- fluidPage(
        titlePanel("Flower dimensions by species"),
        sidebarLayout(
            sidebarPanel(
                selectInput(
                    inputId = "species",
                    label = "Select species",
                    choices = c("setosa", "versicolor", "virginica"),
                    multiple = TRUE
                )
            ),
            mainPanel(
                dataTableOutput(outputId = "Specimen_details")
            )
        )
    )
    
    server <- function(input, output) {
        output$Specimen_details <- renderDataTable({dplyr::filter(iris, Species %in% input$species)})
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 我试了一下,但 dplyr 部分不起作用。我收到此错误:错误:filter() 输入问题..1。 i 输入..1species %in% input$species。 x 无法访问反应性消费者之外的反应性价值“物种”。 i 你需要包裹在reactive() 或observer() 里面吗?
    • 你的虹膜数据集是什么样的?它似乎与内置虹膜不同。您可以使用dput(head(iris)) 来输出结构。
    • dput(head(iris)) 结构(list(id = 1:6, sepal_length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), sepal_width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), 花瓣长度 = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), 花瓣宽度 = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4), 物种 = 结构 (c(1L , 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"), class= "factor")), row.names = c(NA, 6L), class= "data.frame")
    • 请直接将我的答案复制并粘贴到 R 控制台中,它应该可以工作。请注意,renderDataTable 中的代码用大括号 {} 包裹
    • 我这样做了,但仍然无法正常工作。我收到以下错误:正在侦听127.0.0.1:5431 警告:错误:filter() 输入问题..1。 i 输入..1species %in% input$species。 x 对象“物种”未找到 110:
    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2019-05-05
    相关资源
    最近更新 更多