在下面的R Shiny代码中,我使用conditionalPanel()
来显示/隐藏"子X/Y表"中的可选用户输入,以便将额外的变量包含到数学运算中.我只希望conditionalPanel()
将额外的用户输入隐藏(和显示)到在sidebarPanel()
中呈现的子X/Y表中,同时允许计算运行,就好像这些用户输入没有被隐藏一样,从而在mainPanel()
中呈现调用mainTbl
的输出表,无论是否判断checkboxInput()
.如何做到这一点?
These images explain. This first image is what appears when the App is first invoked:
And this image shows what renders when the checkboxInput()
is checked. I would like the below output table from the mainPanel()
to render even when the checkboxInput()
is not checked, when first invoking the App:
代码 :
library(shiny)
library(rhandsontable)
extraFun <- function(input_df, time_win, col_name) {
df <- setNames(data.frame(rep(NA, time_win)), col_name)
df[,col_name] <-
ifelse(seq_along(df[,1])%in%input_df[,1],input_df[match(seq_along(df[,1]),input_df[,1]),2],0)
return(df)
}
ui <- fluidPage(
sidebarPanel(
h5(strong("Variable (Y) over window (W):")),
rHandsontableOutput("parentTbl"),
checkboxInput("showCurves", HTML("<b>Add curves</b>"), FALSE),
conditionalPanel(
condition = "input.showCurves == true",
uiOutput("childTbl")
)
),
mainPanel(tableOutput("mainTbl"))
)
server <- function(input, output, session) {
parentVars <- lapply(1:2, function(i) { reactiveValues(data = 2) })
grpInputs <- reactiveValues(tables = list())
plus <- reactive(extraFun(grpInputs$tbl[["A"]],10,"A"))
minus <- reactive(extraFun(grpInputs$tbl[["B"]],10,"B"))
output$parentTbl <- renderRHandsontable({
rhandsontable(
data.frame(Inputs = sapply(parentVars, function(x) x$data)),
rowHeaders = c("A","B")
)
})
observeEvent(input$parentTbl, {
newValues <- hot_to_r(input$parentTbl)$Inputs
for (i in 1:2) {parentVars[[i]]$data <- newValues[i]}
})
# Below builds child X/Y tables #
lapply(1:2, function(i) {
varInputId <- c("A","B")[i]
output[[varInputId]] <- renderRHandsontable({
df <- data.frame(X = 1, Y = parentVars[[i]]$data)
rhandsontable(df, contextMenu = TRUE, rowHeaders = FALSE)
})
})
output$childTbl <- renderUI({
lapply(1:2, function(i) {
varInputId <- c("A","B")[i]
list(
h5(strong(paste("Adjust ", varInputId, " (Y) at time X:"))),
rHandsontableOutput(varInputId)
)
})
})
observe({
lapply(1:2, function(i) {
varInputId <- c("A","B")[i]
if (!is.null(input[[varInputId]])) {
grpInputs$tbl[[i]] <- hot_to_r(input[[varInputId]])
names(grpInputs$tbl)[i] <- varInputId
}
})
})
amCrs <- reactive({
balances <- cumprod(c(1, 1+plus()[1:10,1]-minus()[1:10,1]))
b <- head(balances, -1)
result <- data.frame(
Begin=b,Add=b*plus()[1:10,1],Subtract=b*minus()[1:10,1],End=balances[-1]
)
})
output$mainTbl <- renderTable({
req(length(grpInputs$tbl) > 0)
amCrs()
})
}
shinyApp(ui, server)