Shiny - Editing rhandsontable tables with multiple input and output elements

烂漫一生 提交于 2019-12-05 06:16:03

问题


I've been working with this post as a starting point. Update handsontable by editing table and/or eventReactive

Very helpful, but I'm trying to extend it to specify the number of values in the table, then update a plot based on the table values after editing.

Here's what I have so far.

library(shiny)
library(rhandsontable)
library(colorSpec)

ui <- fluidPage(
  numericInput("x", "number of values", 2),
  rHandsontableOutput('table'),
  textOutput('result'),
  plotOutput('plot'),
  actionButton("recalc", "generate new random vals and calculate")
)


server <- function(input,output,session)({
  values <- reactiveValues(data=as.data.frame(runif(input$x)))

  observe({
    input$recalc
    values$data <- as.data.frame(runif(input$x))
  })

  observe({
    if(!is.null(input$table))
      values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    rhandsontable(values$data)
  })


  output$result <- renderText({
    sum(values$data)
  })

  output$plot <- reactivePlot({
    barplot(values$data)
  })

})

shinyApp(ui = ui, server = server)

I get an error on the reactiveValues line because I'm trying to use input$x. The previous post had a hard coded value of 2.


回答1:


I think you were almost there. You can, however, not use an input for creating a reactive value. But this is anyways not eneded and you can initiate it with a NULL.

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  numericInput("x", "number of values", 2),
  rHandsontableOutput('table'),
  textOutput('result'),
  plotOutput('plot'),
  actionButton("recalc", "generate new random vals and calculate")
)


server <- function(input,output,session)({
  values <- reactiveValues(data = NULL) ## assign it with NULL

  ## button press resets now the data frame
  observeEvent(input$recalc, { 
    values$data$x <- 0
  })

  ## changes in numericInput sets all (!) new values
  observe({
    req(input$x)
    values$data <- data.frame(x = runif(input$x))
  })

  observe({
    if(!is.null(input$table))
      values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    req(values$data)
    rhandsontable(values$data)
  })


  output$result <- renderText({
    req(values$data)
    sum(values$data)
  })

  output$plot <- renderPlot({
    req(values$data)
    barplot(values$data$x)
  })

})

shinyApp(ui = ui, server = server)



回答2:


You can use reactive() instead of reactiveValues to do this:

library(shiny)
library(rhandsontable)
library(colorSpec)

ui <- fluidPage(
  numericInput("x", "number of values", 2),
  rHandsontableOutput('table'),
  textOutput('result')
)


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

  values <- reactive({
    foo <- as.data.frame(runif(input$x))
    colnames(foo) <- c("Col1")
    return(foo)
  })

  output$table <- renderRHandsontable({
    rhandsontable(values())
  })


  output$result <- renderText({
    sum(values())
  })

})

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/52486037/shiny-editing-rhandsontable-tables-with-multiple-input-and-output-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!