我有下面有 3 個 actionButtons 的閃亮應用程式。我希望每次用戶按下它們中的任何一個時,都應該顯示一對不同的影像。影像已經保存在www我的作業目錄中命名的檔案夾中。它們的名稱如下:
"1betA_green.jpg"
"1betA_green.jpg"
用戶第一次按下任意鍵actionButton()。(默認情況)。
"2betA_green.jpg"
"2betA_green.jpg"
用戶第二次按 any actionButton()。
等等..
這最多可能發生 6 次,然后停止。
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(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){
}
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
每個動作按鈕都與此處的一對影像相關聯。顯然,您需要用www檔案夾中的名稱替換影像的名稱。嘗試這個
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<7){
rv$img11 <- myimage1[nclick]
rv$img12 <- myimage2[nclick]
}else{
rv$img11 <- NULL
rv$img12 <- NULL
}
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/325668.html
