0

I have been referencing the following post which has been tremendously helpful in helping me understand Rshiny functionality:

How can I update plot from rhandsontable with uploaded data, without clicking into the table first?

I am still having some trouble grasping the concept of "Observe" so that may be the issue here. I want to upload 2 csvs where one is static (saving it in a separate tab) and one that can be edited (with edits reflected in the corresponding plot after hitting a button). My end goal is to be able to plot these data sets together on the same axes, but for now, I am having difficulty getting my table display which makes me think it's not uploading properly. I am using the following code:

library(shiny)
library(rhandsontable)

#sample data
year <- substr(Sys.Date(),1,4)
empty_dat=as.data.frame(matrix(1,nrow = 3,ncol = 4,dimnames = list(c("Cat A", "Cat B", "Cat C"),
                                                                   c(paste("May",year),paste("June",year),paste("July",year),
                                                                     paste("August",year)))))

ui = fluidPage(sidebarLayout(
  sidebarPanel(
    #static data input
    fileInput('file1_new', 'Choose CSV File'),
    #reactive data inut
    fileInput('file1', 'Choose CSV File'),
    #display reactive data
    rHandsontableOutput('contents'),
    actionButton("go", "Plot Update"),
    width=7

  ),
  mainPanel(
      tabsetPanel(
        #plot reactive data first tab
        tabPanel("Plot", plotOutput("plot1")),
        #table static data second tab
        tabPanel("Table", tableOutput("table"))
      )
  )
))


server = function(input, output) {

  #static input
  output$table <- renderTable({

    inFile <- input$file_new

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header = input$header,
             sep = input$sep, quote = input$quote)
  })


  indat <- reactiveValues(data=empty_dat)

  #reactive input
  observe({
    inFile = input$file1
    if (is.null(inFile))
      return(NULL)
    data1 = read.csv(inFile$datapath)
    indat$data <- data1
  })

  observe({
    if(!is.null(input$contents))
      indat$data <- hot_to_r(input$contents)

  })  

  output$contents <- renderRHandsontable({
    rhandsontable(indat$data)
  })


  #***example uses only one column-why I attempt multiple columns (indat$data[,1:4],indat$data[],indat$data) I get an error
  #update data when user hits button
  test <- eventReactive(input$go, {
  return(indat$data[,3])
  })


  output$plot1 <- renderPlot({
    #plot updated data
    plot(test(),type = "l")
  })

}


shinyApp(ui, server)

Any help would be greatly appreciated.

CJJ
  • 75
  • 9

1 Answers1

1

I made few changes to the code, and now the second tab displays the static table. I am not sure if this is what you want but here's the code. The changes that I made are below the code.

library(shiny)
library(rhandsontable)

#sample data
year <- substr(Sys.Date(),1,4)
empty_dat=as.data.frame(matrix(1,nrow = 3,ncol = 4,dimnames = list(c("Cat A", "Cat B", "Cat C"),
                                                                   c(paste("May",year),paste("June",year),paste("July",year),
                                                                     paste("August",year)))))

ui = fluidPage(sidebarLayout(
  sidebarPanel(
    #static data input
    fileInput('file1_new', 'Choose CSV File'),
    #reactive data inut
    fileInput('file1', 'Choose CSV File'),
    #display reactive data
    rHandsontableOutput('contents'),
    actionButton("go", "Plot Update"),
    width=7

  ),
  mainPanel(
    tabsetPanel(
      #plot reactive data first tab
      tabPanel("Plot", plotOutput("plot1")),
      #table static data second tab
      tabPanel("Table", tableOutput("table"))
    )
  )
))


server = function(input, output) {

  #static input
  output$table <- renderTable({

    inFile <- input$file1_new

    if (is.null(inFile))
      {return(mtcars)}
    else{
    read.csv(inFile$datapath)}
  })


  indat <- reactiveValues(data=empty_dat)

  #reactive input
  observe({
    inFile = input$file1
    if (is.null(inFile))
      return(NULL)
    data1 = read.csv(inFile$datapath)
    indat$data <- data1
  })

  observe({
    if(!is.null(input$contents))
      indat$data <- hot_to_r(input$contents)

  })  

  output$contents <- renderRHandsontable({
    rhandsontable(indat$data)
  })


  #***example uses only one column-why I attempt multiple columns (indat$data[,1:4],indat$data[],indat$data) I get an error
  #update data when user hits button
  test <- eventReactive(input$go, {
    return(indat$data[,3])
  })


  output$plot1 <- renderPlot({
    #plot updated data
    plot(test(),type = "l")
  })

}


shinyApp(ui, server)

So in the part where you do the renderTable, the variable name was slightly wrong :p, and in the read.csv, there were too many arguments.

 output$table <- renderTable({

    inFile <- input$file1_new

    if (is.null(inFile))
      {return(mtcars)}
    else{
    read.csv(inFile$datapath)}
  })

Please let me know if this works for you... Cheers!

  • Thank you so much ! This is exactly what I needed...I have been trying to pull from examples that I see on here, but I guess I will need to look more into some of the arguments like reading in a csv to better understand what's actually going on. – CJJ Jul 23 '18 at 13:11