Hi Friends ,
Can you please help to correct the errors in below code as im unable to use group_by and nest in shiny. Below is simplified example.
library(tidyverse)
library(shiny)
x<-rnorm(10,100,20)
y<-c("a","a","a","a","a","b","b","b","b","b")
t<-data.frame(x,y)
ui<-fluidPage(
mainPanel(
selectInput("Grp","Input Group",choices=names(t)),
tableOutput("table_1")
)
)
Fraction<-function(df){
df/sum(df)
}
server <-function(input,output){
nested<-reactive({
req(input$Grp)
t %>%
group_by(input$Grp)%>%
nest()
})
output$table_1<-renderTable({
map(nested()$data,Fraction)
})
}
shinyApp(ui=ui,server=server)
'''
----------------------
The normal R code works fine that is similar to above
x <- rnorm(10, 100, 20)
y <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b")
t <- data.frame(x, y)
t1 <- t %>% group_by(y) %>% nest()
Fraction <- function(df) {
df / sum(df)
}
t2 <- map(t1$data, Fraction)
t2