Shiny: Switching between reactive data sets with rhandsontable
You could use reactiveValues
to store the df1
and df2
data frames, and update these values when they are modified. Here's an example server.R
code:
server <- function(input, output) {
values = reactiveValues()
values[["df1"]] <- df1
values[["df2"]] <- df2
observe({
if (!is.null(input$out)) {
temp <- hot_to_r(input$out)
temp$out <- funcX(temp)
if (isolate(input$df) == "df1") {
values[["df1"]] <- temp
} else {
values[["df2"]] <- temp
}
}
})
df <- reactive({
if (input$df == "df1") {
df <- values[["df1"]]
} else {
df <- values[["df2"]]
}
df
})
output$out <- renderRHandsontable({
hot <- rhandsontable(df())
hot
})
}
When the table is changed, the correct df1
or df2
is updated in the reactive values. In the observer, the input$df
is isolated so that this part of the code only reacts to when the user changes the tables.
Switching between reactive data sets of different formats with rhandsontable
Here is a way:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("sel_1", label = "Select list:",
choices = c("list_1", "list_2")),
selectInput("sel_2", label = "Select element 1",
choices = names(list_1))
),
mainPanel(
rHandsontableOutput("hot")
)
)
)
server <- function(input, output, session) {
list_1$first <- data.frame(values = as.numeric(list_1$first))
list_2$third <- data.frame(values = as.numeric(list_2$third))
values <- reactiveValues(list_1 = list_1, list_2 = list_2)
observe({
x <- input$sel_1
label <- paste("Select element", substr(x, nchar(x) - 1, nchar(x)))
updateSelectInput(session, "sel_2", label,
choices = names(values[[x]]))
})
# Key part: storing back the data in `values` every time there is a change
observe({
if (!is.null(input$hot) && !is.null(input$hot$changes$changes))
values[[isolate(input$sel_1)]][[isolate(input$sel_2)]] <- hot_to_r(input$hot)
})
output$hot <- renderRHandsontable({
df <- values[[input$sel_1]][[input$sel_2]]
if (is.null(df)) return(NULL)
if (input$sel_2 %in% c("first", "third")) {
rhandsontable(df, stretchH = "all", rowHeaderWidth = 300, width = 600)
} else {
rhandsontable(df, stretchH = "all", rowHeaderWidth = 50, height = 300, width = 600) %>%
hot_col("bool", allowInvalid = FALSE)
}
})
}
Be careful with this answer though, because as I was writing it, I could still occasionally see weird unreproducible bugs like the wrong table being overwritten, I find reactivity very hard to control in the context of rhandsontable
.
Shiny - Switching Between Models
If that is the only issue, you change your selectInput()
as
selectInput(inputId = "Mod", label = "Model Type:", choices = c("Thin Plate Spline" = 'Model1',
"Tensor Product Smooth" = 'Model2'))
Then create a eventReactive model as
myModel <- eventReactive(input$Mod, {
switch(input$Mod,
"Model1" = Model1b(),
"Model2" = Model2b())
})
and lastly use this in predict as
Z <- matrix(predict(myModel(), newdat), steps, steps)
How to make rhandsontable re-render automatically after selectInput in Shiny
Use eventReactive
and trigger by input$tab
and input$hot
rather than reactive
for your for_week
reactive object:
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)
R shiny: different behaviours of observeEvent and eventReactive
It's because edits()
isn't called thereafter, so shiny
thinks you don't need it, hence no reason to do any work on it, you need to add where it should go or what you want to do with it:
library(shiny)
ui <- fluidPage(
rhandsontable::rHandsontableOutput(
outputId = "data")
)
server <- function(input, output, session) {
data <- data.frame(a = 1, b = 2, c = 3)
output$data <- rhandsontable::renderRHandsontable({
rhandsontable::rhandsontable(
selectCallback = TRUE,
data = data)
})
observeEvent(input$data$changes$changes, {
print("ping")
})
edits <- eventReactive(input$data$changes$changes, {
print("pong")
})
observe({
edits()
})
}
shinyApp(ui = ui, server = server)
Related Topics
Create All Possible Combiations of 0,1, or 2 "1"S of a Binary Vector of Length N
Adding Regression Line Equation and R2 on Separate Lines Graph
Import Multiple Text Files in R and Assign Them Names from a Predetermined List
How to Prevent Objects from Automatically Loading When I Open Rstudio
R Subsetting a Data Frame into Multiple Data Frames Based on Multiple Column Values
Difference Between Installing a Package from Source and from Compiled Binary
How to Replace Numeric Codes with Value Labels from a Lookup Table
Rmarkdown Error "Attempt to Use Zero-Length Variable Name"
How to Add a Non-Overlapping Legend to Associate Colors with Categories in Pairs()
R:Convert Nested List into a One Level List
Ellipse Containing Percentage of Given Points in R
How Do Add a Column in a Data Frame in R
Ggplot2:How to Reduce the Width and the Space Between Bars with Geom_Bar
Ggplot2 Bar Plot with Two Categorical Variables
Using Strsplit and Subset in Dplyr and Mutate
Produce an Inset in Each Facet of an R Ggplot While Preserving Colours of the Original Facet Content