Home > Mobile >  Shiny reactable - click a row in a table and populate a function to return a result
Shiny reactable - click a row in a table and populate a function to return a result

Time:10-05

I have the following App.

enter image description here

It takes in two variables and passes this to a function myFunction() Normally the user would set the inputs to the function using inputNumeric() - however, I am trying to populate the inputNumeric() with the results from the table. i.e. the user select a row and it takes in the sqft and price from the table/row to populate the function.

How can I change the values of the numericInput() to correspond to the values of what row the user selects? I would also like the user to be able to manually enter their own price and mts2 also.

The results get printed out to the console using the observe - so I am trying to pass them through the numericInput function.

EDIT:

Shiny App

library(shiny)
library(tidyverse)
library(reactable)

##### Load the data

house = read.table("http://www.rossmanchance.com/iscam2/data/housing.txt", header = T, sep = "\t")

myFunction = function(price, mts2){
  p = price / 100
  m = mts2 * 100
  pm = p*m
  return(pm)
}

ui <- fluidPage(
  # fluidRow(selectInput("TheFile", "Select Cohort", 
  #                                    choices = c("Test_Dataset1.csv", "Test_Dataset2.csv", "Test_Dataset3.csv.csv"))),
                fluidRow(column(12, div(dataTableOutput("dataTable")))),
                fluidRow(
                  column(3, numericInput("price", label = "Price?", value = 1000000)),
                  column(3, numericInput("mts2", label = "Mts2?", value = 100)),
                  column(3, actionButton("computefunc", "Compute function"))
                  ),
                fluidRow(reactableOutput("result")),

                
                # Show a plot of the generated distribution
                mainPanel(
                  DT::dataTableOutput("fancyTable")
                  #plotOutput("plot")
                  
                ) # end of main panel
                
)

server <- function(input, output, session) {
  
  myCSV <- reactive({
    # read.csv(input$TheFile)
    house
  })
  
  
  output$fancyTable <- DT::renderDataTable(
    datatable( data = myCSV()
               , extensions = 'Buttons',
               selection = 'single'
    )
  )
  
  observe({
    req(input$fancyTable_rows_selected)
    selRow <- myCSV()[input$fancyTable_rows_selected,]
    print(selRow)
  })
  
  result = eventReactive(input$computefunc, {
    res = myFunction(input$price, input$mts2)
  })
  
  
}


shinyApp(ui = ui, server = server)

CodePudding user response:

Shiny provides the function updateNumericInput() to set/change the value of a numericInput.

For example you can call it from the observe:

  observe({
    req(input$fancyTable_rows_selected)
    selRow <- myCSV()[input$fancyTable_rows_selected,]
    updateNumericInput(session, "price", value = selRow$price)
  })
  • Related