How to show and save data read from a file and new generated data in a table in turn

Please tell me where is my mistake. In the application, one button at a time creates a new table, it should be displayed. Or I click upload file, and the table from the file should also be output. Either a new table or a table from a file should be shown. This does not work. Then the table needs to be saved to a file. To simplify the task, I removed all the code for editing the loaded table.

#========================
makeTab <- function(x=4, y=4, val = NA) {
  tb <- data.frame(Param = rep(x = val, times = y))  
  if (x > 1) {
    for (i in 2:x) {
      tb <- data.frame(tb, Param = rep(x = val, times = y))
    }
  }
  return(tb)
}
  
# ========================
library(shiny)

ui <- fluidPage(
  
  #-------- generate new table
  actionButton(
    inputId = 'newtabl', 
    label = 'New table'
  ),
    
  br(),
  br(), 
  
  fileInput(
    inputId = 'readtabl', 
    label = 'Load table',
    multiple = FALSE,
    buttonLabel = 'Select table',
    placeholder = 'no tables',
    accept = ".csv"
    ),
  
  tableOutput(
    outputId = "tabl"
  ),
  
  downloadButton(
    outputId = "writetabl",
    label = 'Save table'
  )
)


# ==================================
server <- function(input, output) {
  
  # new variable to be initialized in different ways
  tb2 <- reactiveVal(value = NA)
  
  #---------- new table
  newtb <- eventReactive(
    eventExpr = input$newtabl,
    valueExpr = {
      r <- makeTab(x = 5, y = 6, val = 7)
      # tb2(r)   # does not work
  })

  loadtb <- eventReactive(
    eventExpr = input$readtabl,
    valueExpr = {
      req(input$readtabl)
      z <- read.table(
       file = input$readtabl$datapath,
       header = TRUE,
       sep = ';',
       dec = '.'
      )
    # tb2(z) # does not work
  })
    
  # ================================= save table to file
  output$writetabl <- downloadHandler(
    filename = function() {
       input$readtabl$name
    },
  
  content = function(file) {
        write.table(
          # x = tb2(),
           x = loadtb(),
          # x = newtb(),
          file = file,
          append = FALSE,
          sep = ';',
          dec = '.'
        )
    })   
        
   output$tabl <- renderTable(
     expr = {
        # tb2()
         loadtb()
        # newtb()
   })
}

shinyApp(ui = ui, server = server)

Hello Korall,

This is what I run your code

image

It can read csv file and render it to table although the table format does not look good.

This is my code to read the table and show it. The only difference is that I put it in the main panel. I am not familiar with how to write the table. So I have no comment yet.

ui <- fluidPage(
sidebarLayout(
conditionalPanel(
condition = "input.first_user == 1", # Example condition
fileInput("file", "upload your saved file")
),
)
mainPanel(
conditionalPanel(
condition = "input.first_user == 1", # Example condition
tableOutput("data"),
),

yu.cai.tch thank you! I understood your idea with the Conditional Panel, this is the solution, great!

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.