Error trying to show an image in shiny

1,145 views
Skip to first unread message

jorg...@sasnw.com

unread,
Dec 15, 2016, 4:02:13 PM12/15/16
to Shiny - Web Framework for R
Hello. I can´t upload an image in my shiny app. Even though I´ve done what some have suggested in stackoverflow and in this group, my image doesn´t appear in my app. The image I want to show in my app is in the same directory where I have my App.R code.
Here is some code.

setwd("C:/Users/User/Documents/5.-Shiny")
library(shiny)
library(markdown)
library(corrplot)
ui<-fluidPage(
  titlePanel(title = "Some App"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Cargar archivo',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv'
                )
      ),
      
      checkboxInput('header', '¿Contiene Encabezado?', TRUE),
      radioButtons('sep', 'Delimitador',
                   c(Comma=',',
                     "Punto y coma"=';',
                     Tab='\t'),
                   ','),
      radioButtons('resume', 'Summary',
                   c('Individual',
                     'Múltiple'), selected = "Individual",
                   inline = TRUE),
      img(src = "C:/Users/User/Documents/5.-Shiny/Logo.png", align="right")
      ),
    mainPanel(
      img(src = "C:/Users/User/Documents/5.-Shiny/Logo.png", align="right"),
      tabsetPanel(
        tabPanel("Datos",
                 dataTableOutput('contents')),
        tabPanel("Exploratorio",
                 h3("Muestra del archivo cargado:"),
                 #dataTableOutput('contents'),
                 verbatimTextOutput("summary"),
                 plotOutput("histog")
                 #textOutput("vare")
        )#,
        #tabPanel("Panel3",
        #        dataTableOutput('contents'))
      )
    )
  )
)


server<-function(input, output, session) {
  tableData <- reactive({
    
    inFile <- input$file1
    
    if (!is.null(inFile)){
      read.csv(inFile$datapath, header=input$header, sep=input$sep, 
               quote=input$quote)
    }
    else {
      return(NULL)
    }
  })
  
  observe({
    updateSelectInput(
      session,
      "xcol",
      choices=names(tableData()))
  })
  var_name<-reactive(input$xcol)
  nuevo_df<-reactive(tableData()[sapply(tableData(),is.numeric)])
  
  output$contents <- renderDataTable({
    head(tableData(), n=100)
    #if (!is.null(input$file1))
    # head(tableData()) else return(NULL)
    
  })
  output$summary <- renderPrint({
    if (!is.null(input$file1)){if(input$resume == 'Individual')
      summary(tableData()[,c(input$xcol)]) else (summary(tableData()))}
  })
  #output$vare<-renderPrint(names(tableData()))
  #output$vare<-renderPrint(tableData()[,c(input$xcol)])
  output$histog<-renderPlot({
    
    if (!is.null(input$file1)){hist(tableData()[,c(input$xcol)],probability = TRUE, breaks= seq(0,max(tableData()[,c(input$xcol)]),l=input$bins+1), l=input$bins+1,
                                    col = input$color, main = paste("Histograma de", input$xcol), xlab=paste(input$xcol))}
    if (input$density) {
      dens <- density(tableData()[,c(input$xcol)],
                      adjust = input$bw_adjust)
      lines(dens, col = "white",type = "h")
    }
  })
}

shinyApp(ui = ui, server = server)

Joe Cheng

unread,
Dec 15, 2016, 6:18:47 PM12/15/16
to jorg...@sasnw.com, Shiny - Web Framework for R
Put the image in a *subdirectory* of the directory where the app code is. The subdirectory must be called "www". Then, use

img(src = "Logo.png")

The reason for "www" is because your app's root directory may contain private files like CSV data, .R files containing proprietary code, etc., so it can't be made downloadable. Instead, we require a separate www subdirectory so by putting a file in there you're specifically asking for it to be made publicly downloadable.


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e488a6c5-b1cd-498e-8687-57d517b59b51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jorge Sua

unread,
Dec 15, 2016, 7:40:30 PM12/15/16
to Joe Cheng, Shiny - Web Framework for R
Oh!! It works. Thank you for your answer and the explanation about the use of a *subdirectory*


Thanks.


Jorge Sua
Consultor Senior de Analítica
Calle 90 N° 13A-20. Oficina 702


To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages