I'm building a ShinyApp where different plots can be coloured based on several variables according to a selectInput panel, and a different image should also be inserted depending on the user's selection on such panel.
I'm failing to be able to render the images. I just get a blank space where they should appear.
I'm pretty sure I'm missing something obvious here, but I'm still experimenting with shiny and just can't figure it out. Thanks for your help.
Relevant parts of my code (let me know if you need more):
Server:
shinyServer(function(input, output) {
output$Imagen<- renderImage({
if(input$ColorBy=="Raza") Leg<-"Razaimagen.png"
if(input$ColorBy=="Log") Leg<-"Logimagen.png"
list(src=Leg)
})
})
UI:
shinyUI(fluidPage(
fluidRow(
column(3,
h3("Imagen", align = "center"),
imageOutput(outputId="Imagen")
))
))
"Razaimagen.png" and "Logimagen.png" are sitting happily in my www folder. They work well if I try eg img(src="Razaimagen.png") directly in the ui.R
Your deleting the images, use:
output$Imagen<- renderImage({
if(input$ColorBy=="Raza") Leg<-"www/Razaimagen.png"
if(input$ColorBy=="Log") Leg<-"www/Logimagen.png"
list(src=Leg)
}, deleteFile = FALSE)
Related
i'm trying to show a logo in my Shiny app, but when i try to see i only can see a small icon, but not the image, this is my code:
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='C:/Users/carlo239/image[![enter image description here][1]][1]/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
And this is the issue, i tried to reload, but is not working,
I know that the image is ok, because if i click on Download i get the same image, but i'm unable to see in my outputs. THANKS !!
There are two ways:
Put the file in the www folder of your app (as suggested by #YBS). That is in most cases the recommended solution.
"Register" your folder as resource folder for your app (see below). However, that solution is intended to help package authors make resources available to the package's components. Also be aware that absolute paths can pose problems when you deploy your app.
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='/foo/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
addResourcePath("foo", "C:/Users/wherever/your/file/may/be/located")
}
shinyApp(ui, server)
I've got a Flexdashboard-based Shiny application with several tabs, and within each tab, grids of multiple plots. Performance is a bit of an issue, particularly when deployed on the free Shiny Server.
Initially, the main issue was that clicking each tab would require re-rendering of the plots. I set the suspendWhenHidden option to be FALSE, and this helps - Now switching the input has a slow delay to load all the plots, but at least when navigating the UI, performance is snappy.
This got me thinking, however - is there any way to achieve a hybrid of the two behaviors? So say I'm on an active tab which just produces a single plot. This plot renders quickly. Can we tell shiny to render this plot, display it to the user, and then in the background, continue loading all the elements of the other tabs? As it stands now, the active tab will not finish rendering the plot until all plots on the hidden tabs are also rendered.
In summary, a hybrid suspendWhenHidden = FALSE and TRUE:
Render the active tab elements first, display to the user, then
Continue rendering the elements on the hidden tabs
I thought perhaps setting priority might achieve this, but it doesn't seem to work. Any thoughts or suggestions?
Here's a minimal reproducible example. The goal would be for the first plot (in tab 1) to render and appear before beginning rendering of the second plot (in tab 2) - But the plot should start rendering in tab 2 without requiring a click of tab 2.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput('n', 'Size', 10)
),
mainPanel(
tabsetPanel(
tabPanel("Tab1", plotOutput("plot1")),
tabPanel("Tab2", plotOutput("plot2"))))
)
)
# Define the server code
server <- shinyServer(function(input, output, session) {
output$plot1 <- renderPlot({plot(1:input$n)},height = 400,width=800)
output$plot2 <- renderPlot({ Sys.sleep(5); plot(1:input$n,col="red")},height = 400,width=800)
outputOptions(output, "plot2", suspendWhenHidden = FALSE)
})
# Return a Shiny app object
shinyApp(ui = ui, server = server)
There is two ways to achieve this
Load all the tabs and then show the output
Use eventReactive to activate the other tabs
For the first option just put below additionally
outputOptions(output, "plot1", suspendWhenHidden = FALSE)
and if you want reactivity write eventReactive functions for each tab.
This question is a follow-up to How can put multiple plots side-by-side in shiny r? and R Shiny layout - side by side chart and/or table within a tab.
Is it possible to embed two side-by-side plots in a tab panel when there are other outputs present besides the two plots I want to embed side-by-side? I have tried every suggestion offered in the answers to the previous two questions and nothing works. No matter what I try I end up breaking my program so it refuses to display any out put at all.
mainPanel(
width = 10,
tabsetPanel(
tabPanel("Dashboard", textOutput("text30"), textOutput("text31"), textOutput("text28"), textOutput("text29"),
column(6, plotOutput("plot1st"), column(6, plotOutput("plot2nd")), #what do I put here to make this work?
plotOutput("plot3rd"), plotOutput("plot9th"), plotOutput("plot4th"), plotOutput("plot8th"), plotOutput("plot7th"), plotOutput("plot6th")),
tabPanel("Graph Switcher", plotOutput("selected_graph"))
))))
Is there anything I can do to make this work?
Update: Thanks to Florian's answer I was able to make the app look the way I wanted. However, as can be seen in the screenshot below, a small number 12 appears in between the plots, and the plots are not perfectly horizontally aligned. I'm wondering if it's possible to fix this?
You can put multiple column elements in one fluidRow:
Hope this helps!
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("Dashboard",
fluidRow(12,
column(6,plotOutput('plot1')),
column(6,plotOutput('plot2'))
),
fluidRow(12,
column(4,plotOutput('plot3')),
column(4,plotOutput('plot4')),
column(4,plotOutput('plot5'))
),
fluidRow(12,
column(3,plotOutput('plot6')),
column(3,plotOutput('plot7')),
column(3,plotOutput('plot8')),
column(3,plotOutput('plot9'))
)
)))
server <- function(input,output)
{
lapply(seq(10),function(x)
{
output[[paste0('plot',x)]] = renderPlot({hist(runif(1:20))})
})
}
shinyApp(ui,server)
I would like to add a Font Awesome 'child' icon twice in my actionButton in Shiny. The following app displays the child once:
library(shiny)
ui <- fluidPage(
actionButton("child","children", icon("child"))
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
I tried the obvious to no avail:
actionButton("child","children", icon(c("child","child")))
I must accept this is quite a niche question.
This has nothing to do with the fact that you want the same icon twice. You just can't pass a vector of names into the icon() function. If you look at the documentation of icon() it says it accepts the name of an icon, not a vector for multiple icons.
To do what you want, you can simply add multiple icons in the label. Something like this
actionButton("child", div(icon("child"), icon("child"), "children"))
I am trying to include a selectizeInput widget in a Shiny app. However, one aspect of its behavior is problematic: each time I make a selection, the box containing the choices closes.
I took a look at the example app here: http://shiny.rstudio.com/gallery/selectize-examples.html. Specifically, input number 2: Multi-select. The selection window remains open in this example, yet I see no differences between that code and mine which would account for the variance in behavior.
For the sake of a reproducible example, I have put together the following code:
ui <- fluidPage(uiOutput("example"))
server <- function(input, output, session){
output$example <- renderUI({
selectizeInput(
inputId="people",
label=NULL,
choices=paste("A", 1:50, sep="_"),
multiple = TRUE,
selected=input$people
)
})
} # close server
shinyApp(ui = ui, server=server)
My guess is that I'm missing something obvious, so here's a chance for an easy answer for someone that knows their way around Shiny. Any help will be greatly appreciated.
When you remove the selected=input$people line, it works as intended.