我已经从iris数据集创建了一个汇总表,当我从datatable
中选择任何一行时,我想使用所选行来分配主数据,然后将该子集数据发布为datatable
library(shiny)
library(dplyr)
library(DT)
setwd("C:/Users/143812/Documents/Shiny")
df <- iris
vchoices <- colnames(df)
ui <- fluidPage(h1("PLOT FOR NOW"),
sidebarLayout(sidebarPanel(
fluidPage(
column(10,selectInput(inputId = "group",label = "Group BY",choices = vchoices)),
column(10,selectInput(inputId = "operator",label = "OPERATOR",choices = c("sum","mean"),selected = "sum")),
column(10,uiOutput("operateUI"))
)
),
mainPanel(dataTableOutput("summarytable"),dataTableOutput("drilldata")))
)
server <- function(input,output,session){
df1 <- reactive({df %>% group_by_(input$group) %>% summarise(Result = get(input$operator)(get(input$operate)))})
#input for integer and numeric columns
a <- (sapply(df,class))
lv <- a=="integer" | a=="numeric"
b <- a[lv]
numchoice <- names(b)
output$operateUI <- renderUI({
selectInput(inputId = "operate",label = input$operator,choices = numchoice)
})
#publish summary table to select output from by name summary table
output$summarytable <- DT::renderDataTable(df1())
#create data for drill report
drilldata <- reactive({
shiny::validate(need(length(input$summarytable_rows_selected) > 0, "Select Rows to drill down")
)
selected_column <- df1[as.integer(input$summarytable_rows_selected),]$get(input$group)
df[df$get(input$group) %in% selected_column, ]
})
output$drilldata <- DT::renderDataTable(drilldata())
#function end
}
shinyApp(ui,server)
这段代码给了我错误:'closure'类型的对象不是子集。 我要求用户选择group by和要执行的操作列,sum或mean以及要执行操作的列。这个方法似乎在mlegge的代码中工作,其中值是预定义的而不是用户询问的。 link to question
答案 0 :(得分:1)
您的错误消息很可能来自此行
selected_column <- df1[as.integer(input$summarytable_rows_selected),]$get(input$group)
您在df1之后忘记了功能括号()
。对我来说,get()没有用,所以我使用[[
代替
尝试
selected_column <- df1()[as.integer(input$summarytable_rows_selected),][[input$group]]
我也有这条线的问题
df[df$get(input$group) %in% selected_column, ]
并将其更改为
df[df[[input$group]] %in% selected_column, ]
它正在为我工作