R shiny reactive ggplot geom_bar problem

Hi there. I´m trying to code a simple shiny app with ggplot, but I cannot see why my error when run the plot is: "Tentative of applying a non-function". If anyone can help me I send him/her a lot of likes from Spain. Here is my code:

#CODE

library(dplyr)

Countries= c("España", "Italia", "Francia", "Alemania")
Confirmed= c(2345, 4366, 3255, 3435)
Deaths= c(345,565,345,344)
Recovered= c(345,243,234,654)

df= data.frame(Countries,Confirmed, Deaths, Recovered)
View(df)



#UI

#UI

library(shiny)

ui <- fluidPage(
    titlePanel(title = h4("Ranking by countries affected", align = "center")),
    sidebarLayout(
        sidebarPanel(
            selectInput("data", "1- Select the data you want to know:", choices = c("Confirmed" = 1, "Deaths" = 2,"Recovered"= 3), selected = 1),
            br(),
            sliderInput("number", "2- Select the number of countries you want to rank:", min=1, max=4, value=2),
            br(),
            radioButtons("color", "3- Select the color you want to plot", choices = c("Red","Yellow","Green"), selected = "Green")
        ),
        
        mainPanel(
            
            tabsetPanel(type="tab",
                        tabPanel("Summary", verbatimTextOutput("summary")),
                        tabPanel("Structure", verbatimTextOutput("str")),
                        tabPanel("Data", tableOutput("tab")),
                        tabPanel("Plot", textOutput("obs"), plotOutput("myplot")))
            
            
        )
        
    )
    
)




#SERVER

library(shiny)
library(ggplot2)
library(forcats)

server <- function(input, output) {
    
    
    output$obs <- renderText(
        paste("Number of countries on the plot", input$number)
        
    )
    
    output$summary <- renderPrint({
        colm <- as.numeric(input$data)
        summary(df[, c(1, colm+1)])
        
    })
    
    output$str <- renderPrint({
        colm <- as.numeric(input$data)
        str(df[, c(1, colm+1)])
    })
    
    output$tab <- renderTable({
        colm <- as.numeric(input$data)
        df[, c(1, colm+1)]
    })
    
    
    
    
    
    
#Here is my problem. I want to plot as bars the information. For that I have to use input$number #(number of countries I want), and input$data(the type I want to represent:Confirmed, Deaths or #Recovered). But my output says it is a tentative of a non-function, and I don´t know what else to try...
    
    output$myplot <- renderPlot({
        
        dat <- as.numeric(input$data)
        df <- df[order(-df$dat()),]
        cor <- subset(df[1:input$number,])
        
        ggplot(cor, aes(x=Countries, y=dat()))+
            geom_bar(stat="identity")
        
    })
}

shinyApp(ui, server)

Remove the brackets
y=dat

  output$myplot <- renderPlot({
    
    dat <- as.numeric(req(input$data))
    df <- df[order(-dat),]
    cor <- subset(df[1:req(input$number),])
    
    ggplot(cor, aes(x=Countries, y=dat))+
      geom_bar(stat="identity")
    
  })

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

#Mmmm I´m afraid it gives me the same error. If you have another method I would acept it.
#Thanks for your answer!
#Rubén.

In the UI I changed:

      selectInput("data", "1- Select the data you want to know:", choices = c("Confirmed", "Deaths", "Recovered"), selected = "Confirmed"),

then the server code


server <- function(input, output) {
  output$obs <- renderText(
    paste("Number of countries on the plot", input$number)
  )

  output$summary <- renderPrint({
    colm <- req(input$data)
    select(
      df,
      Countries,
      !!sym(colm)
    ) %>% summary()
  })

  output$str <- renderPrint({
    colm <- req(input$data)
    select(
      df,
      Countries,
      !!sym(colm)
    ) %>% str()
  })

  output$tab <- renderTable({
    colm <- req(input$data)
    select(
      df,
      Countries,
      !!sym(colm)
    )
  })



  # Here is my problem. I want to plot as bars the information. For that I have to use input$number #(number of countries I want), and input$data(the type I want to represent:Confirmed, Deaths or #Recovered). But my output says it is a tentative of a non-function, and I don´t know what else to try...

  output$myplot <- renderPlot({
    dat <- sym(req(input$data))
    df2 <- select(
      df,
      Countries,
      !!dat
    )
    df2 <- arrange(
      df2,
      !!dat
    )

    cor <- filter(
      df2,
      row_number() <= req(input$number)
    )

    ggplot(cor, aes(x = reorder(Countries, !!dat), y = !!dat)) +
      geom_col()
  })
}

Oh that´s Good because there´s a plot! But I think it´s not "reactive"...when you choose "Confirmed", it mean to be ordered by this column and "España" appears always the first in the plot…
Also when I select 4 countries to rank, there´s always 2...
Thank you very much, your answer is helping me a lot!

OMG you are a geniuos! Thank you very much!! I spent so many hours with that with no solution!

You made me so happy! I put you answer as solution. If there´s anything I have to do to give you point just tell me(It´s the first time I´m using this page).

Have a great day!