カウンタ値に応じて異なる出力を表示したい。たとえば、カウンタが3の場合は出力Aが表示されます。カウンタが3未満の場合は表示されず、カウンタの値が3より大きい場合は出力AとCが表示されます。 表示される出力は、 observe()関数に由来します。ここでShiny:カウンタ条件に基づいて結果を表示する方法
が最小の例である
library(shiny)
ui <- fluidPage(
sidebarPanel(numericInput("c1","Example", NA),
actionButton("update", "Update"),
br(), br(),
actionButton("reset", "Clear"),
br(), br(),
uiOutput("displayCounter"),
br(),br(),
textOutput("displaysum"),
br(),br(),
textOutput("total_sum")
),
mainPanel(tableOutput("example")
)
)
server <- function(input, output, session) {
values <- reactiveVal(data.frame(A=1, B=2, C=3))
# update values table on button click
observeEvent(input$update,{
old_values <- values()
A_new <- input$c1
B_new <- A_new + 2
C_new <- A_new + B_new
new_values <- data.frame(A=A_new, B=B_new, C=C_new)
# attach the new line to the old data frame here:
new_df <- rbind(old_values, new_values)
#store the result in values variable
values(new_df)
#reset the numeric input to NA
updateNumericInput(session, "c1", "Example", NA)
})
#Delete last row
deleteEntry <- observeEvent(input$reset,{
values(values()[-nrow(values()),])
})
##reactive counter
updateCounter <- reactive({
if (is.na(input$update)) {k <- 0}
else {k <- (input$update - input$reset)}
return(k)
})
##After 3 interactions stop counter
Counterlimit <- reactive({
if(updateCounter() > 3){ return(3)
}else{ updateCounter() }
})
output$displayCounter <- renderText({ c("Iteractions:",
Counterlimit()) })
# reactive
Total <- reactive({ colSums(values()[3]) })
#After 3 interactions display sum of column 3
observe({
c3_sum <-Total()
if (updateCounter()==3) {
output$displaysum <- renderText({paste("Partial sum", c3_sum)})
}
if (updateCounter()<3){
output$displaysum <- renderText({paste("Partial sum", NULL)})
}
if (updateCounter()>3) {
output$total_sum <- renderText({paste("Total sum", Total())})
}
})
# Print table
output$example <- renderTable({ return(values()) })
}
shinyApp(ui = ui, server = server)
私は3未満のカウンタを取得するにはクリアボタンを使用する場合は、総合計は、まだ表示されます。私の問題は条件で結果を表示することなので、私が望むものを表示するために私のUIに条件付きパネルを使用できるかどうかは疑問でした。
observe()関数の出力を条件付きパネルの入力として使用して結果を表示することはできますか?またはカウンター条件に基づいてそれらの出力を表示する他の提案あなたはとても
ui <- fluidPage(
selectInput("choose", "choose", letters[1:2]),
conditionalPanel(
condition = "output.myCondition == true",
actionButton("button", "this button is only visible when a is selected"))
)
server <- function(input, output){
output$myCondition <- reactive({input$choose == 'a'})
outputOptions(output, "myCondition", suspendWhenHidden = FALSE)
}
shinyApp(ui, server)
suspendWhenHidden
一部のように条件にouput
スロットを割り当てる必要があります
- あなたの "出力オブザーバー" にrenderText({NULL})は' –