Learn with Kumaran
Learn with Kumaran

Reputation: 105

Rendering "textInput" automatically once the browse button is pressed in R shiny

I'm working on R shiny, and I've included a browse button. What I want is for the "Study info:" to be automatically changed to "Study Summary Calculations" after I upload any file using the browse button. How to implement this in R shiny?

I've included a snippet of code below where I'd want the text to be rendered automatically.

textInput("txt", "Study info:", ""),

Entire Code:

## Only run examples in interactive R sessions
if (interactive()) {
  
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        fileInput("file1", "Choose CSV File", accept = ".csv"),
        
        textInput("txt", "Study info:", ""),
      ),
      mainPanel(
        tableOutput("contents")
      )
    )
  )
  
  server <- function(input, output) {
    output$contents <- renderTable({
      file <- input$file1
      ext <- tools::file_ext(file$datapath)
      
      req(file)
      validate(need(ext == "csv", "Please upload a csv file"))
      
      read.csv(file$datapath, header = input$header)
    })
  }
  
  shinyApp(ui, server)
}

Upvotes: 1

Views: 155

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33442

You can use observeEvent to trigger a call to updateTextInput after input$file1 is changed.

Please check the following:

library(shiny)

## Only run examples in interactive R sessions
if (interactive()) {
  
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        fileInput("file1", "Choose CSV File", accept = ".csv"),
        textInput("txt", "Study info:", ""),
      ),
      mainPanel(
        tableOutput("contents")
      )
    )
  )
  
  server <- function(input, output, session) {
    output$contents <- renderTable({
      
      file <- input$file1
      ext <- tools::file_ext(file$datapath)
      
      req(file)
      validate(need(ext == "csv", "Please upload a csv file"))
      
      read.csv(file$datapath, header = input$header)
    })
    
    observeEvent(input$file1, {
      updateTextInput(inputId = "txt", value = "Study Summary Calculations")
    })
    
  }
  
  shinyApp(ui, server)
}

Upvotes: 1

Related Questions