【问题标题】:Shiny App for generating a summary table of columns based on user input用于根据用户输入生成列汇总表的闪亮应用程序
【发布时间】:2020-03-04 16:12:39
【问题描述】:

我正在尝试生成一个闪亮的应用程序,其中可以为用户从列下拉列表中选择的列生成汇总表。使用的数据框是一个示例数据集。



library(shiny)
library(dplyr)

df <- data.frame(grouping_letter = c('A', 'A', 'B', 'B', 'C', 'C'), 
                 grouping_animal = c('Cat', 'Dog', 'Cat', 'Dog', 'Cat', 'Dog'),
                 value = c(1,2,3,4,5,6))

df <- df %>% mutate(
    grouping_letter = as.character(grouping_letter),
    grouping_animal = as.character(grouping_animal))

# Define UI for application that summarizes a table
ui <- fluidPage(

    selectInput(inputId ="column",
                label = "Choose Column for Summary",
                choices = names(df),
                selected = "grouping_letter"),

    tableOutput('table')
)

# Define server logic required to output summary table
server <- function(input, output) {
    groupvar <- reactive({input$column})
    cols <- c('value', unlist(groupvar))
    dt_f <- df[,cols] %>%
        group_by_at(2) %>%
        summarise (value = n(), yield = round(mean(value)*100, 1)) %>%
        mutate(Pct_of_value = paste0(round(100 * value/sum(value), 0), "%"))
    output$table <- renderTable(dt_f)
}

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

我收到一个错误: 警告:.subset 中的错误:无效的下标类型“列表” 我在 Windows 上使用 R 3.6 和 RStudio

【问题讨论】:

    标签: shiny user-input summary


    【解决方案1】:

    有两个问题

    • groupvar 是响应式的,因此您不想取消列出响应式值。
    • 您需要使用groupvar() 来获取反应对象内的值。但是,为了运行groupvar(),您需要在主动响应式上下文中执行它:例如observeobserveEventrenderXXX。给你一个不太准确的想法是,闪亮的函数允许你使用function({}) 表达式。

    请看我下面的代码,这应该是你想要的。注意我是如何使用renderTable的。

    library(shiny)
    library(dplyr)
    
    df <- data.frame(grouping_letter = c('A', 'A', 'B', 'B', 'C', 'C'), 
                     grouping_animal = c('Cat', 'Dog', 'Cat', 'Dog', 'Cat', 'Dog'),
                     value = c(1,2,3,4,5,6))
    
    df <- df %>% mutate(
        grouping_letter = as.character(grouping_letter),
        grouping_animal = as.character(grouping_animal))
    
    # Define UI for application that summarizes a table
    ui <- fluidPage(
    
        selectInput(inputId ="column",
                    label = "Choose Column for Summary",
                    choices = names(df),
                    selected = "grouping_letter"),
    
        tableOutput('table')
    )
    
    # Define server logic required to output summary table
    server <- function(input, output) {
        groupvar <- reactive({input$column})
    
        output$table <- renderTable({
            df[, c('value', groupvar())] %>%
                group_by_at(2) %>%
                summarise (value = n(), yield = round(mean(value)*100, 1)) %>%
                mutate(Pct_of_value = paste0(round(100 * value/sum(value), 0), "%"))
        })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2019-02-08
    • 2020-01-16
    • 2023-03-07
    • 2014-04-11
    • 2015-11-12
    • 2018-04-30
    • 1970-01-01
    • 2017-05-11
    • 2016-12-09
    相关资源
    最近更新 更多