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)