Can we have a table displayed without applying any inputs

Hi all,

below is the reprex code. The code is fine, but actually as soon as I open the app, I need only 2 rows of the iris table displayed (Like this should be mandatory without any filter and submit button). then as per the filter and submit button the table should be displayed. can we achieve this?

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(DT)
library(shinycssloaders)
library(DT)
library(rhandsontable)
library(shinyjs)
library(dplyr)
library(shiny)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
selectInput("Tic","",choices = c("","ALL",as.character(iris$Species)),selected = NULL)
actionButton("Submit","Submit")
textOutput("Total")
tableOutput("SUMMARY_GENERAL_table")


  data1 <- eventReactive(input$Submit,{
  if(input$Tic == "ALL"){
  table_display  <- iris
  }

  else {
  table_display <- iris %>% filter(Species %in% input$Tic)
  }
})


output$SUMMARY_GENERAL_table <- renderTable(
                data1()
  )


output$Total <- renderText(
  paste0("Sum ",formatC(as.numeric(sum(data1()[(data1()$Species == "setosa"),]$Sepal.Width))))
)

```


Submit button starts in the app with value 0, each press of submit increases the count.
So you can use that if you want special behaviour before the button is pressed.
Below gives the first two rows of iris before submit is pressed.

output$SUMMARY_GENERAL_table <- renderTable(
  {
          if(! (input$Submit>0))
              table_display  <- iris[1:2,]
          else
                data1()
  }
  )

or

if (input$Submit==0)

if you prefer :smiley:

perfect and thanks a lot.......

Thanks again. This is what I wanted. I was not aware of how to declare the action button :slight_smile: you helped me

Hey hi, just remembered one logic suddenly. I agree with your code. But let’s say I have clicked submit multiple times( greater than 0) but now again I clear all dates and emptied filter as well. What if i have to make the table displayed???(only clicking submit button with all other empty), Sorry I remembered this suddenly :blush:. What do u you think. Do my question make sense?

Hi,

Not sure if I have paste the entire reprex. Please let me know :blush:

Two versions.
first is instant update, no need for a submit button

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(DT)
library(shinycssloaders)
library(DT)
library(rhandsontable)
library(shinyjs)
library(dplyr)
library(shiny)

Column {data-width=650}

Chart A

selectInput("Tic", "", choices = c("", "ALL", as.character(iris$Species)), selected = NULL)

textOutput("Total")
tableOutput("SUMMARY_GENERAL_table")

data1 <- reactive({
  if (input$Tic == "") {
    table_display <- iris[1:2, ]
  } else if (input$Tic == "ALL") {
    table_display <- iris
  }

  else {
    table_display <- iris %>% filter(Species %in% input$Tic)
  }
})


output$SUMMARY_GENERAL_table <- renderTable({
 data1()
})


output$Total <- renderText(
  paste0("Sum ", formatC(as.numeric(sum(data1()[(data1()$Species == "setosa"), ]$Sepal.Width))))
)

Second Version still uses submit button

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(DT)
library(shinycssloaders)
library(DT)
library(rhandsontable)
library(shinyjs)
library(dplyr)
library(shiny)

Column {data-width=650}

Chart A

selectInput("Tic", "", choices = c("", "ALL", as.character(iris$Species)), selected = NULL)
actionButton("Submit", "Submit")
textOutput("Total")
tableOutput("SUMMARY_GENERAL_table")


data2 <- reactiveVal(iris[1:2,])

observeEvent(input$Submit,{
  if (input$Tic == "") {
    table_display <- iris[1:2, ]
  } else if (input$Tic == "ALL") {
    table_display <- iris
  }

  else {
    table_display <- iris %>% filter(Species %in% input$Tic)
  }
  
  data2(table_display)
})




output$SUMMARY_GENERAL_table <- renderTable({
 data2()
})


output$Total <- renderText(
  paste0("Sum ", formatC(as.numeric(sum(data2()[(data2()$Species == "setosa"), ]$Sepal.Width))))
)

hmm its ugly copy and pasting rmarkdown as this forum is trying to interpret markdown....
oh well, hope you get the idea

Thanks a lot. But actually, the iris (2 rows) is not displayed once I open the applications.

let me explain :slight_smile:

  1. Your yesterday code was perfect
  2. Only addition I am trying is, later if the user is not selected anything(is.null(input$Tic) and click on submit button the table (iris 2 rows to be displayed). Note : There is a still a need to have the table (iris 2 rows) displayed once I open the app (input$Submit == 0).

I am trying a lot but not able to. Can you please help me

I believe I acheived (2) if filter is blank in my first version. you instantly see iris first two rows.
in the second version after submit on a blank filter i see iris first two rows.

It doesnt work like that on your machine ?

selectInput("Tic", "", choices = c("", "ALL", as.character(iris$Species)), selected = NULL)
actionButton("Submit", "Submit")
textOutput("Total")
tableOutput("SUMMARY_GENERAL_table")


data2 <- reactiveVal(iris[1:2, ])

observeEvent(input$Submit, {
  if (input$Tic == "") {
    table_display <- iris[1:2, ]
  } else if (input$Tic == "ALL") {
    table_display <- iris
  }  else {
    table_display <- iris %>% filter(Species %in% input$Tic)
  }

  data2(table_display)
})


output$SUMMARY_GENERAL_table <- renderTable({
  data2()
})

output$Total <- renderText(
  paste0("Sum ", formatC(as.numeric(sum(data2()[(data2()$Species == "setosa"), ]$Sepal.Width))))
)

Thanks. Your code is perfectly working. But when I replicate the same in my code(Not able to share since it is confidential), everything is working fine. But after playing with the data and finally when I empty the filters(Input$Tic) and click on submit button, the app closes. Not sure why? Actually the data should be displayed.

Does it close/crash also when not using your confidential data but when using the iris dataset?

When I’m using the above code of yours. It’s working fine. But when the same logic is applied to my code. The app closes the moment I click submit button with empty values (Input$Tic). The error I get is

Warning: Error in UseMethod: no applicable method for 'tbl_vars' applied to an object of class "character"

I can only assume that your code is different from the example code we are considering... so its hard to debug what I don't see.

consider this though :

a_char_vector <- c("a","b","c")
a_df <- iris 

dplyr::left_join(iris,a_char_vector)

This returns the same error.
The error relates to using a character vector as if it was a dataframe (tibble).
Are you perhaps making such a mistake somewhere?

Great. Will check, But in your code may i know what is the use of data2 <- reactiveVal(iris[1:2, ]). I mean why reactiveVal is used?

Hi Nir,

Thanks a lot for your help. i am still facing same issue. I have come with similar issue in the below reprex (I mean the app is getting closed once I click on Submit button). Can you please refer it .

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(shiny)
library(shinydashboard)
library(shinycssloaders)
library(DT)
library(rhandsontable)
library(shinyjs)
library(dplyr)
library(formattable)


```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
df <- structure(list(Date = structure(c(1541662915.921, 1541662949.842, 
1541662949.845, 1541662949.845, 1541662993.957, 1541662915.921, 
1541662949.842, 1541662949.845, 1541662949.845, 1541662993.957
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Date1 = structure(c(17843, 
17843, 17843, 17843, 17843, 17843, 17843, 17843, 17843, 17843
), class = "Date"), ID = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L), .Label = c("AAA", "BBB"), class = "factor"), 
    Value = c(68, 70175, 71107, 702175, 72638, 7238, 739, 738469, 
    75901, 7106), Status = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 
    1L, 1L, 2L, 1L), .Label = c("Approved", "Pending"), class = "factor")), row.names = c(NA, 
-10L), class = "data.frame")

