John
John

Reputation: 11

Googlevis, R shiny : not in a new window

I have a problem. I am trying to use package Googlevis with R shiny and I have some problems.

 library (googleVis)
    library(shiny)
    library(shinythemes)

df=data.frame(annee=c("2000","2005","2010","2014","2015","2016","2017"), val= c(30.0,27.0,29.7,28.6,28.8,29.4,26.))
graph2 <- {gvisLineChart(df)}

shinyApp(
  ui = navbarPage(
    theme = shinytheme("yeti"),"Pageweb",
    tabPanel("Page1",
             sidebarPanel(
               tags$em("TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE")
               ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Onglet1", plotOutput("graph1"), "Texte de présentation", plot(graph2)),
                 tabPanel("Onglet2"),
                 tabPanel("Onglet3")
               )
             )
               )),
  server = function(input, output) {

    output$graph1 <- renderPlot({
      y = c(30.0,27.0,29.7,28.6,28.8,29.4,26.)
      barplot(y,names.arg=c("2000","2005","2010","2014","2015","2016","2017"),main="Titre graph1",
              xlab="Années", ylab="Pourcentage (en %)", col="#2345AA", border="#515353",space=0.3,
              ylim=c(0,35), density=c(30))
    })  
  }
  )

The graph created with Googlevis opens in a new window but i want to implemant it in the R shiny page. I tried htmloutput function without results.

If someone has an idea to put the graph made by Googlevis in the page made by R shiny

df=data.frame(annee=c("2000","2005","2010","2014","2015","2016","2017"), val= c(30.0,27.0,29.7,28.6,28.8,29.4,26.))
    graph2 <- {gvisLineChart(df)}

Also, I am forced to declare those 2 lines at the begining. If not, the variable is not found.

Upvotes: 1

Views: 104

Answers (1)

MalditoBarbudo
MalditoBarbudo

Reputation: 2015

With googleVis plots, you need the same logic as with any other plot. In the same way you render your graph1 with plotOutput in the UI and renderPlot in the server, for the googleVis graph2 is the same, using htmlOutput in the UI and renderGvis in the server:

library (googleVis)
library(shiny)
library(shinythemes)

shinyApp(
  ui = navbarPage(
    theme = shinytheme("yeti"),"Pageweb",
    tabPanel("Page1",
             sidebarPanel(
               tags$em("TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE TEXTE")
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Onglet1", plotOutput("graph1"),
                          "Texte de présentation", htmlOutput('graph2')),
                 tabPanel("Onglet2"),
                 tabPanel("Onglet3")
               )
             )
    )),
  server = function(input, output) {

    output$graph1 <- renderPlot({
      y = c(30.0,27.0,29.7,28.6,28.8,29.4,26.)
      barplot(y,names.arg=c("2000","2005","2010","2014","2015","2016","2017"),main="Titre graph1",
              xlab="Années", ylab="Pourcentage (en %)", col="#2345AA", border="#515353",space=0.3,
              ylim=c(0,35), density=c(30))
    })
    output$graph2 <- renderGvis({
      df=data.frame(annee=c("2000","2005","2010","2014","2015","2016","2017"), val= c(30.0,27.0,29.7,28.6,28.8,29.4,26.))
      gvisLineChart(df)
    })
  }
)

In shiny, most widgets (leaflet, plotly, googleVis...) provide output and render functions to use in the UI and server, respectively. In this case the renderGvis function.

The data and googleVis chart creation can be put on the renderGvis function, similar at what you did in the renderPlot call.

Upvotes: 0

Related Questions