r - 隐藏闪亮页面中的所有内容并在按下任何 actionButton() 后将其替换为文本
问题描述
我有下面带有 3 个动作按钮的闪亮应用程序。每次用户按下它们中的任何一个时,都应显示一对不同的图像。图像已经保存在www
我的工作目录中命名的文件夹中。它们被命名为:
"1betA_green.jpg"
"1betA_green.jpg"
用户打开应用时的默认情况。
"2betA_green.jpg"
"2betA_green.jpg"
用户第一次按 any actionButton()
。
ETC..
这可能发生多达 6 次,然后停止。第 6 次按下后,图像消失了,但我还想隐藏所有内容(和 actionButtons),并有一个完全空白的页面,用文本代替图像。我可能可以使用标签来做到这一点,但我根本不希望标签可见,所以我想这不是最好的解决方案。
library(shiny)
library(shinyjs)
ui <- fluidPage(
id="main",
title="Risk and ambiguity",
useShinyjs(),
#when you press on of the 3 actionbuttons for first-example
fluidRow(wellPanel(
splitLayout(cellWidths = c("50%", "50%"), column(6,uiOutput("image1")), column(6,uiOutput("image2"))
# column(12,img(src="1betA_green.jpg", height="80%", width="80%", align="left")),
# column(12,img(src="1betB_green.jpg", height="80%", width="80%", align="right"))
))),
#when you press on of the 3 actionbuttons for second time-example
#fluidRow(wellPanel(
# splitLayout(cellWidths = c("50%", "50%"),
# column(12,img(src="2betA_green.jpg", height="80%", width="80%", align="left")),
# column(12,img(src="2betB_green.jpg", height="80%", width="80%", align="right"))))),
####
fluidRow(wellPanel(
splitLayout(cellWidths = c("33%", "33%", "33%"),
#uiOutput("myactions")
column(12,align="center",actionButton("action11", label = "Je choisis option A")),
column(12,align="center",actionButton("action12", label = "Je choisis le sac avec A et B")),
column(12,align="center",actionButton("action13", label = "Je choisis option B"))
)))
)
server <- function(input, output, session){
rv <- reactiveValues(img11=NULL, img12=NULL)
myimage1 <- c("YBS.png", "mouse.png","EC.jpg", "man_log.png", "cube.png", "hotdog.png")
myimage2 <- c("man_log.png", "cube.png", "hotdog.png", "YBS.png", "mouse.png","EC.jpg")
output$image1 <- renderUI({tags$img(src=rv$img11, height = "50px")})
output$image2 <- renderUI({tags$img(src=rv$img12, height = "50px")})
observe({
nclick <- sum(as.numeric(input$action11) + as.numeric(input$action12) + as.numeric(input$action13))
if (nclick==0) { ### initial display
rv$img11=myimage1[1]
rv$img12=myimage2[1]
}else if (nclick>0 & nclick<6){
rv$img11 <- myimage1[nclick+1]
rv$img12 <- myimage2[nclick+1]
}else{
rv$img11 <- NULL
rv$img12 <- NULL
}
})
}
shinyApp(ui = ui, server = server)
解决方案
这是你想要的?
library(shiny)
library(shinyjs)
ui <- fluidPage(
id="main",
title="Risk and ambiguity",
useShinyjs(),
#when you press on of the 3 actionbuttons for first-example
fluidRow(wellPanel(
splitLayout(cellWidths = c("50%", "50%"), column(6,uiOutput("image1")), column(6,uiOutput("image2"))
# column(12,img(src="1betA_green.jpg", height="80%", width="80%", align="left")),
# column(12,img(src="1betB_green.jpg", height="80%", width="80%", align="right"))
))),
#when you press on of the 3 actionbuttons for second time-example
#fluidRow(wellPanel(
# splitLayout(cellWidths = c("50%", "50%"),
# column(12,img(src="2betA_green.jpg", height="80%", width="80%", align="left")),
# column(12,img(src="2betB_green.jpg", height="80%", width="80%", align="right"))))),
####
fluidRow(wellPanel(
splitLayout(cellWidths = c("33%", "33%", "33%"),
#uiOutput("myactions")
column(12,align="center",actionButton("action11", label = "Je choisis option A")),
column(12,align="center",actionButton("action12", label = "Je choisis le sac avec A et B")),
column(12,align="center",actionButton("action13", label = "Je choisis option B")),
),column(12,align="center",hidden(verbatimTextOutput("mytext")))
))
)
server <- function(input, output, session){
rv <- reactiveValues(img11=NULL, img12=NULL)
myimage1 <- c("YBS.png", "mouse.png","EC.jpg", "man_log.png", "cube.png", "hotdog.png")
myimage2 <- c("man_log.png", "cube.png", "hotdog.png", "YBS.png", "mouse.png","EC.jpg")
output$image1 <- renderUI({tags$img(src=rv$img11, height = "50px")})
output$image2 <- renderUI({tags$img(src=rv$img12, height = "50px")})
output$mytext <- renderText({paste("This is hidden for 6 clicks")})
observe({
nclick <- sum(as.numeric(input$action11) + as.numeric(input$action12) + as.numeric(input$action13))
if (nclick==0) { ### initial display
rv$img11=myimage1[1]
rv$img12=myimage2[1]
}else if (nclick>0 & nclick<6){
rv$img11 <- myimage1[nclick+1]
rv$img12 <- myimage2[nclick+1]
}else{
rv$img11 <- NULL
rv$img12 <- NULL
shinyjs::hide("action11")
shinyjs::hide("action12")
shinyjs::hide("action13")
shinyjs::show("mytext")
}
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- javascript - 使用 jQuery 的 .load() 函数保持导入文本文件的格式?
- ios - Swift 中的 IBM Silverpop 推送集成
- python - Django UpdateView 仅在对象字段满足特定条件时才允许
- powershell - 连接 AzureRmAccount :访问_ws_metadata_exchange_failed
- python - 遍历目录以查找匹配的文件
- javascript - 执行 Firebase 事务时如何检查节点的值?
- java - 多线程读取文件并执行runnable
- selenium - 自动提示文本自动 travelocity.com 上的 xpath 失败
- excel - 来自 2 列或更多列的 VBA 唯一值
- python - Python unittest - 在 0.000 秒内额外运行 0 次测试