0

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 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks for your help Ben but somehow for the numericinput it doesn't return what I expected. if I type 50 in the numeric input then it return random number in table (like 100, 60 etc)
A is decimal, would this be matter?
I thought you wanted to filter numbers great than 50 in your example...please clarify what you want to do with the number. What do you expect happen if 50 entered? Please describe more.
Did you only want to include rows where A equals 50, if someone enters "50"?
No, I want to filter the number that user input in the numeric input section. If user select 2020-08-01 and 50 then only first line of data(relevant data that user has inputted) shows in the table and if user select 2020-10-01 and 75 then the only relevant data the user has select shows in the table. That's I am aiming to do
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.