在Shiny中,我希望在使用selectInput时能够 Select All.

然而,在下面的shinshine应用程序中,当我将selectInput用于列Species,并将选项All用于choices时,shinshine将列转换为number(而不是因子).

library(shiny)
library(palmerpenguins)

# Load the palmerpenguins dataset
data("penguins")

# Define UI
ui <- fluidPage(
  titlePanel("Penguin Species Selector"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("species", "Select Species:",
                  choices = c("All", unique(penguins$species))),
      hr(),
      helpText("Select a species to view its data.")
    ),
    
    mainPanel(
      tableOutput("penguin_table")
    )
  )
)

# Define server logic
server <- function(input, output) {
  output$penguin_table <- renderTable({
    if (input$species == "All") {
      return(penguins)
    } else {
      return(subset(penguins, species == input$species))
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)

当我删除All并使用这个choices = unique(penguins$species)(见下文),shiny 的应用程序将显示列作为一个因素,但现在我不能 Select All

library(shiny)
library(palmerpenguins)

# Load the palmerpenguins dataset
data("penguins")

# Define UI
ui <- fluidPage(
  titlePanel("Penguin Species Selector"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("species", "Select Species:",
                  choices = unique(penguins$species)),
      hr(),
      helpText("Select a species to view its data.")
    ),
    
    mainPanel(
      tableOutput("penguin_table")
    )
  )
)


# Define server logic
server <- function(input, output) {
  output$penguin_table <- renderTable({
    if (input$species == "All") {
      return(penguins)
    } else {
      return(subset(penguins, species == input$species))
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

enter image description here

推荐答案

这是因为factor是以数字形式存储的.如果您在控制台中运行代码,将清楚地看到正在发生的事情.

c("All", unique(penguins$species))
#[1] "All" "1"   "3"   "2"  

一个简单的修复方法是将物种转换为字符,同时将其传递为selectInput.

c("All", unique(as.character(penguins$species)))
#[1] "All"       "Adelie"    "Gentoo"    "Chinstrap"

完整代码—

library(shiny)
library(palmerpenguins)

# Load the palmerpenguins dataset
data("penguins")

# Define UI
ui <- fluidPage(
  titlePanel("Penguin Species Selector"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("species", "Select Species:",
                  choices = c("All", unique(as.character(penguins$species)))),
      hr(),
      helpText("Select a species to view its data.")
    ),
    
    mainPanel(
      tableOutput("penguin_table")
    )
  )
)

# Define server logic
server <- function(input, output) {
  output$penguin_table <- renderTable({
    if (input$species == "All") {
      return(penguins)
    } else {
      return(subset(penguins, species == input$species))
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)

enter image description here

R相关问答推荐

查找满足SpatRaster中条件的单元格位置

当两个图层映射到相同的美学时,隐藏一个图层的图例值

R for循环返回到先前值

二维样条,严格以一个参数递增

R函数,用于生成伪随机二进制序列,其中同一数字在一行中不出现超过两次

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

根据现有列的名称和字符串的存在进行变异以创建多个新列

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

自定义gggraph,使geom_abline图层仅在沿x轴的特定范围内显示

在纵向数据集中创建新行

如何计算每12行的平均数?

通过初始的shiny 应用更新部署的shiny 应用的数据和参数,其中部署的应用程序显示为URL

在shiny 表格中输入的文本在第一次后未更新

以R表示的NaN值的IS.NA状态

按两个条件自动过滤数据

将某个阈值以下的列中的值分类到不同的列中,否则保持该列的原样

在R中使用ggraph包排列和着色圆

如何创建直方图与对齐的每月箱?

从单个html段落中提取键-值对