更改数据集时,在闪亮的应用程序中动态显示列名会闪烁错误

用户29609

我有一个闪亮的应用程序,我希望允许用户根据一组上传的文件选择一个数据集,然后指定要从所选数据集中显示的列。如果我选择某些列然后切换数据集,则会闪烁错误并输出到控制台,指出在应用程序切换数据集并正确显示之前,所选列未知。然而,在我的完整应用程序中,应用程序崩溃了,但我无法弄清楚如何重现崩溃。我认为这可能与一些预处理有关,这些预处理是为了添加跨数据集相同并保持选中状态的附加列,但是如果没有该功能,错误是相同的。

library(shiny)
library(tidyverse)
library(DT)


ui <- fluidPage(
  checkboxGroupInput("select_var", label = "Select Variables"),
  selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
  DT::dataTableOutput("table")

)


server <- function(session, input, output) {
  # define the dataset
  data <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})

  # add a common column name that is always selected
  dataprocessed <- reactive({data <- data()
                             data$num <- seq(1:nrow(data))
                             return(data)})

  # dynamically generate the variable names
  observe({
    vchoices <- names(dataprocessed())
    updateCheckboxGroupInput(session, "select_var", choices = vchoices, selected = c("num"))
  })

  # select the variables based on checkbox
  data_sel <- reactive({
    req(input$select_var)
    df_sel <- dataprocessed() %>% select(input$select_var) 
      })

  output$table <- DT::renderDataTable(data_sel())
}

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

我们可以添加一个条件要求req(),用于在渲染之前测试列是否存在:

library(shiny)
library(tidyverse)
library(DT)

ui <- fluidPage(
  checkboxGroupInput("select_var", label = "Select Variables"),
  selectInput("dataset", label = NULL, choices = c("mtcars", "rock")),
  DT::dataTableOutput("table")

)

server <- function(session, input, output) {
  # define the dataset
  data <- reactive({
    switch(input$dataset,"rock" = rock,"mtcars" = mtcars)
  })

  # add a common column name that is always selected
  dataprocessed <- reactive({
    data <- data()
    data$num <- seq(1:nrow(data))
    return(data)
  })

  # dynamically generate the variable names
  observe({
    vchoices <- names(dataprocessed())
    updateCheckboxGroupInput(session, "select_var", choices = vchoices, selected = c("num"))
  })

  # select the variables based on checkbox
  data_sel <- reactive({
    req(input$select_var)
    req(names(dataprocessed()) %in% input$select_var)
    a <- names(dataprocessed())[names(dataprocessed()) %in% input$select_var]
    df_sel <- dataprocessed() %>% select(a) 
  })

  output$table <- DT::renderDataTable(data_sel())
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在闪亮的DT数据表中更改数据集时保留选定的行

仅在将数据加载到闪亮的应用程序中时显示框

如何在phonegap应用程序后端sqlite中动态显示图表?

通过Play框架2中的配置动态显示应用程序名称

如何在 django 应用程序中动态显示消息?

动态显示列名

当我更改数据库时,为什么我的 React 应用程序在 useEffect 中运行 firebase 查询?

通过闪亮应用程序中列名称的组合来设置数据框

当TabPanel干净时在闪亮的应用程序中显示文本,在显示输出时将其隐藏

VBA,应用程序定义或对象错误,更改数据透视筛选器

如何在闪亮的Golem应用程序中显示动态生成的PDF文件

在闪亮的应用程序中显示数据表,以checkboxGroupInput为条件显示列

闪亮的应用程序部署-错误(无法更改工作目录)

如何在Rails应用程序中更改数据库

在闪亮的应用程序中动态调整ggvis图的大小

在闪亮的应用程序中动态选择和缩放变量

如何使用反应数据更改闪亮应用程序中的 flexdashbord::gauge 标签?

读取多个文件时,ggplot图形未显示在闪亮的应用程序中-无法打开连接

修复初始闪亮应用程序加载时的selectInput错误

在闪亮的应用程序中同时显示多个数据表行范围

在textBlock中动态显示数据

在闪亮的应用程序中更改validate()中文本的颜色

在闪亮的应用程序中更改标签内的字体

在闪亮的应用程序中更改 tabPanels 名称的字体

在闪亮的应用程序的 selectizeInput 中更改边框阴影的颜色

在闪亮的应用程序中以模式显示dataTableOutput

在闪亮的应用程序中并排显示多个地块

在闪亮的应用程序中显示文档页面

使用Shinydashboard在闪亮的应用程序中未显示输出