IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

R Discussion :

Inserer un graph (poorly) dans Shiny


Sujet :

R

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2018
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Inserer un graph (poorly) dans Shiny
    Bonjour a tous,

    Je souhaite creer une application qui permette a l'utilisateur de upload un fichier excel et de faire des graphes a partir des variables provenant du fichier excel.
    Cependant, je n'arrive pas a savoir pourquoi mon graphique ne s'affiche pas. J'utilise ggplot puis ggplotly.

    J'espere que vous pourrez m'eclairer.

    Merci d'avance.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
     
    library(shiny)
    library(rsconnect)
    library(ggplot2)
    library(plotly)
     
    plotType <- function(data, x, y, type){
      switch(type,
             "Line" = ggplot(data, aes_string(x, y)) + geom_line(),
             "Scatterplot" = ggplot(data, aes_string(x, y)) + geom_point()
      )
    }
     
    ui <- fluidPage(
     
      # sidebar with user input options
      sidebarPanel(
     
        # Input: select a file
        fileInput(inputId = "file1", label = "Choose CSV File",
                  multiple = FALSE,
                  accept = c("text/csv",
                             "text/comma-separated-values, text/plain",
                             ".csv")
        ),
        # Horizontal line
        tags$hr(),
     
        # Input: Checkbox if file has header
        checkboxInput("header", "Header", TRUE),
     
        # Input: Select separator
        radioButtons(inputId ="sep", label = "Separator",
                     choices = c(Comma = ",",
                                 Semicolon = ";",
                                 Tab = "\t"),
                     selected = ","),
     
        radioButtons(inputId = "quote", label = "Quote",
                     choices = c(None = "",
                                 "Double Quote" = '"',
                                 "Single Quote" = "'"),
                     selected = '"'),
        # Horizontal line
        tags$hr(),
        # Empty inputs - they will be updated after the data is uploaded
        selectInput('xcol', 'X Variable', ""),
        selectInput('ycol', 'Y Variable', "", selected = ""),
         # Horizontal line
        tags$hr(),
        # Input: Select the type of graph 
        radioButtons(inputId ="graph", label = "Type of graph:",
                     choices = c("Line", 
                                 "Scatterplot"),
                     selected = "Line")
      ),
        mainPanel(
        tabsetPanel( type = "tabs",
                     tabPanel(
                       # App title
                       titlePanel("Uploading Files"),
                       # Output: Data file
                       tableOutput("contents")
     
                     ),
                     tabPanel(
                       titlePanel("Plot"),
                       plotOutput('MyPlot')
                     ),
                     tabPanel(
                       titlePanel("Summary Statistics"),
                       verbatimTextOutput("summary")
                     )
        )
      )
    )
     
    server <- function(input, output, session) {
     
     
     
      data <- reactive({
        req(input$file1)
     
        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)
     
        updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                          choices = names(df), selected = names(df)[sapply(df, is.numeric)])
        updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                          choices = names(df), selected = names(df)[sapply(df, is.numeric)])
     
        return(df)
     
      })
     
      x_axe <- reactive({
        data()[ , input$xcol]
      })
     
      y_axe <- reactive({
        data()[, input$ycol]
      })
     
      style <- reactive({
        input$graph
      })
     
      output$contents <- renderTable({
        data()
      })
     
      output$MyPlot <- renderPlotly({
        #x <- data()[, c(input$xcol, input$ycol)]
        p <- plotType(data(), x_axe(),
                 y_axe(),
                 style())
        p
     
     
      })
     
     
      output$summary <- renderPrint({
        y <- data()
        summary(y)
     
      })
     
    }
     
    # Create Shiny app
    shinyApp(ui = ui, server = server)

  2. #2
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2018
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Il y a une faute dans le titre. J'ai voulu ecrire : plotly

  3. #3
    Membre actif Avatar de Alpacky
    Homme Profil pro
    .
    Inscrit en
    Mars 2014
    Messages
    99
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mars 2014
    Messages : 99
    Points : 200
    Points
    200
    Par défaut
    bonjour,

    si tu utilises renderPlot au lieu de renderPlotly, cela fonctionne ?
    je pense que le problème vient du fait que tu ne transformes pas ton objet ggplot en ggplotly

    à tester, en espérant que cela aide,
    cdt,

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2018
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Oui tu as parfaitement raison. Cela fonctionne avec renderPlot. Cependant, lorsque j'utilise renderPlotly et ggplotly, je recois un message d'erreur.
    J'utilise ggplotly pour avoir un plus joli graphique.

    Voici mon code avec renderPlotly et ggplotly.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
      output$MyPlot <- renderPlotly({
        x <- data()[, c(input$xcol, input$ycol)]
        p <- plotType(x, input$xcol, input$ycol, input$graph)
        print(p)
        ggplotly(p)
      })
    Mais je recois le message d'erreur suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Listening on http://127.0.0.1:6505
    Warning in origRenderFunc() :
      Ignoring explicitly provided widget ID "24c3713fed73"; Shiny doesn't use them
    En lisant sur differents forums, j'ai cru comprendre le probleme viendrait probablement de la mise a jour de mes versions.

    Pourtant ma version de R ( R version 3.5.0) date du 23 Avril 2018. J'ai les versions suivantes:
    - plotly_4.7.1
    - shiny_1.1.0

    quelqu'un pourrait-il m'aider s'il vous plait?

    Amicalement,

  5. #5
    Membre actif Avatar de Alpacky
    Homme Profil pro
    .
    Inscrit en
    Mars 2014
    Messages
    99
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mars 2014
    Messages : 99
    Points : 200
    Points
    200
    Par défaut
    salut,

    dans ton ui change

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
          plotOutput('MyPlot') par plotlyOutput('MyPlot')
    ca fonctionne comme ca chez moi
    a+

  6. #6
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Juin 2018
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par Alpacky Voir le message
    bonjour,

    si tu utilises renderPlot au lieu de renderPlotly, cela fonctionne ?
    je pense que le problème vient du fait que tu ne transformes pas ton objet ggplot en ggplotly

    à tester, en espérant que cela aide,
    cdt,
    Waouh! Ca va faire plusieurs semaines que j'etais dessus! L'erreur venait donc de la!
    Je te remercie infiniment pour ton aide. Tu es le meilleur!

    Amities,
    Yves

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Inserer une image.jpg dans table
    Par loumanga dans le forum PostgreSQL
    Réponses: 7
    Dernier message: 30/01/2006, 14h14
  2. inserer une image BMP dans un fichier rtf
    Par Alice9 dans le forum MFC
    Réponses: 17
    Dernier message: 06/07/2004, 10h31
  3. Réponses: 3
    Dernier message: 15/04/2004, 08h44
  4. Réponses: 6
    Dernier message: 23/09/2003, 19h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo