0

I am new to the shiny, I would like to edit different multiple data frames by radio button or selectinput by using rhandsontable package. However, my script can not show other data frame but only the first one, I don't know what is the problem.

ui.R:
library(rhandsontable)
fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("select2", label = h3("Choose to edit"), 
              choices = list("003.csv", "004.csv", "005.csv", 
                             "006.csv", "007.csv"), 
              selected = "003.csv"),
  actionButton("saveBtn", "Save changes")
),
mainPanel(
  rHandsontableOutput("hot")
)))

server.R
values <- reactiveValues() 
setHot <- function(x) values[["hot"]] <<- x 
function(input, output, session) {

 fname <- reactive({
   x <- input$select2
   return(x)
 })

 observe({ 
   input$saveBtn # update csv file each time the button is pressed
   if (!is.null(values[["hot"]])) { 
  write.csv(x = values[["hot"]], file = fname(), row.names = FALSE)
}
})

 output$hot <- renderRHandsontable({ 
   if (!is.null(input$hot)) { # if there is an rhot user input...
  DF <- hot_to_r(input$hot) # convert rhandsontable data to R object 
  and store in data frame
  setHot(DF) # set the rhandsontable values

} else {
  DF <- read.csv(fname(), stringsAsFactors = FALSE) # else pull table from the csv (default)
  setHot(DF) # set the rhandsontable values
}

rhandsontable(DF) %>% # actual rhandsontable object
  hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = TRUE) %>%
  hot_col("Status", readOnly = FALSE)
 })}

I can edit and save the dataframe that it shows the first one 003.csv, however when i use the drop down list to 004.csv, it didn't show the dataframe. please advise.

Peter Chung
  • 1,010
  • 1
  • 13
  • 31

1 Answers1

1

This will write (and possibly overwrite ⚠ any existing file with) dummy data:

for (i in c("003.csv", "004.csv", "005.csv", "006.csv", "007.csv")) {
  write.csv(cbind(V1 = rep(i, 3), Status = "foo"), i, row.names = FALSE)
}

I overhauled server a bit:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        "select2", label = h3("Choose to edit"), selected = "003.csv",
        choices = list("003.csv", "004.csv", "005.csv", "006.csv", "007.csv")
      ),
      actionButton("saveBtn", "Save changes")
    ),
    mainPanel(
      rHandsontableOutput("hot")
    )
  )
)

server <- function(input, output, session) {

  DF <- reactiveVal()

  observe({
    DF(read.csv(input$select2, stringsAsFactors = FALSE))
  })

  observe({
    if (!is.null(input$hot)) DF(hot_to_r(input$hot))
  })

  observeEvent(input$saveBtn, {
    if (!is.null(DF())) write.csv(DF(), input$select2, row.names = FALSE)
  })

  output$hot <- renderRHandsontable({
    rhandsontable(DF()) %>% 
      hot_table(highlightCol = TRUE, highlightRow = TRUE, readOnly = TRUE) %>%
      hot_col("Status", readOnly = FALSE)
  })

}

shinyApp(ui, server)
Aurèle
  • 12,545
  • 1
  • 31
  • 49
  • I got an error when i add these two command in observe. observe({ DF(read.csv(input$select2, stringsAsFactors = FALSE), y <- names(x)[grepl(pattern = "_Date",names(x))], x[,y] <- as.Date(x[,y],format = "%d-%b-%y")) }) – Peter Chung Jan 17 '18 at 02:04
  • This is syntactically probably not what you intended (there is no `x` defined). When reporting an error, it also helps to report it in full (when it occurs, and full error message, stack trace, debugging attempts...). Maybe try: `observe({ x <- read.csv(input$select2, stringsAsFactors = FALSE) ; y <- names(x)[grepl(pattern = "_Date", names(x))] ; x[, y] <- as.Date(x[, y], format = "%d-%b-%y") ; DF(x) })` – Aurèle Jan 17 '18 at 10:44
  • (For readability, better to replace the `;`s with new lines. I put semi colons here because new lines are not allowed in SO comments) – Aurèle Jan 17 '18 at 10:49