In the below toy example I have two data sets df1 and df2. The data sets are output to an interactive table using shiny & rhandsontable. My issue is that the output$out only recognizes the shift in data sets if the user hasn't edited the data set in the shiny application as indicated by is.null(input$out).
How do I fix the below code to load either df1 or df2 when input$df changes when input$out isn't null?
require(rhandsontable)
require(shiny)
df1 <- data.frame(col1 = rnorm(20),
col2 = rep(T, 20))
df2 <- data.frame(col1 = rnorm(20),
col2 = rep(F, 20))
funcX <- function(x) {
x$out <- ifelse(x$col2 == T, x$col1 * 1.5, x$col1)
x$out
}
df2$out <- funcX(df2)
df1$out <- funcX(df1)
server <- function(input, output) {
df <- reactive({
if (input$df == "df1") {
df <- df1
} else {
df <- df2
}
df
})
output$out <- renderRHandsontable({
if (is.null(input$out)) {
hot <- rhandsontable(df())
} else {
str(input$out)
hot <- hot_to_r(input$out)
hot$out <- funcX(hot)
hot <- rhandsontable(hot)
}
hot
})
}
ui <- fluidPage(sidebarLayout(sidebarPanel(
selectInput(
'df', 'Select data.frame:',
choices = c('df1', 'df2'),
selected = 'df1'
)
),
mainPanel(rHandsontableOutput("out"))))
shinyApp(ui = ui, server = server)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With