0
votes

I am new to Shiny and trying to do the following- I have a csv file which has two columns- ID and name. On the ui side, the user can enter an ID (textInput). On the server side, I am reading in the csv file as a dataframe and based on the user-defined textInput, I am subsetting the dataframe. I want to present the resulting dataframe (after subsetting) as another user input with a checkbox next to each row. I don't know where to start.

    ui <- fluidPage(
          useShinyjs(), #include shinyjs

           # Application title
           titlePanel(fluidRow(
             column(3, "  ADVANCE DATA RETRIEVAL",align="center"),
             column(2, offset = 0,img(height =90,width=250,src="logo.png",align="left"))

           )),


           sidebarLayout(sidebarPanel(width = 5,
                                      helpText("Please enter a station ID."),textInput("stationID", "IDs", value = ""),
                                      actionBttn("goButton","Go!",color = "default",style = "fill",size = "lg")), 
                                      mainPanel(width = 6,tableOutput("results"))))

    server <- function(input, output, session) {
    TS_metadata<-eventReactive(input$goButton,{
    PRECIP<-read.csv("Precip.csv", header = TRUE)
    Subset_Precip<-subset(PRECIP, PRECIP$ID==input$stationID)
    )}
}

PRECIP.csv look like-

enter image description here

If input$stationID = 17517 then the resulting dataframe has the first two rows and I want the dataframe (with two rows) to appear as a user input where each row has a checkbox next to it. I have presented here a few lines of the code to give an idea as to what I am trying to do. Any help is greatly appreciated. Thanks.

1

1 Answers

0
votes

Here is a brief demo including checkboxes in a DT datatable. This is based on a solution provided in a github response by yihui.

This approach will keep your subsetted data in a reactiveVal for use. Checkboxes are made dynamically and use the entered ID number for the checkbox inputs. For example, selected ID 20546 will make 20546_1 for 1st checkbox, 20546_2 for 2nd, etc. so each is unique as you change data files (just to demonstrate).

Another output shows how you can retrieve the checkbox results (TRUE/FALSE).

library(shiny)
library(DT)
library(shinyjs)
library(shinyWidgets)


ui <- fluidPage(
    useShinyjs(), #include shinyjs
    # Application title
    titlePanel(
      fluidRow(
        column(3, "  ADVANCE DATA RETRIEVAL",align="center"),
        column(2, offset = 0,img(height =90,width=250,src="logo.png",align="left"))
    )),
    sidebarLayout(
      sidebarPanel(
        width = 5,
        helpText("Please enter a station ID."),textInput("stationID", "IDs", value = ""),
        actionBttn("goButton","Go!",color = "default",style = "fill",size = "lg")), 
      mainPanel(
        width = 6,
        DT::dataTableOutput("results"),
        verbatimTextOutput('cbinfo')
      )
    )
  )

server = function(input, output, session) {

  Subset_Precip <- reactiveVal(NULL)

  TS_metadata <- observeEvent(input$goButton,{
    PRECIP<-read.csv("Precip.csv", header = TRUE)
    Subset_Precip(subset(PRECIP, PRECIP$ID==input$stationID))
  })

  # create a character vector of shiny inputs
  shinyInput = function(FUN, len, id, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = NULL, width = 1, ...))
    }
    inputs
  }

  # obtain the values of inputs
  shinyValue = function(id, len) {
    unlist(lapply(seq_len(len), function(i) {
      value = input[[paste0(id, i)]]
      if (is.null(value)) NA else value
    }))
  }

  dataCB <- reactive({
    if (is.null(Subset_Precip())) return(data.frame(NULL))
    data.frame(
      v1 = shinyInput(checkboxInput, nrow(Subset_Precip()), paste0(input$goButton, '_'), value = TRUE),
      v2 = Subset_Precip()$ID,
      v3 = Subset_Precip()$Name,
      stringsAsFactors = FALSE
    )
  })

  # render the table containing shiny inputs
  output$results = DT::renderDataTable(
    dataCB(), server = FALSE, escape = FALSE, selection = 'none', options = list(
      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
    )
  )

  # print the values of inputs
  output$cbinfo = renderPrint({
    if (is.null(Subset_Precip())) return (NULL)
    data.frame(v1 = shinyValue(paste0(input$goButton, '_'), nrow(Subset_Precip())))
  })
}

shinyApp(ui, server)