0
votes

I am new to R shiny and having a struggle with it.

I have data set called 'performance' similar to below.

Date         A       B
2020-08-01  50      100 
2020-08-03  50.95   34.9695
2020-08-03  54      1.39927
2020-09-01  150     350
2020-09-05  96.03   129.96
2020-09-07  68.41   35.9961 
2020-10-01  75      200
2020-10-05  72.41   67.125  
2020-11-01  35   250

I want to make reactive for date and A column If user select 2020-08-01~2020-08-03 in 'dateRangeInput' and type in 50 in 'numericinput' section then I want output of table should look like as below.

Date         A       B
2020-08-01  50      100 
2020-08-03  50.95   34.9695

Here is the code I got so far. I am not sure whether how to make two different reactive in R shiny

library(shiny)
   

ui <- fluidPage(

  title = "Performance Table",

 dateRangeInput("daterange", "choose date",
                start = min(performance$Date),
                end = max(performance$Date),
                min = min(performance$Date),
                max = max(performance$Date),
                format = "yyyy/mm/dd",
                seperator="-"),

  numericInput("num", "A", value = 0, min=0, max=500),

  sidebarLayout(

    sidebarPanel(

      conditionalPanel(

        'input.dataset === "performance"',

        checkboxGroupInput("show_vars", "Columns in performance:",

                           names(performance), selected = names(performance))

      ),
       

    mainPanel(

      tabsetPanel(

        id = 'dataset',

        tabPanel("performance", DT::dataTableOutput("mytable1")),
     

      )

    )

  )

)

 

server <- function(input, output) {
          

 filteredData <- reactive({
      req(input$daterange)
      df <- performance[performance$Date >= input$daterange[1] &
                        performance$Date <= input$daterange[2] &
                        performance$A == trunc(input$num), ]

     df[sample(nrow(df), 1000, replace = T), ]

    })


  output$data <- renderTable({
      performance[, c("performance", input$show_vars), drop=FALSE]
  }, rownames=TRUE)
  output$mytable1 <- DT::renderDataTable({

    DT::datatable(filteredData()[, input$show_vars, drop = FALSE])

  })
                 

}   
 
shinyApp(ui, server)

when I run above code and select date 2020-08-01~2020-08-03 and type 50 in numericinput then I get output as below which is different to what I expect for the output. Is there a way that I can see the decimal values as well?

Date         A       B
2020-08-01  50      100 
1

1 Answers

1
votes

I revised the previous version I had created for you (which was working) to include a numericInput.

In server, if you want to filter based on column A, you can add:

performance$A == input$num

Note that the filtering will be done before you sample.

If you want to match numbers and ignore decimal places, you can truncate with trunc:

trunc(performance$A) == trunc(input$num)

This would ignore decimal places of the A column in the data, as well as the number entered in numericInput.

Also, in your first version you only have one value from input$num. This differs from dateRangeInput, where you have a vector of length two as a result, and use input$daterange[1] and input$daterange[2] when utilizing the result.

ui <- fluidPage(
  title = "Performance",
  dateRangeInput("daterange", "Date range:",
                 start = "2020-08-01",
                 end   = "2020-11-01"),
  numericInput("num", "A", value = 0, min = 0, max = 500),
  sidebarLayout(
    sidebarPanel(
        checkboxGroupInput("show_vars", "Columns in diamonds to show:",
                           names(performance), selected = names(performance)
                           )
    ),
    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("diamonds", DT::dataTableOutput("mytable1"))
      )
    )
  )
)
  
server <- function(input, output) {
  
  filteredData <- reactive({
    req(input$daterange)
    df <- performance[performance$Date >= input$daterange[1] & 
                      performance$Date <= input$daterange[2] & 
                      trunc(performance$A) == trunc(input$num), ]
    df[sample(nrow(df), 1000, replace = T), ]
  })
  
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(filteredData()[, input$show_vars, drop = FALSE])
  })
  
}

shinyApp(ui, server)