Loading image file from Dropbox into R Shiny app

二次信任 提交于 2019-12-13 20:00:52

问题


I am trying to make a very simple shiny app that allows the user to choose an image file from a dropdown menu and then the chosen one will be loaded from Dropbox and displayed in the shiny app. I am using the drop_get function from the rdrop2 package as described here: https://github.com/karthik/rdrop2. Unfortunately it seems that the file is not loaded, however it takes a few seconds before the alternative text is shown so something is going on in the background (no error message or warning in the console). My token and dropbox setup should work, because if I try it outside of the shiny app, it loads the file perfectly.

Apologies that the code is not 100% reproducible as the token includes my personal dropbox authentication details...

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("My app"),
  sidebarLayout(
    sidebarPanel(
      selectInput("figure",
                  "Choose figure",
                   choices = list("file1","file2"),
                   selected = "file1")
    ),
    mainPanel(
        imageOutput("image")
    )
  )
))

server.R

library(shiny)
library(rdrop2)
token <- readRDS("droptoken.rds")
drop_acc(dtoken = token)
shinyServer(function(input, output) {
    reactive({
        drop_get(paste(input$figure, '.jpg', sep = ''))
        })
    output$image <- renderImage({
        filename <- paste(input$figure, '.jpg', sep='')
        list(src = filename,
             alt = paste("Image name:", input$figure))
    }, deleteFile = FALSE)
})

回答1:


You need to call your reactive function within renderImage. Something similar to the following code worked in one of my shiny apps.

library(shiny)
library(rdrop2)
token <- readRDS("droptoken.rds")
drop_acc(dtoken = token)
shinyServer(function(input, output) {
    myImage <- reactive({
        drop_get(paste(input$figure, '.jpg', sep = ''))
        })
    output$image <- renderImage({
        myImage()
        filename <- paste(input$figure, '.jpg', sep='')
        list(src = filename,
             alt = paste("Image name:", input$figure))
    }, deleteFile = FALSE)
})

EDIT: Alternatively, you could put drop_get code in renderImage.

library(shiny)
library(rdrop2)
token <- readRDS("droptoken.rds")
drop_acc(dtoken = token)
shinyServer(function(input, output) {
    output$image <- renderImage({
        drop_get(paste(input$figure, '.jpg', sep = ''))
        filename <- paste(input$figure, '.jpg', sep='')
        list(src = filename,
             alt = paste("Image name:", input$figure))
    }, deleteFile = FALSE)
})


来源:https://stackoverflow.com/questions/38197751/loading-image-file-from-dropbox-into-r-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!