如何在 Shiny 中选择输入后自动重新渲染 rhandsontable
How to make rhandsontable re-render automatically after selectInput in Shiny
我正在开发一个闪亮的应用程序,用户可以在其中将一些数据输入到不同的数据集中,然后用于计算数据集中的其他变量。数据集的选择由 selecInput 决定。
我选择了 rHandsontable,因为它成功地模仿了熟悉的电子表格输入。我调整了计算 中的列的解决方案,并且效果很好。除了一件小事:在选择不同的数据集之后,rhandsontable 在用户与其交互之前不会重新呈现。我知道这是应该发生的事情,但就我而言,我们需要重新渲染。
我尝试了 observe、observreEvent 和 eventReactive 的不同组合,但没有任何效果。我最接近的是包含选择更改的 reactiveVal 指示器,这至少表明应该使用新数据。
这是我得到的 MRE。
library(shiny)
library(rhandsontable)
data <- list (
table1 = data.frame( beginning = as.numeric(rep(8, 4)),
ending = as.numeric(rep(15, 4))),
table2 = data.frame( beginning = as.numeric(rep(9, 4)),
ending = as.numeric(rep(17, 4)))
)
data[["table1"]]$hours <- data[["table1"]]$ending - data[["table1"]]$beginning
data[["table2"]]$hours <- data[["table2"]]$ending - data[["table2"]]$beginning
############################# UI #############################
ui = shinyUI(fluidPage(
selectInput("tab", "Chose table: ", choices = list("table1", "table2")),
fluidRow(wellPanel(
column(6,
rHandsontableOutput("hot"),
actionButton(inputId="enter",label="Save")
),
column(6,
textOutput("title"),
tableOutput("tabela")
)))
))
########################## SEREVER ###########################
server=function(input,output, session){
tab_change <- reactiveVal(FALSE)
# rw <- reactivePoll(1000, session, file_name, read.csv2)
react_week <- reactive({
df <- data[[input$tab]]
})
output$title <- renderText(input$tab)
output$tabela <- renderTable(react_week())
observeEvent(input$tab,
{tab_change(TRUE)
})
# Calculation of columns
for_week <- reactive({
datacopy <- NULL
#For initial data upltabd
if(isolate(tab_change()) || is.null(input$hot)) {
datacopy <- react_week()
}
else {
datacopy <- hot_to_r(input$hot)
}
#If there is change in data
if(!is.null(input$hot$changes$changes)){
col.no <- as.numeric(unlist(input$hot$changes$changes)[2])
new.val <- unlist(input$hot$changes$changes)[4]
#If the changed value is prihod or odhod
if(col.no == 0 || col.no == 1){
datacopy[, 3] <- as.numeric(datacopy[, 2]) - as.numeric(datacopy[, 1])
}
}
tab_change(FALSE)
datacopy
})
output$hot <- renderRHandsontable(
rhandsontable(for_week())
)
observeEvent(input$enter, {
data[[input$tab]] <<- hot_to_r(input$hot)
output$tabela <- renderTable( data[[input$tab]])
})
}
shinyApp(ui = ui, server = server)
我是 Shiny 的新手,所以我可能遗漏了一些明显的东西,但是有没有办法在选择更改后立即重新渲染 rhandsontable,而不是使用一些我害怕的低级函数,比如 sendInputMessage?
如果没有,我在哪里可以找到有关如何构建适合我的消息的说明?
使用 eventReactive
并通过 input$tab
和 input$hot
而不是 reactive
触发你的 for_week
反应对象:
library(shiny)
library(rhandsontable)
data <- list (
table1 = data.frame( beginning = as.numeric(rep(8, 4)),
ending = as.numeric(rep(15, 4))),
table2 = data.frame( beginning = as.numeric(rep(9, 4)),
ending = as.numeric(rep(17, 4)))
)
data[["table1"]]$hours <- data[["table1"]]$ending - data[["table1"]]$beginning
data[["table2"]]$hours <- data[["table2"]]$ending - data[["table2"]]$beginning
############################# UI #############################
ui = shinyUI(fluidPage(
selectInput("tab", "Chose table: ", choices = list("table1", "table2")),
fluidRow(wellPanel(
column(6,
rHandsontableOutput("hot"),
actionButton(inputId="enter",label="Save")
),
column(6,
textOutput("title"),
tableOutput("tabela")
)))
))
########################## SEREVER ###########################
server=function(input,output, session){
tab_change <- reactiveVal(FALSE)
# rw <- reactivePoll(1000, session, file_name, read.csv2)
react_week <- reactive({
df <- data[[input$tab]]
})
output$title <- renderText(input$tab)
output$tabela <- renderTable(react_week())
observeEvent(input$tab,
{tab_change(TRUE)
})
# Calculation of columns
# ----------------------------Modified here----------------------------
# for_week <- reactive({
for_week <- eventReactive(c(input$tab, input$hot), {
# ---------------------------------------------------------------------
datacopy <- NULL
#For initial data upltabd
if(isolate(tab_change()) || is.null(input$hot)) {
datacopy <- react_week()
}
else {
datacopy <- hot_to_r(input$hot)
}
#If there is change in data
if(!is.null(input$hot$changes$changes)){
col.no <- as.numeric(unlist(input$hot$changes$changes)[2])
new.val <- unlist(input$hot$changes$changes)[4]
#If the changed value is prihod or odhod
if(col.no == 0 || col.no == 1){
datacopy[, 3] <- as.numeric(datacopy[, 2]) - as.numeric(datacopy[, 1])
}
}
tab_change(FALSE)
datacopy
})
output$hot <- renderRHandsontable(
rhandsontable(for_week())
)
observeEvent(input$enter, {
data[[input$tab]] <<- hot_to_r(input$hot)
output$tabela <- renderTable( data[[input$tab]])
})
}
shinyApp(ui = ui, server = server)
我正在开发一个闪亮的应用程序,用户可以在其中将一些数据输入到不同的数据集中,然后用于计算数据集中的其他变量。数据集的选择由 selecInput 决定。
我选择了 rHandsontable,因为它成功地模仿了熟悉的电子表格输入。我调整了计算
library(shiny)
library(rhandsontable)
data <- list (
table1 = data.frame( beginning = as.numeric(rep(8, 4)),
ending = as.numeric(rep(15, 4))),
table2 = data.frame( beginning = as.numeric(rep(9, 4)),
ending = as.numeric(rep(17, 4)))
)
data[["table1"]]$hours <- data[["table1"]]$ending - data[["table1"]]$beginning
data[["table2"]]$hours <- data[["table2"]]$ending - data[["table2"]]$beginning
############################# UI #############################
ui = shinyUI(fluidPage(
selectInput("tab", "Chose table: ", choices = list("table1", "table2")),
fluidRow(wellPanel(
column(6,
rHandsontableOutput("hot"),
actionButton(inputId="enter",label="Save")
),
column(6,
textOutput("title"),
tableOutput("tabela")
)))
))
########################## SEREVER ###########################
server=function(input,output, session){
tab_change <- reactiveVal(FALSE)
# rw <- reactivePoll(1000, session, file_name, read.csv2)
react_week <- reactive({
df <- data[[input$tab]]
})
output$title <- renderText(input$tab)
output$tabela <- renderTable(react_week())
observeEvent(input$tab,
{tab_change(TRUE)
})
# Calculation of columns
for_week <- reactive({
datacopy <- NULL
#For initial data upltabd
if(isolate(tab_change()) || is.null(input$hot)) {
datacopy <- react_week()
}
else {
datacopy <- hot_to_r(input$hot)
}
#If there is change in data
if(!is.null(input$hot$changes$changes)){
col.no <- as.numeric(unlist(input$hot$changes$changes)[2])
new.val <- unlist(input$hot$changes$changes)[4]
#If the changed value is prihod or odhod
if(col.no == 0 || col.no == 1){
datacopy[, 3] <- as.numeric(datacopy[, 2]) - as.numeric(datacopy[, 1])
}
}
tab_change(FALSE)
datacopy
})
output$hot <- renderRHandsontable(
rhandsontable(for_week())
)
observeEvent(input$enter, {
data[[input$tab]] <<- hot_to_r(input$hot)
output$tabela <- renderTable( data[[input$tab]])
})
}
shinyApp(ui = ui, server = server)
我是 Shiny 的新手,所以我可能遗漏了一些明显的东西,但是有没有办法在选择更改后立即重新渲染 rhandsontable,而不是使用一些我害怕的低级函数,比如 sendInputMessage? 如果没有,我在哪里可以找到有关如何构建适合我的消息的说明?
使用 eventReactive
并通过 input$tab
和 input$hot
而不是 reactive
触发你的 for_week
反应对象:
library(shiny)
library(rhandsontable)
data <- list (
table1 = data.frame( beginning = as.numeric(rep(8, 4)),
ending = as.numeric(rep(15, 4))),
table2 = data.frame( beginning = as.numeric(rep(9, 4)),
ending = as.numeric(rep(17, 4)))
)
data[["table1"]]$hours <- data[["table1"]]$ending - data[["table1"]]$beginning
data[["table2"]]$hours <- data[["table2"]]$ending - data[["table2"]]$beginning
############################# UI #############################
ui = shinyUI(fluidPage(
selectInput("tab", "Chose table: ", choices = list("table1", "table2")),
fluidRow(wellPanel(
column(6,
rHandsontableOutput("hot"),
actionButton(inputId="enter",label="Save")
),
column(6,
textOutput("title"),
tableOutput("tabela")
)))
))
########################## SEREVER ###########################
server=function(input,output, session){
tab_change <- reactiveVal(FALSE)
# rw <- reactivePoll(1000, session, file_name, read.csv2)
react_week <- reactive({
df <- data[[input$tab]]
})
output$title <- renderText(input$tab)
output$tabela <- renderTable(react_week())
observeEvent(input$tab,
{tab_change(TRUE)
})
# Calculation of columns
# ----------------------------Modified here----------------------------
# for_week <- reactive({
for_week <- eventReactive(c(input$tab, input$hot), {
# ---------------------------------------------------------------------
datacopy <- NULL
#For initial data upltabd
if(isolate(tab_change()) || is.null(input$hot)) {
datacopy <- react_week()
}
else {
datacopy <- hot_to_r(input$hot)
}
#If there is change in data
if(!is.null(input$hot$changes$changes)){
col.no <- as.numeric(unlist(input$hot$changes$changes)[2])
new.val <- unlist(input$hot$changes$changes)[4]
#If the changed value is prihod or odhod
if(col.no == 0 || col.no == 1){
datacopy[, 3] <- as.numeric(datacopy[, 2]) - as.numeric(datacopy[, 1])
}
}
tab_change(FALSE)
datacopy
})
output$hot <- renderRHandsontable(
rhandsontable(for_week())
)
observeEvent(input$enter, {
data[[input$tab]] <<- hot_to_r(input$hot)
output$tabela <- renderTable( data[[input$tab]])
})
}
shinyApp(ui = ui, server = server)