使用下一个选项卡在 R Shiny 上显示文件
Display files on R Shiny using next tab
我有一组图像,我想通过下一步操作按钮使用 R shiny 查看这些图像。如何使用下一步按钮一个接一个地查看它们。每次我 运行 我的代码时,我都可以查看第一张图片,但是,当我单击下一步按钮时,我会收到以下错误:警告:$: 对象类型 [=15 中的错误=] 不可子集化。 这是我到目前为止的代码。
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)
首先,我建议创建三个示例图片以使代码完全可重现:(它们将保存在工作目录中)
# Create 3 sample pictures
for(nr in 1:3){
png(filename = paste0(nr, ".png"))
plot(nr)
dev.off()
}
你的服务器功能好像有点复杂。其实你只需要:
server <- function(input, output) {
values <- reactiveValues(count = 1)
observeEvent(input$next_image,{
values$count <- min(values$count + 1, length(L))
})
output$plot3 <- renderImage({
list(src = normalizePath(paste0(values$count, ".png")))
}, deleteFile = FALSE)
}
(请注意,我假设您的工作目录中没有保存其他 .png。)
有关可用的应用,请参见下文:
# Create 3 sample pictures
for(nr in 1:3){
png(filename = paste0(nr, ".png"))
plot(nr)
dev.off()
}
L <- list.files(getwd(), pattern = ".png")
library(shiny)
library(shinydashboard)
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("plot3",width=300,height=300)
)
)
)
)
server <- function(input, output) {
values <- reactiveValues(count = 1)
observeEvent(input$next_image,{
values$count <- min(values$count + 1, length(L))
})
output$plot3 <- renderImage({
list(src = normalizePath(paste0(values$count, ".png")))
}, deleteFile = FALSE)
}
shinyApp(ui, server)
我有一组图像,我想通过下一步操作按钮使用 R shiny 查看这些图像。如何使用下一步按钮一个接一个地查看它们。每次我 运行 我的代码时,我都可以查看第一张图片,但是,当我单击下一步按钮时,我会收到以下错误:警告:$: 对象类型 [=15 中的错误=] 不可子集化。 这是我到目前为止的代码。
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)
首先,我建议创建三个示例图片以使代码完全可重现:(它们将保存在工作目录中)
# Create 3 sample pictures
for(nr in 1:3){
png(filename = paste0(nr, ".png"))
plot(nr)
dev.off()
}
你的服务器功能好像有点复杂。其实你只需要:
server <- function(input, output) {
values <- reactiveValues(count = 1)
observeEvent(input$next_image,{
values$count <- min(values$count + 1, length(L))
})
output$plot3 <- renderImage({
list(src = normalizePath(paste0(values$count, ".png")))
}, deleteFile = FALSE)
}
(请注意,我假设您的工作目录中没有保存其他 .png。)
有关可用的应用,请参见下文:
# Create 3 sample pictures
for(nr in 1:3){
png(filename = paste0(nr, ".png"))
plot(nr)
dev.off()
}
L <- list.files(getwd(), pattern = ".png")
library(shiny)
library(shinydashboard)
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("plot3",width=300,height=300)
)
)
)
)
server <- function(input, output) {
values <- reactiveValues(count = 1)
observeEvent(input$next_image,{
values$count <- min(values$count + 1, length(L))
})
output$plot3 <- renderImage({
list(src = normalizePath(paste0(values$count, ".png")))
}, deleteFile = FALSE)
}
shinyApp(ui, server)