R 在闪亮的UI中动态显示上传的图像
这与这里提出的问题基本相同: 但是,我没有生成数量可变的绘图(我已经成功地做到了),而是尝试将选定的图像上传到应用程序中,并在用户界面上显示它们。使用上述问题中描述的相同方法,我用下面的代码生成了我的应用程序。但只有第一个图像在UI中呈现 我今天错过了什么明显的事情 R 3.2.2(Windows 7) 闪亮的0.12.2 server.RR 在闪亮的UI中动态显示上传的图像,r,shiny,R,Shiny,这与这里提出的问题基本相同: 但是,我没有生成数量可变的绘图(我已经成功地做到了),而是尝试将选定的图像上传到应用程序中,并在用户界面上显示它们。使用上述问题中描述的相同方法,我用下面的代码生成了我的应用程序。但只有第一个图像在UI中呈现 我今天错过了什么明显的事情 R 3.2.2(Windows 7) 闪亮的0.12.2 server.R library(shiny) shinyServer(function(input, output) { output$files <- re
library(shiny)
shinyServer(function(input, output) {
output$files <- renderTable(input$files)
files <- reactive({
files <- input$files
files$datapath <- gsub("\\\\", "/", files$datapath)
files
})
output$images <- renderUI({
image_output_list <-
lapply(seq_along(nrow(files())),
function(i)
{
imagename = paste0("image", i)
imageOutput(imagename)
})
do.call(tagList, image_output_list)
})
observe({
for (i in seq_along(nrow(files())))
{
local({
my_i <- i
imagename = paste0("image", my_i)
output[[imagename]] <-
renderImage({
list(src = files()$datapath[my_i],
alt = "Image failed to render")
}, deleteFile = FALSE)
})
}
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = 'files',
label = 'Select an Image',
multiple = TRUE,
accept=c('image/png', 'image/jpeg'))
),
mainPanel(
tableOutput('files'),
uiOutput('images')
)
)
))
你的距离太近了!试试这个:
library(shiny)
server <- shinyServer(function(input, output) {
output$files <- renderTable(input$files)
files <- reactive({
files <- input$files
files$datapath <- gsub("\\\\", "/", files$datapath)
files
})
output$images <- renderUI({
if(is.null(input$files)) return(NULL)
image_output_list <-
lapply(1:nrow(files()),
function(i)
{
imagename = paste0("image", i)
imageOutput(imagename)
})
do.call(tagList, image_output_list)
})
observe({
if(is.null(input$files)) return(NULL)
for (i in 1:nrow(files()))
{
print(i)
local({
my_i <- i
imagename = paste0("image", my_i)
print(imagename)
output[[imagename]] <-
renderImage({
list(src = files()$datapath[my_i],
alt = "Image failed to render")
}, deleteFile = FALSE)
})
}
})
})
ui <- shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = 'files',
label = 'Select an Image',
multiple = TRUE,
accept=c('image/png', 'image/jpeg'))
),
mainPanel(
tableOutput('files'),
uiOutput('images')
)
)
))
shinyApp(ui=ui,server=server)
库(闪亮)
服务器太棒了。我现在看到了。非常感谢你抽出时间。