Last active
June 16, 2018 00:07
-
-
Save tellyshia/d6e9a7f6696668bf8dc23400664f47df to your computer and use it in GitHub Desktop.
How to update data table with newly calculated calculated values?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(DT) | |
library(dplyr) | |
library(purrr) | |
shinyApp( | |
ui = fluidPage( | |
tagList( | |
fluidRow( | |
column(4, h2("input sample size"), | |
tags$p("How can I update the mean and sd after changing sample size?"), | |
DT::DTOutput('x1')), | |
column(4, h2("Interactive plotting"), | |
tags$p("This needs to be updated after changing sample size"), | |
plotOutput("histo")), | |
column(4, h2("Updated sample size from input"), DT::DTOutput("updatesize")) | |
) | |
) | |
), | |
server = function(input, output) { | |
shinyInput = function(FUN, len, id, ...) { | |
inputs = character(len) | |
for (i in seq_len(len)) { | |
inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...)) | |
} | |
inputs | |
} | |
# obtain the values of inputs | |
shinyValue = function(id, len) { | |
unlist(lapply(seq_len(len), function(i) { | |
value = input[[paste0(id, i)]] | |
if (is.null(value)) NA else value | |
})) | |
} | |
dt <- reactiveValues(n = 50) | |
observe({ | |
set.seed(123) | |
# subset from each column in the data set. default values is 50 | |
dt$subset <- map2(iris[,1:4], dt$n, sample) | |
# calcualte the mean and sd from subset | |
dt$table <- dt$subset %>% | |
map_df(~invoke_map(list(sd=sd, mean=mean), ,.), .id="trait") %>% | |
mutate(samplesize = shinyInput( | |
numericInput, nrow(.), "samplesize_", | |
value = 50, width = "75px" | |
)) | |
}) | |
# rendertable | |
output$x1 <- DT::renderDT( | |
{dt$table}, | |
server = FALSE, | |
selection = "single", | |
escape = FALSE, | |
extensions = "Scroller", | |
options = list( | |
preDrawCallback = JS("function() {\nShiny.unbindAll(this.api().table().node()); }"), | |
drawCallback = JS("function() {\nShiny.bindAll(this.api().table().node()); } "), | |
stateSave = TRUE, | |
scrollX = TRUE, | |
scrollY = 350, | |
scroller = TRUE | |
)) | |
# plot selected row | |
output$histo <- renderPlot({ | |
req(input$x1_rows_selected) | |
current.trait <- dt$table$trait[input$x1_rows_selected] %>% | |
as.character() | |
hist(dt$subset[[current.trait]], main = current.trait) | |
}) | |
# output from changing sample size | |
editedsize <- reactive({ | |
set.seed(123) | |
samplesize <- shinyValue("samplesize_", nrow(dt$table)) | |
subset <- map2(iris[,1:4], samplesize, sample) | |
# calcualte the mean and sd from subset | |
subset %>% | |
map_df(~invoke_map(list(sd=sd, mean=mean), ,.), .id="trait") %>% | |
mutate(samplesize = samplesize) | |
}) | |
output$updatesize <- DT::renderDT( | |
editedsize() | |
) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment