闪亮-编辑具有多个输入和输出元素的Rhsonsontable表 [英] Shiny - Editing rhandsontable tables with multiple input and output elements
问题描述
我一直以这篇文章为起点. 通过编辑表和/或eventReactive来更新handontable
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)
在reactiveValues
行上出现错误,因为我正在尝试使用input$x
.上一篇文章的硬编码值为2.
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.
推荐答案
我认为您快到了.但是,您不能使用输入来创建反应值.但这始终没有得到限制,您可以使用NULL
来启动它.
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)
这篇关于闪亮-编辑具有多个输入和输出元素的Rhsonsontable表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!