dateRangeInput("Date","",start = '', end = '',format = "yyyy-mm-dd",separator = "-")
selectInput("Tic","",choices = c("ALL",as.character(df$ID)),selected = "ALL")
actionButton("Submit","Submit")
textOutput("Total")
tableOutput("table")

data2 <- reactiveVal(df[1:2, ])

     observeEvent(input$Submit,{
      if(input$Date == ""){
      df1 <- df[1:2, ]
    }
      else if (input$Date != ""){
      df1 <- df
      
    } else {
      df1 <- df %>% filter(Date %in% input$Date && ID %in% input$Tic)
    }
    data2(df1)
  })


output$table <- renderTable({
  data2()
})

output$Total <- renderText(
        paste0("$ ",formatC(as.numeric(sum(data2()[(data2()$Status == "Approved"),]$`Net Estimated Transaction Value`)), format="f", digits=2, big.mark=","))
  )

```

use reactiveVal primarily so that other shiny reactives will be promted to reactively recalculate/redraw thenselves when the reactiveVal variable is updated.

first problem is that '' isnt a date in a valid yyyyy-mm-dd format
try

dateRangeInput("Date", "", start = NA, end = NA, format = "yyyy-mm-dd", separator = "-"),

in the observeEvent there are issues with the if conditions being checked for.
for example. input$Date, would be returning a 2 length Date vector. so a check for the equivalent to some date is blank would be

 if (any(is.na(input$Date))) {
      df1 <- df[1:2, ]
    }

recommend library(lubridate) for helpful date functions
for example you can check within dates. Also important that for 'and' condition I use & not && 

```r
  df1 <- df %>% filter(Date %within% lubridate::interval(
        start = input$Date[[1]],
        end = input$Date[[2]]
      )
      & ID %in% input$Tic)