I just want that the actionButton works when I click directioning the code ahead

Hi!
I'm a begginer at R and Shiny.
I just want that the actionButton works when I click directioning the code ahead...
It's a interface where the user makes the archive.csv upload, and click on "Submmit" botton and the things continue to work ahead.
This is the code:

library(shiny)
library(bslib)
library(rmarkdown)

light <- bs_theme()

# user interface
ui <- fluidPage(
  
  theme = light,
  tags$title("WS_musteR"),
  titlePanel("MusteR"),
  includeCSS("html/style.css"),
  includeHTML("html/index.html"),
  
  sidebarLayout(
    sidebarPanel(
      fileInput("filecsv", "Insira a tabela CSV",
        multiple = FALSE,
        accept = c(
          "text/csv",
          "text/comma-separated-values,text/plain",
          ".csv"),
        width = NULL,
        buttonLabel = "Browse...",
        placeholder = "No file selected"
      ),
      
      actionButton("action", "Submeter", class = "btn-success"),
    ),
    mainPanel(
      tableOutput("contents")
      #plotOutput(outputId = "distPlot")
    ),
  ),
)

# server side
server <- function(input, output) {
  
    output$contents <- renderTable({
    observeEvent(input$filecsv, {
    file <- input$filecsv
    if (is.null(file)) {
      stop("Entrada Vazia!")
    } else {
      ext <- tools::file_ext(file$datapath)
      req(file)
      validate(need(ext == "csv", "ERROR: Please upload a .csv file!"))
      x <- read.csv(file$datapath, header = input$header)
      print("Running code...")
      source(paste0(bibpath,"main-ligs-v1_WS.R"))
    }})
  })
  
}

shinyApp(ui = ui, server = server)

In the example below, I rearranged your code to end up with an app that gives two outputs when the Submeter button is clicked. If a file has not been uploaded, it will show "Entrada Vazia!", otherwise it will show "Running code..." (both as table outputs).

I preserved your code as much as possible, commenting out lines that would cause it to fail on my end (but would work for you - i.e. html/style.css). In the server section, I pulled the entire observeEvent() chunk out of the renderTable() function to create two different chunks. Since the desired action is based on the button click, I also updated observeEvent(input$action, ...), now looking at the button instead of the file input. Finally, to show the functionality, I created a reactive value to ultimately display in the renderTable(). When the button is clicked, the reactive value is updated based on the logic checking file input as NULL or not.

library(shiny)
library(bslib)
library(rmarkdown)

light <- bs_theme()

# user interface
ui <- fluidPage(
  
  theme = light,
  tags$title("WS_musteR"),
  titlePanel("MusteR"),
  # includeCSS("html/style.css"),
  # includeHTML("html/index.html"),
  
  sidebarLayout(
    sidebarPanel(
      fileInput("filecsv", "Insira a tabela CSV",
                multiple = FALSE,
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv"),
                width = NULL,
                buttonLabel = "Browse...",
                placeholder = "No file selected"
      ),
      
      actionButton("action", "Submeter", class = "btn-success"),
    ),
    mainPanel(
      tableOutput("contents")
      #plotOutput(outputId = "distPlot")
    ),
  ),
)

# server side
server <- function(input, output) {
  
  # observe the button click
  observeEvent(input$action, {
    file <- input$filecsv
    if (is.null(file)) {
      # update the reactive value with this message
      message$d <<- data.frame(message = 'Entrada Vazia!')
    } else {
      ext <- tools::file_ext(file$datapath)
      req(file)
      # validate(need(ext == "csv", "ERROR: Please upload a .csv file!"))
      # x <- read.csv(file$datapath, header = input$header)
      # update the reactive value with this message
      message$d <<- data.frame(message = 'Running code...')
      # source(paste0(bibpath,"main-ligs-v1_WS.R"))
    }})
  
  # object being updated with the appropriate message
  message = reactiveValues(d = NULL)
  
  # render the message
  output$contents <- renderTable({
    message$d
  })
  
}

shinyApp(ui = ui, server = server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.