shiny screenshot appears with colorless legend - r

In shiny the ggplotly visual looks like:
If I create a screenshot with shinyscreenshot it appears with a colorless legend:
Is there any way to solve this issue?
Update 12:04
It seems that this issues only appears with colorbars. With factorized groups the legend works:
Unfortunately, in real world data I need the colorbar as its a range between 0.0001 and 100%.
MWE
library(shiny)
library(shinydashboard)
library(shinyscreenshot)
library(data.table)
library(DT)
library(bslib)
library(plotly)
################################################################################
################################ S E R V E R ###################################
################################################################################
server = shinyServer(function(input,output){
output$histogram = renderPlotly(
ggplotly(ggplot(mtcars, aes(x=disp, y=hp, color=gear)) + geom_point())
)
output$active_cases = DT::renderDataTable(
mtcars)
######################### SCREENSHOT REACTIVE FUNCTION #########################
observeEvent(input$go,{
screenshot(id = "to_plot") # plot only ID "to_plot"
})
######################### SCREENSHOT REACTIVE FUNCTION #########################
})
################################################################################
#################################### U I #######################################
################################################################################
ui = shinyUI(
dashboardPage(
dashboardHeader(
title="just a test"
),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="active_cases", icon = icon("magnifying-glass-location"))
)
),
dashboardBody(
tabItems(
tabItem(tabName="active_cases", shiny::h2("Active Cases"),
fluidRow(actionButton("go", "go")),
fluidRow(
box(title="Table", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases")))
),
div(id="to_plot",
fluidRow(
box(title="Visual1", status="primary", solidHeader=TRUE, plotlyOutput("histogram"))
)
)
)))))
################################################################################
################################### R U N ######################################
################################################################################
shinyApp(ui, server)

You could use library(capture) instead of library(shinyscreenshot):
# remotes::install_github("dreamRs/capture")
library(shiny)
library(shinydashboard)
library(capture)
library(data.table)
library(DT)
library(bslib)
library(plotly)
################################################################################
################################ S E R V E R ###################################
################################################################################
server = shinyServer(function(input,output){
output$histogram = renderPlotly(
ggplotly(ggplot(mtcars, aes(x=disp, y=hp, color=gear)) + geom_point())
)
output$active_cases = DT::renderDataTable(
mtcars)
})
################################################################################
#################################### U I #######################################
################################################################################
ui = shinyUI(
dashboardPage(
dashboardHeader(
title="just a test"
),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="active_cases", icon = icon("magnifying-glass-location"))
)
),
dashboardBody(
tabItems(
tabItem(tabName="active_cases", shiny::h2("Active Cases"),
fluidRow(
capture::capture(
selector = "#to_plot",
filename = "capture_plotly.png",
icon("camera"), "Capture plotly graph",
options=list(backgroundColor = "white")
)
),
fluidRow(
box(title="Table", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases")))
),
div(id="to_plot",
fluidRow(
box(title="Visual1", status="primary", solidHeader=TRUE, plotlyOutput("histogram"))
)
)
)))))
################################################################################
################################### R U N ######################################
################################################################################
shinyApp(ui, server)

Related

How to shrink DT datatable without reducing font-size

The datatable in my shiny app looks like this:
I don't like the scrollbar and think it's not really necessary to have it if I could shrink the table a bit.
I could do it with div(DT::dataTableOutput("active_cases"), style = "font-size: 75%;").
But with my real-world data, it's still a little bit too wide. If I see the yellow marked columns I don't think that there is a need to reduce font size if I could reduce the space between columns.
Is there a way to make such a table more compact/dense?
MWE
library(shiny)
library(data.table)
library(DT)
library(bslib)
################################################################################
################################ S E R V E R ###################################
################################################################################
server = shinyServer(function(input,output){
output$histogram = renderPlot(
hist(faithful$eruptions, breaks=input$days_plot)
)
output$active_cases = DT::renderDataTable(
mtcars, selection = 'single', options=list(scrollX=TRUE))
})
################################################################################
#################################### U I #######################################
################################################################################
ui = shinyUI(
dashboardPage(
dashboardHeader(
title="iris test"
),
dashboardSidebar(
#h3("Downstream", style="text-align:center;
# color:white;
# background-color:red'"
# ),
sidebarMenu(id="tabs",
menuItem("Iris - Active Cases", tabName="active_cases", icon = icon("magnifying-glass-location")),
menuItem("Iris - Archive", tabName="archive", icon = icon("box-archive")),
menuItem("Configuration", sliderInput("days_plot", "Days into past", 1, 60, 30))
)
),
dashboardBody(
tabItems(
tabItem(tabName="active_cases", shiny::h2("Active Cases"),
fluidRow(
box(title="Cases", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases"))),
box(title="Cases", status="primary", solidHeader=TRUE, plotOutput("histogram"))
)
),
tabItem(tabName="archive", shiny::h2("Archive"))
)
)
)
)
################################################################################
################################### R U N ######################################
################################################################################
shinyApp(ui, server)
Use following CSS style:
library(shiny)
library(data.table)
library(DT)
library(bslib)
library(shinydashboard)
################################################################################
################################ S E R V E R ###################################
################################################################################
server = shinyServer(function(input,output){
output$histogram = renderPlot(
hist(faithful$eruptions, breaks=input$days_plot)
)
output$active_cases = DT::renderDataTable(
DT::datatable(mtcars, selection = 'single', options=list(scrollX=TRUE))
)
})
################################################################################
#################################### U I #######################################
################################################################################
ui = shinyUI(
dashboardPage(
dashboardHeader(
title="iris test"
),
dashboardSidebar(
#h3("Downstream", style="text-align:center;
# color:white;
# background-color:red'"
# ),
sidebarMenu(id="tabs",
menuItem("Iris - Active Cases", tabName="active_cases", icon = icon("magnifying-glass-location")),
menuItem("Iris - Archive", tabName="archive", icon = icon("box-archive")),
menuItem("Configuration", sliderInput("days_plot", "Days into past", 1, 60, 30))
)
),
dashboardBody(
tabItems(
tabItem(tabName="active_cases", shiny::h2("Active Cases"),
fluidRow(
tags$style(
'
#active_cases th {padding: 0;}
/* #active_cases :is(th, td) {padding: 0;} */
'
),
box(title="Cases", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases"), style = "width: 100%")),
box(title="Cases", status="primary", solidHeader=TRUE, plotOutput("histogram"))
)
),
tabItem(tabName="archive", shiny::h2("Archive"))
)
)
)
)
################################################################################
################################### R U N ######################################
################################################################################
shinyApp(ui, server)
To make it more compact, use the second commented line in the style instead #active_cases :is(th, td) {padding: 0;}:

If I use the function includeHTML in shiny, javascript based graphing packages dont work

Once I add an HTML document in my shiny app my graphs stop populating. I am using bs4dash but shinydashboard also has the exact same issue.
Below is my code as well as a screenshot of what is happening.
Code before i add HTML document
Ui
library(shiny)
library(bs4Dash)
library(highcharter)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
box(width = 12,
plotlyOutput("plot1")
)
),
fluidRow(
box(
#width = 12,
#includeHTML("first.html")
)
)
)
)
Server
server <- function(input, output) {
a = rnorm(100)
output$plot1 = renderPlotly({
plot_ly(x = ~a, type = "histogram")
})
}
Now when i remove the hastags to display my HMTL document. My graph all of a sudden disappears.
Ui
library(shiny)
library(bs4Dash)
library(highcharter)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
box(width = 12,
plotlyOutput("plot1")
)
),
fluidRow(
box(
width = 12,
includeHTML("first.html")
)
)
)
)
Server
server <- function(input, output) {
a = rnorm(100)
output$plot1 = renderPlotly({
plot_ly(x = ~a, type = "histogram")
})
}
I would like to have the graph still show. What is going wrong in the code. Thank you
I cannot reproduce your problem. I just observed that your fluidRow is the fourth parameter of dashboardPage which, however, expects a dashboardControlbar. Both, putting the fluidRow into dashboardBody or wrapping it in a call to dashboardControlbar works for me.
So either it is your first.html or indeed "just" the missing dashboardControlbar.
first.html
<span>I am an external HTML</span>
app.R
library(shiny)
library(bs4Dash)
library(highcharter)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
box(width = 12,
plotlyOutput("plot1")
),
fluidRow(
box(
width = 12,
includeHTML("first.html")
)
)
)
)
ui2 <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
box(width = 12,
plotlyOutput("plot1")
)
),
dashboardControlbar(
fluidRow(
box(
width = 12,
includeHTML("first.html")
)
)
)
)
server <- function(input, output) {
a = rnorm(100)
output$plot1 = renderPlotly({
plot_ly(x = ~a, type = "histogram")
})
}
shinyApp(ui, server)
## shinyApp(ui2, server) ## works likewise
Screenshots

