tomas hujo tomas hujo - 3 months ago 15
R Question

Plot in shiny display in viewer in R studio instead of web browser

this is my first shiny app I am trying to do.

Previously I reviewed some tutorials and when I ran app externally all of it was displayed in web browser.

Now everything is displayed in webbrowser but a plot which is displayed in viewer in R studio. It's reactive to sliderInput which is properly displayed in web browser.I obviously want to have it displayed everything in webbrowser

What could be the problem ? I did it reproducible example with diamonds dataset
the code is following:

ui.R

library(shiny)
library(ggvis)
library(ggplot2)
library(dplyr)

shinyUI(fluidPage(

titlePanel("Analyza demografickych udajov Slovenskej republiky"),

sidebarLayout(
sidebarPanel(
sliderInput("rok","Vyber rok ktory chces pozriet",min = min(diamonds$table),max = max(diamonds$table),value = 60,step = 1)

),



mainPanel(
tabsetPanel(type="tab",
tabPanel("plot",plotOutput("plot")),
tabPanel("Vzorka raw dat",tableOutput("table"))

)
)



)))



server.R

library(shiny)
library(ggvis)
library(ggplot2)
library(dplyr)

#read.csv("Demog.csv")
#p<-read.table(file = "Demog.csv",header = T,sep = ";")
#head(p)
#colnames(p)[2]<-"rok"
#colnames(p)[3]<-"pocet obyvatelov"

shinyServer(function(input,output){

output$table<-renderTable({

head(diamonds,10)
})

output$plot<-renderPlot({

diamonds%>%filter(table==as.numeric(input$rok))%>%ggvis(~depth,~price)%>%layer_lines

})

})

}

Answer

This is one solution:

In the server, use reactive instead of renderPlot,

  plot<-reactive({           
  diamonds%>%filter(table==as.numeric(input$rok))%>%ggvis(~depth,~price)%>%layer_lines
  })

and bind the ggvis plot to shiny outside of the reactive statement:

plot%>% bind_shiny(plot_id="plot")

In the ui, your output in the main panel should then be:

      tabsetPanel(type="tab",
              tabPanel("plot",ggvisOutput("plot")),
              tabPanel("Vzorka raw dat",tableOutput("table"))

  )

Also note that, you don't need to library the packages in your ui.

Hope this helps