用于根据用户输入生成列摘要 table 的 Shiny App
Shiny App for generating a summary table of columns based on user input
我正在尝试生成一个闪亮的应用程序,其中可以为用户从列下拉列表中选择的列生成摘要 table。使用的数据框是示例数据集。
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 中的错误:无效的下标类型 'list'
我在 windows
上使用 R 3.6 和 RStudio
有两个问题
groupvar
是反应性的,因此您不想取消列出反应性值。
- 您需要使用
groupvar()
来获取反应对象中的值。但是,为了 运行 groupvar()
,您需要在主动反应上下文中进行:如 observe
、observeEvent
或 renderXXX
。给你一个不太精确的想法是闪亮的函数允许你使用 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)
我正在尝试生成一个闪亮的应用程序,其中可以为用户从列下拉列表中选择的列生成摘要 table。使用的数据框是示例数据集。
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 中的错误:无效的下标类型 'list' 我在 windows
上使用 R 3.6 和 RStudio有两个问题
groupvar
是反应性的,因此您不想取消列出反应性值。- 您需要使用
groupvar()
来获取反应对象中的值。但是,为了 运行groupvar()
,您需要在主动反应上下文中进行:如observe
、observeEvent
或renderXXX
。给你一个不太精确的想法是闪亮的函数允许你使用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)