0
私は、次のアクションボタンでRを使用して表示したい画像のセットを持っています。次のボタンを使用してそれらを次々とどのように表示しますか?コードを実行するたびに、最初のイメージを表示することができますが、次のボタンをクリックすると、次のエラーが表示されます。警告:$のエラー: 'closure'タイプのオブジェクトはサブセット化できません。ここに私がこれまで持っていたコードがあります。R Shinyのファイルを次のタブを使って表示する
library(shiny)
library(shinydashboard)
dir_path <- '*/*'
L <- list.files(dir_path, pattern = ".png")
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(
title = "Controls", solidHeader = TRUE, status="primary",collapsible = TRUE,
actionButton("next_image","next")
),
box(
imageOutput("myimage",width=300,height=300)
)
)
)
)
server <- function(input, output) {
values<-reactiveValues(data = NULL)
values$count <- 1
ntext <- eventReactive(input$next_image,{
# Check if the counter `values$count` are not equal to the length of your questions
# if not then increment by 1 and return that image
# Note that initially the button hasn't been pressed yet so the `ntext()` will not be executed
if(values$count != length(L)){
values$count <- values$count + 1
return(L[values$count])
}
else{
# otherwise just return the last image
return(L[1])
}
})
ntext <- eventReactive(input$next_image,{
# Check if the counter `values$count` are not equal to the length of your questions
# if not then increment by 1 and return that image
# Note that initially the button hasn't been pressed yet so the `ntext()` will not be executed
if(values$count != length(L)){
values$count <- values$count + 1
print(values$count)
return(renderImage({
path<-file.path(paste('*/*',L[values$count], sep = ""))
return(list(src = path,
contentType = "image/png"))
},
deleteFile = FALSE
))
}
else{
# otherwise just return the last image
return(L[length(L)])
}
})
output$myimage<- renderImage({
if (input$next_image == 0){
path<-file.path(paste(dir_path,L[1], sep = ""))
return(list(src = path,
contentType = "image/png"))}
ntext()
},
deleteFile = FALSE
)
}
shinyApp(ui, server)