Home > OS >  How to pass an Input inside a max() function in R studio (shiny App)?
How to pass an Input inside a max() function in R studio (shiny App)?

Time:12-20

I am trying to pass the input input$sel into the max() function max(dataf$get(input$sel)) Known that dataf is data frame that has values.

My goal is that I get the max value from a column from dataf through the select input input$sel.

server <- function(input, output, session) {
  #Summarize Data and then Plot 
  data <- reactive({
    req(input$sel)
    df <- dataf %>%  
      group_by(code) %>% 
      summarise(output = get(input$sel))
    print(df)
  })
  
  #Plot 
  output$plot <- renderPlot({  
    g <- ggplot(data(), aes(y = output, x = code) ) 
    g   geom_bar( stat = "sum")
    
  })
}

ui <- basicPage(
  
  selectInput(inputId = "sel",
              label = "eine möglichkeit auswählen",
             
               list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),
 
   plotOutput("plot")

######   here is my approuch   ##########

max(dataf$get(input$sel))


)

CodePudding user response:

Input values are only available inside the app server. More properly, they need reactive contexts provided by observe/reactive.

Try this instead:

Note: I used iris dataset as dummy data to make the code reproducible.

library(shiny)
library(tidyverse)

ui <- basicPage(
  selectInput(
    inputId = "sel",
    label = "eine möglichkeit auswählen",
    choices = names(iris)
  ),
  # list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),

  plotOutput("plot")
)

server <- function(input, output, session) {
    # Summarize Data and then Plot
    data <- reactive({
        req(input$sel)
        df <- iris %>%
            group_by(Species) %>%
            summarise(output = max(get(input$sel)))
        print(df)
        df
    })
    
    
    
    # Plot
    output$plot <- renderPlot({
        g <- ggplot(data(), aes(y = output, x = Species))
        g   geom_bar(stat = "sum")
    })
}


shinyApp(ui, server)

  • Related