I'm trying to allow user to select two input one for group by and another for sum in a shiny app. those input will be used to further create a summary table.
Below is the code
library(shiny)
library(dplyr)
library(DT)
df <- iris
vchoices <- colnames(iris)
ui <- fluidPage(h1("PLOT FOR NOW"),
sidebarLayout(sidebarPanel(
fluidPage(
column(10,selectInput(inputId = "group",label = "Group BY",choices = vchoices)),
column(10,selectInput(inputId = "sum",label = "SUM",choices = vchoices,selected = "Sepal.Length"))
)
),
mainPanel(tabsetPanel(tabPanel("table",dataTableOutput("table"))
)))
)
server <- function(input,output,session){
df1 <- reactive({df %>% group_by_(input$group) %>% summarise( fb =sum(input$sum))})
output$table <- DT::renderDataTable(df1())
}
shinyApp(ui,server)
Now i have been able to use input$group with help of group_by_ function but how do I use it for sum in summaries ?
答案 0 :(得分:1)
如评论中所述,您应该使用get()
来获取输入中的值。
您也可以在group_by(get(input$group))
代替group_by_(input$group)
以下是修改后的服务器功能:
server <- function(input,output,session){
df1 <- reactive({
df %>% group_by_(input$group) %>% summarise( fb =sum(get(input$sum)))})
output$table <- DT::renderDataTable(df1())
}
希望它有所帮助!