Run a code with an action button in Shiny

Hi,

I'm trying to understand how to simply run a code using an action button with shiny.

For example, I would like to do the following:

  1. upload a table in an excel file using "fileInput"
  2. using an action button to change something in the table (eg. the columns names)
  3. I would like the result to be saved and used later on to apply my code in Shiny as well

I've tried different things but can't figure this out.

Please help me.
All the best

Shiny has quite extensive documentation, with plenty of examples.
There are also in depth tutorials on shiny concepts such as reactivity.
Its hard to answer your issue because its relatively vague so doesnt invite a short answer...

here is actionbutton documentation
https://shiny.rstudio.com/articles/action-buttons.html
https://shiny.rstudio.com/reference/shiny/latest/actionButton.html

Hey,

Thanks for replying.
I understand it seems vague so I'll give an example with a code here.

Unfortunately, I've read the tutorials but can't seem to apply what I want.

Here is a code using dashboard shiny. it aims to upload two tables and then, with the action button to make the rownames of one table as the colnames of the other.

library(shiny)
library(shinydashboard)

# Define UI ----
    sidebar <- dashboardSidebar(
        sidebarMenu(
            menuItem("load data", tabName = "input", icon = icon("dashboard")),
            menuItem("choose parameters", icon = icon("th"), tabName = "parameters",
                     badgeLabel = "new", badgeColor = "green")
            )
        )

    body <- dashboardBody(
        tabItems(
            tabItem(tabName = "input",
                    fileInput("input", "load table 1"),
                    fileInput("input", "load table 2"),
                    actionButton(inputId = "link_tables", "link the two tables together" )
            ),
            
            tabItem(tabName = "parameters",
                    h2("take batches into account"),
                    selectInput("batches", "batches:",
                                colnames(coldata))
        )
    )
)


# Put them together into a dashboardPage
ui <- dashboardPage(
    dashboardHeader(title = "Analysis app"),
    sidebar,
    body
)


# Define server logic ----
server <- function(input, output, session) {
    observeEvent(input$link_tables, {
        rownames( table2 ) <- table2$Sample_Number
        colnames( table1 ) <- table2$Sample_Number
        session$sendCustomMessage(type = 'testmessage',
                                  message = 'proceed to the next tab')
    })
}

# Run the app ----
shinyApp(ui = ui, server = server)

I'm obviously off here, but will be more than happy to get a tip on how to approach this.
Best

ok, there are some obvious problems here.
you want to modify table2 and modify table1 based on some property of table2.
but table2 is not something that is defined anywhere in your code.

I recommend reading the documentation for fileInput, and on using the reactive() convention.

However, if you are looking to load excel files, you should probably begin with a plain R script (i.e. no shiny) in order to write a minimal code, that will open your excel file and interpret it as the sort of dataframe you expect. (readxl package maybe ?)

Ok then, thank you very much. I will check this out.

Best

This topic was automatically closed 54 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.