Shiny with ggplot and dashboardPage doesn't work

I don't understand why this app doesn't work?
The app works without the content within the server part.
It works whith the UI definition and the server function empty.
When I added ggplot code in server function the app leaf to work.
Could you help me?
Thanks
#install.packages("quantmod")
#install.packages('circlepackeR')
library(quantmod)
library(ggplot2)
library(shiny)
library(data.table)
library(plotly)
library(shinydashboard)
apple=as.data.frame(getSymbols("AAPL", src = "yahoo", from = "2010-01-01", to = "2020-10-15", auto.assign = FALSE))
colnames(apple)=c('Apertura','Maximo','Minimo','Cierre','Volumen','Ajustado')
apple$fecha=as.Date(rownames(apple))
ui <- fluidPage(
dashboardPage(
dashboardHeader(title = "SP500 Top-5"),
dashboardSidebar(
sidebarMenu(
menuItem("Apple", tabName = "aapl")
)
),
dashboardBody(
# tags$head(
# tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
# ),
tabItems(
tabItem(tabName = "aapl",
fluidRow(
tabsetPanel(
tabPanel("Plot", plotOutput("plota1")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)))))))
server <- function(input, output) {
apple=reactive({apple})
output$plota1 <- renderPlot({
g=ggplot()+
geom_line(mapping=aes(x=fecha, y=Cierre), data=apple(), size=1, alpha=0.5)+
scale_x_date("Fecha") + scale_y_continuous(name="Serie de precios de cierre")+
ggtitle ("Comportamiento diario de la acción APPPLE")
g
})
}
shinyApp(ui = ui, server = server)
Inside the server, change the reactive name to something other than apple. I defined it as df1, and used airquality data, and I get the output as shown below. Otherwise, your code is fine.
apple <- as.data.frame(airquality)
apple$fecha <- apple$Day
apple$Cierre <- apple$Temp
ui <- dashboardPage(
dashboardHeader(title = "SP500 Top-5"),
dashboardSidebar(
sidebarMenu(
menuItem("Apple", tabName = "aapl")
)
),
dashboardBody(
# tags$head(
# tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
# ),
tabItems(
tabItem(tabName = "aapl",
fluidRow(
tabsetPanel(
tabPanel("Plot", plotOutput("plota1")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", DTOutput("table"))
))))))
server <- function(input, output) {
df1 <- reactive({apple})
output$plota1 <- renderPlot({
g <- ggplot(data=df1())+
geom_line(mapping=aes(x=fecha, y=Cierre), size=1, alpha=0.5)+
#scale_x_date("Fecha") + scale_y_continuous(name="Serie de precios de cierre")+
ggtitle ("Comportamiento diario de la acción APPPLE")
g
})
output$table <- renderDT(df1())
}
shinyApp(ui = ui, server = server)
My knowledge on all things R is still a bit low especially when it comes to shiny. But to try get better I've been involving myself in the Qs to see if I can find a work through.
With this one, in the server code I removed
apple=reactive({apple})
and data=apple(). Knowing ggplot I wasn't sure why apple was a function anyway so I just had it read data=apple
This got the server ui function to run graph as shown. Obviously you haven't included any information on the summary or table parts so they remain blank. This worked for me, but I could be completely way off the mark and could run into other problems down the line.

R- Shiny - how to change color of legends in pie chart?

I want to change the color of legends in piechart.
Here is the code:
library(ECharts2shiny)
library(shiny)
dat5 <- c(rep("Female", 3376), rep("Male", 2180))
ui <- shinyUI(
dashboardPage(dashboardHeader(title = "PSM"),
dashboardBody(
mainPanel(
tabsetPanel(
tabPanel(
loadEChartsLibrary(), tags$div(id="test5",
style="width:60%;height:300px;"),
deliverChart(div_id = "test5"))
server <- shinyServer(function(input,output){
renderPieChart(div_id = "test5", data = dat5 ) })
Can anybody help me how to change the legends color?
hope it helps:
library(shiny)
library(shinydashboard)
dat5 <- c(rep("Female", 3376), rep("Male", 2180))
app <- shinyApp(
ui <- shinyUI(
dashboardPage(dashboardHeader(title = "PSM"),
dashboardSidebar(),
dashboardBody(
mainPanel(
tabsetPanel(
tabPanel(tags$div(id="test5",
style="width:60%;height:300px;"),
plotOutput("pie_chart"))
))))
),
server <- shinyServer(function(input,output){
output$pie_chart <- renderPlot({
df <- table(dat5)
cols <- rainbow(length(df))
pie(df, col = cols)
})
})
)
runApp(app)

Implementing span to click on plot and move in R shiny

The given code creates a simple scatterPlot. I wish to click on the plot and move it in any direction that I want to, basically the span functionality. Attached the snapshot for references.Please help and thanks.
## app.R ##
library(shiny)
library(shinydashboard)
library(bupaR)
library(edeaR)
library(eventdataR)
library(processmapR)
library(processmonitR)
library(xesreadR)
library(lubridate)
library(dplyr)
library(knitr)
library(XML)
library(xml2)
library(data.table)
library(ggplot2)
library(ggthemes)
library(glue)
library(tibble)
library(miniUI)
library(tidyr)
library(shinyTime)
library(petrinetR)
library(magrittr)
library(shinyWidgets)
library(DiagrammeR)
ui <- dashboardPage(
dashboardHeader(title = "Zoom and Reset Dashboard",titleWidth = 290),
dashboardSidebar(
width = 0
),
dashboardBody(
# Creation of tabs and tabsetPanel
tabsetPanel(type = "tab",
tabPanel("Resource Dashboard",
fluidRow(column(10,
grVizOutput("res_freq_plot")))),
id= "tabselected"
)
))
server <- function(input, output)
{
output$res_freq_plot <- renderDiagrammeR(
{
patients %>% process_map()
}
)
}
shinyApp(ui, server)
You can use plotly
## app.R ##
library(shiny)
library(plotly)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Zoom and Reset Dashboard",titleWidth = 290),
dashboardSidebar(
width = 0
),
dashboardBody(
# Creation of tabs and tabsetPanel
tabsetPanel(type = "tab",
tabPanel("Resource Dashboard",
fluidRow(column(10,
plotlyOutput("res_freq_plot")))),
id= "tabselected"
)
))
server <- function(input, output)
{
output$res_freq_plot <- renderPlotly(
{
plot_ly(iris, x= iris$Petal.Length, y = iris$Sepal.Length)
}
)
}
shinyApp(ui, server)

Resources