Download multiple files to one PDF shiny - r

I have an app with multiple pages that each have two plots and two tables. Is there a way to download all 4 as one file? A PDF would be nice but if there's a different file type that would work better, I'm open to that.
I tried following the suggestion here but I could not get it to work. I need the plots to be interactive within the app, but they can be static when downloaded. Any suggestions or is this even possible? Is there a way to even just print the main panel?
library(plotly)
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
plotlyOutput("SepalPlot"),
DT::dataTableOutput("Sepal"),
plotlyOutput("PetalPlot"),
DT::dataTableOutput("Petal")
)
)
server <- function(input, output) {
output$SepalPlot<- renderPlotly({
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'scatter', mode = 'markers')
})
sep<-data.frame(c(iris$Sepal.Length, iris$Sepal.Width))
output$Sepal<-renderDataTable({datatable(sep)})
output$PetalPlot<- renderPlotly({
plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter', mode = 'markers')
})
pet<-data.frame(c(iris$Petal.Length, iris$Petal.Width))
output$Petal<-renderDataTable({pet})
}
shinyApp(ui = ui, server = server)

Related

Click and zoom in a plot_ly in R Shiny application without reinitialisation

I am using R Shiny and I am plotting some 3D point cloud with plot_ly from the library plotly. I can move and zoom on the points.
When I click on some point, some information about the point is stored in a variable, but then this resets the visualization.
It is possible to prevent this reinitialization? E.g. I would like to zoom on some part of the data, and then successively click on the points without any reset..
Here is a reproducible example :
library(shiny)
library(plotly)
df=iris
ui <- fluidPage(
plotlyOutput("plot3D"),
textOutput("selection")
)
server <- function(input, output, session) {
react <- reactiveValues(value = 0)
output$plot3D <- renderPlotly({
click_data <- event_data("plotly_click", priority = "event")
if (!is.null(click_data)) {
react$value<-click_data$customdata
}
fig<-plot_ly(df,
x=~Sepal.Length,y=~Sepal.Width,z=~Petal.Length,
type="scatter3d",
mode = 'markers',
customdata = ~Species
)
fig
})
output$selection <- renderPrint({
react$value
})
}
shinyApp(ui = ui, server = server)
And here is a gif about what happens :
The issue is, that you are collecting the event_data inside your renderPlotly call. Accordingly your plot is re-rendered with each click event (reactive dependency).
Please check the following:
library(shiny)
library(plotly)
library(datasets)
DF <- iris
ui <- fluidPage(plotlyOutput("plot3D"),
textOutput("selection"))
server <- function(input, output, session) {
output$plot3D <- renderPlotly({
plot_ly(
DF,
x = ~ Sepal.Length,
y = ~ Sepal.Width,
z = ~ Petal.Length,
type = "scatter3d",
mode = 'markers',
customdata = ~ Species,
source = "myscatter3d"
)
})
click_data <- reactive({
event_data("plotly_click", source = "myscatter3d", priority = "event")
})
output$selection <- renderPrint({
click_data()
})
}
shinyApp(ui = ui, server = server)

Plotly R- Can not adjust orientation of colorbar

I am trying to create a scatterplot within a Shiny App that has a colorbar with a horizontal orientation. Based on my reading of the Plotly reference it appears that the orientation of a colorbar can be adjusted within Layout/Coloraxis/Colorbar by setting orientation="h".
After many attempts and tweaks I can not find a way to get the colorbar to be displayed horizontally.
Any help would be appreciated.
Minimal example using the iris dataset:
library(shiny)
ui <- fluidPage(
plotlyOutput("Scatter")
)
server <- function(input, output) {
ScatterPlot<-reactive({
plot_ly(
iris,
type='scatter',
mode='lines+markers'
)%>%
add_trace(
x=~Sepal.Length,
y=~Sepal.Width,
mode='markers',
marker=list(
color=~Petal.Length,
coloraxis='coloraxis'
)
)%>%
layout(
showlegend=FALSE,
margin=list(b=50),
coloraxis=list(
colorbar=list(
orientation="h"
)
)
)
}
)
output$Scatter<-renderPlotly({ScatterPlot()})
}
shinyApp(ui = ui, server = server)

Changing Dimensions of downloaded plot of plotly in r shiny

I have a code which downloads the png image of the graph formed in plotly with same dimensions everytime. I want to generate a image with higher quality in it. Like if i click on the buttons provided at the top right corner in plotly graphs, it should download with different dimensions
Reference code is given below
library(shiny)
library(plotly)
ui <- fluidPage(
selectInput("choice", "Choose", choices = names(iris), selected = NULL),
plotlyOutput("graph")
)
server <- function(input, output, session){
output$graph <- renderPlotly({
plot_ly(iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers')
})
}
shinyApp(ui, server)
You can use toImageButtonOptions in the config function to set the dimensions:
plot_ly(
iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers'
) %>% config(
toImageButtonOptions = list(format = "png", width = 1500, height = 750)
)

includeHTML conflicting with renderPlotly

I'm creating a small app with shiny to show simulation results based on user input with plot_ly() (need plotly for animation). It utilizes navbarpage() to show a home page (where I explain the rationale) and a simulation page (where the app is actually displayed).
To create the homepage, I created a .Rmd file and knitted to html. Unfortunately, it appears that includeHTML() and renderPlotly() have some sort of javascript conflict and so plotly will not render. Double unfortunately, I know almost nothing about HTML or javascript.
A simple (almost reprex) version:
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage("RCV", position = "fixed-top", collapsible = TRUE,
tabPanel("Home",
includeHTML("www/yourFav.html")),
tabPanel("Simulation",
plotlyOutput("plot")
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session){
output$plot <- renderPlotly({
plot_ly(data = cars,
x = ~mgp,
y = ~wt)
})
}
Any suggestions you have will be well received!
Best,
Brennan
includeHTML is intended to be used for HTML fragments. Use an iframe for a full HTML page. The HTML file must be in the www subfolder, and you have to pass it to the src argument of tags$iframe without the www/ prefix.
library(shiny)
library(plotly)
ui <- fluidPage(
navbarPage("RCV", position = "fixed-top", collapsible = TRUE,
tabPanel("Home",
tags$iframe(src = "rcv_homePage.html",
width = "600", height = "500",
style = "margin-top: 70px;")),
tabPanel("Simulation",
plotlyOutput("plot")
)
)
)
server <- function(input, output, session){
output$plot <- renderPlotly({
plot_ly(data = cars,
x = ~mgp,
y = ~wt)
})
}
shinyApp(ui, server)

Plot_ly function doesn't show a plot in shiny App

I am working with R version 3.6.0.
When I try to build a plot with plotly inside of a shiny App, it only shows a white space.
Also when I run the plotly function by itself, it is not shown in the Viewer, only when I open it in a new window.
library(shiny)
library(plotly)
library(datasets)
shinyApp(
ui <- fluidPage(plotlyOutput("plot1")),
server <- function(input, output) {
p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers')
output$plot1 <- renderPlotly({p})
}
)

Resources