conditionals with shiny

Hello, good evening, I am trying to create a shiny app in which there are 2 text boxes where you enter dates and I filter the data by dates. Then I print you a result with the difference between two values, but I have not found a way to transform the functions to a Shiny app. I leave the code with the general idea of what I want to do in the app, in case someone has any idea to introduce these functions to a shiny app:

library(lubridate)
library(dplyr)

df <- data.frame(
  fecha = seq(as.Date("1980-01-01"), as.Date("2020-05-31"), by = "quarter"),
  datos = sample(c(1:100), size = 162, replace = TRUE)
)

dato1 <- df %>% 
  filter(fecha == "2019-07-01")

dato2 <- df %>% 
  filter(fecha == "2017-07-01")

x <- dato1$datos - dato2$datos

if( x < 0 ){
  print(paste("Hubo una entrada de capitales por",abs(x),"millones de dólares"))  
} else {
  print(paste("Hubo una salida de capitales por",abs(x),"millones de dólares"))   
}

Thanks for reading me

Hello, good night everyone. I am trying to make a shiny app with a database that has dates and values. The idea is the following: Have two text boxes in which you enter the dates and that the output is the resulting value of subtracting the two values related to those dates. It occurs to me that I could use the filter() function to be able to get the individual values for each date, but from there I'm not sure how to get the respective output. Does anyone have any ideas I could go on with?

Hi,

In response to your first question, below is an app that can get you started. It seems you do not have a lot of Shiny knowledge yet, so I suggest you check out the tutorials to get started and then ask questions here again with some actual Shiny code, as we of course are not going to write the whole app for you :slight_smile:

library(shiny)
library(dplyr)

#UI
ui <- fluidPage(
  
  sidebarLayout(
    
    sidebarPanel(
      dateRangeInput("dateRange", "Set the date range")
    ),
    
    mainPanel(
      dataTableOutput("myTable")
    )
  )
  
)


#SERVER
server <- function(input, output, session) {
  
  #Load the original data
  df <- data.frame(
    fecha = seq(as.Date("1980-01-01"), as.Date("2020-05-31"), by = "quarter"),
    datos = sample(c(1:100), size = 162, replace = TRUE)
  )
  
  #Set the default values in the UI
  updateDateRangeInput(session, "dateRange",
                       start = min(df$fecha), end = max(df$fecha))
  
  #Filter the table based-ff the user input
  output$myTable = renderDataTable({
    
    df %>% filter(between(fecha, input$dateRange[1], input$dateRange[2]))
    
  })
  
}

shinyApp(ui, server)

Good luck,
PJ

Thanks a lot, you save me

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.