I am trying to load an image from the www folder (this part works) and then using the image name to display it in the UI. When I try this I get the following error:
Warning: Error in cat: argument 1 (type 'closure') cannot be handled by 'cat'
Here is the fairly simple code
'''
library(shiny)
library(imager)
setwd("E:/CIS590-03I Practical Research Project/Project")
# ui object
ui <- fluidPage(
titlePanel(p("Dog Breed Classification", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
fileInput("image",
"Select your image:", placeholder = "No file selected"),
tags$head(
tags$style("body .sidebar {background-color: white; }",
".well {background-color: white ;}"),
),
p("Image to categorize"),
),
mainPanel(htmlOutput("testHTML"),
)
)
)
# server()
server <- shinyServer(function(input, output) {
output$testHTML <- renderText({
paste("<b>Selected image file is: ", input$image$name, "<br>")
reactive(img(
src = input$image$name,
width = "250px", height = "190px"
))
})
})
# shinyApp()
shinyApp(ui = ui, server = server)
'''
Any help will be greatly appreciated.
Thank you,
Bill.
The reason you are getting the error message is because the renderText is returning a reactive function rather than the image HTML tag. reactive shouldn't appear in any render... function.
As #MrFlick has mentioned, renderText will only return a character string to the UI. An alternative for renderUI and uiOutput is renderImage and imageOutput. These will add the uploaded image to the UI in a convenient way, as the render function only requires a list of attributes to give the img tag. This also allows easy inclusion of images that aren't in the www directory.
In the solution below, I have included req to the render functions so that error messages don't appear when no image has been uploaded.
library(shiny)
ui <- fluidPage(
tags$head(
tags$style(
".sidebar {background-color: white;}",
".well {background-color: white;}",
".title-text {color: #3474A7;}"
)
),
h2(
class = "title-text",
"Dog Breed Classification"
),
sidebarLayout(
sidebarPanel(
fileInput(
"image",
"Select your image:",
placeholder = "No file selected"
),
p("Image to categorize")
),
mainPanel(
tags$p(textOutput("filename", container = tags$b)),
imageOutput("testHTML")
)
)
)
server <- function(input, output) {
output$filename <- renderText({
req(input$image)
paste("Selected image file is:", input$image$name)
})
output$testHTML <- renderImage({
req(input$image)
list(
src = input$image$datapath,
width = "250px",
height = "190px"
)
}, deleteFile = TRUE)
}
shinyApp(ui = ui, server = server)
Related
I've reviewed similar posts and havenĀ“t found any that address this specific need. Below is very simple MWE of what I'm trying to do: shown in 2 images, and in runnable code. My "Hide" button (or remove UI) doesn't work. Help!! I'm sure it's a simple solution but I'm new to this.
What I'm trying to do: Click on the "Add" button and a file input prompt appears below. Click "Hide" button and the file input prompt goes away. Click "Add" again (after "Hide") and the file input prompt appears again. If you click "Add" now (and repeatedly), that single file input prompt remains. (Most other posts have the object appearing repeatedly, again and again in a growing column, with every additional click of the button - this isn't what I need). Just one click to make it appear (and clicking "Add" over and over just keeps it there in its original single manifestation), and "Hide" makes it go away. Simple as that.
Images:
library(shiny)
ui <- fluidPage(
h2("Testing showing and hiding of a function in UI ..."),
br(),
h3(actionButton("addBtn", "Add")),
h3(actionButton("hideBtn","Hide")),
uiOutput("FileInput"),
) # close fluid page
server <- function(input, output, session) {
output$FileInput <- renderUI({
"txt"
req(input$addBtn)
tagList(fileInput("file1", "Choose file",multiple= FALSE,
accept=c("csv",
"comma-separated-values",
".csv"), # close c
width=250,
buttonLabel = "select one file",
placeholder = "Add file"
), # close file input
)} # close tag list
)} # close render UI
observeEvent(input$hideBtn, {
removeUI(
selector = "div:has(> #txt)")
}) # close observe event
shinyApp(ui, server)
Perhaps you can use shinyjs package to get the desired result. Try this
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
h2("Testing showing and hiding of a function in UI ..."),
br(),
h3(actionButton("addBtn", "Add")),
h3(actionButton("hideBtn","Hide")),
uiOutput("FileInput"),
) # close fluid page
server <- function(input, output, session) {
output$FileInput <- renderUI({
"txt"
req(input$addBtn)
tagList(fileInput("file1", "Choose file",multiple= FALSE,
accept=c("csv",
"comma-separated-values",
".csv"), # close c
width=250,
buttonLabel = "select one file",
placeholder = "Add file"
) # close file input
) # close tag list
}) # close render UI
observeEvent(input$addBtn, {
shinyjs::show("FileInput")
})
observeEvent(input$hideBtn, {
shinyjs::hide("FileInput")
#removeUI(selector = "div:has(> #txt)")
})
}
shinyApp(ui, server)
If you do not want to use shinyjs package, you can use insertUI and removeUI as shown below.
library(shiny)
ui <- fluidPage(
h2("Testing showing and hiding of a function in UI ..."),
br(),
h3(actionButton("addBtn", "Add")),
h3(actionButton("hideBtn","Hide")),
uiOutput("FileInput"),
) # close fluid page
server <- function(input, output, session) {
output$FileInput <- renderUI({
req(input$addBtn)
tagList(tags$div(id = 'placeholder1')
) # close tag list
}) # close render UI
observeEvent(input$addBtn, {
if (input$addBtn==0){return(NULL)
}else {
insertUI(
selector = '#placeholder1' ,
## wrap element in a div with id for ease of removal
ui = tags$div(id="fi",
div(style="display: inline-block; width: 185px ;" ,
fileInput("file1", "Choose file",multiple= FALSE,
accept=c("csv",
"comma-separated-values",
".csv"), # close c
width=250,
buttonLabel = "select one file",
placeholder = "Add file"
) # close file input
))
)
}
})
observeEvent(input$hideBtn, {
if (input$hideBtn==0){return(NULL)
}else {
removeUI(selector = "div:has(> #fi)")
}
})
}
shinyApp(ui, server)
I successfully changed the text sizes in shiny dashboard interface by editing css file.
Or I use following structure:
div(DTOutput(outputId = "table"), style = "font-size:85%"))
However, I couldn't find the node name of shiny modals. Is it possible to change the text size in shiny modals through .css?
Are you looking for something like this?
shinyApp(
ui = basicPage(
actionButton("show", "Show modal dialog")
),
server = function(input, output) {
observeEvent(input$show, {
showModal(modalDialog(
title = "Important message",
div("This is an important message!", style="font-size:160%")
))
})
}
)
ModalDialog takes as its first argument(s) UI elements. This appears to be the same kind of arguments accepted by other shiny elements. Compare for example: ?siderbarPanel and ?modalDialog. So if you can do it in the body of an app, you can probably do it in a modal.
For example, I stuck a sidebar layout inside a modal:
shinyApp(
ui = basicPage(
actionButton("show", "Show modal dialog")
),
server = function(input, output) {
observeEvent(input$show, {
showModal(modalDialog(
sidebarLayout(sidebarPanel("yeah"),mainPanel("cool"))
))
})
}
)
I'm new to Shiny and can't figure out how to "unbold" labels (feed rate and operation in the screenshot attached). Here's my code for the UI part:
ui <- fluidPage(titlePanel(""),
sidebarLayout(
sidebarPanel(
# adding the new div tag to the sidebar
tags$div(class="header", checked=NA,
tags$h4(strong("Duty"))),
selectInput('id1', 'Feed rate (m^3/h)', c("All", main$metric[1:3])),
selectInput('id2', 'Operation', c("All", main$metric[4:5])),
mainPanel(DT::dataTableOutput("table"))
))
And here's the screenshot:
You can do this by adding your own style sheet to your Shiny app. First we give the sidebar panel a class sidebar so we can refer to it. Then we can add the following to a file www/style.css:
.sidebar label {
font-weight: 400;
}
Finally we set the theme parameter of your fluidPage to "style.css".
ui <- fluidPage(theme="style.css", titlePanel(""),
# content here
))
The result should look like this:
This is another option (you don't have to create a file)
library(shiny)
remove_bold <-"
#expr-container label {
font-weight: 400;
}
"
ui <- fluidPage(
titlePanel("My app"),
sidebarLayout(
sidebarPanel(
tags$style(remove_bold), ####### NEW CODE
tags$div(id = "expr-container", ####### NEW CODE
textInput(inputId = "data2", "Data1", value = "data"))
),
mainPanel(
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Inspired in this post: How to change the pickerinput label to fine instead of bold
I read all the threads about dynamic ui within the Shiny framework, but I did not find anything that work. I want to display a twitter timeline. This chunk of code works really well :
library(shiny)
library(shinydashboard)
runApp(list(ui = fluidPage(
tags$head(tags$script('!function(d,s,id){var js,fjs=d.getElementsByTagName(s) [0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");')),
titlePanel(""),
sidebarLayout(
sidebarPanel()
, mainPanel(
a("Tweets by Andrew Ng",
class="twitter-timeline",
href = "https://twitter.com/AndrewYNg"
)
)
)
)
, server = function(input, output, session){
}
)
)
But when, I try to make it reactive, I only got a link to the twitter timeline:
library(shiny)
library(shinydashboard)
runApp(list(ui = fluidPage(
tags$head(tags$script('!function(d,s,id){var js,fjs=d.getElementsByTagName(s) [0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");')),
titlePanel(""),
sidebarLayout(
sidebarPanel()
, mainPanel(
uiOutput("mytimeline")
)
)
)
, server = function(input, output, session){
output$mytimeline <- renderUI({
a("Tweets by Andrew Ng",
class="twitter-timeline",
href = "https://twitter.com/AndrewYNg"
)
})
}
)
)
The Twitter script only loads embedded content when it runs the first time. Since the script is in static UI but the timeline is in dynamic UI, the script will always run before the timeline is inserted.
The Twitter docs have a section about this: https://dev.twitter.com/web/javascript/initialization
You can run twttr.widgets.load() to scan the page for newly added embedded content.
One way to run execute this when inserting embedded content would be to include it in a script tag:
library(shiny)
twitterTimeline <- function(href, ...) {
tagList(
tags$a(class = "twitter-timeline", href = href, ...),
tags$script("twttr.widgets.load()")
)
}
runApp(list(ui = fluidPage(
tags$head(tags$script('!function(d,s,id){var js,fjs=d.getElementsByTagName(s) [0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");')),
titlePanel(""),
sidebarLayout(
sidebarPanel()
, mainPanel(
uiOutput("mytimeline")
)
)
)
,
server = function(input, output, session) {
output$mytimeline <- renderUI({
twitterTimeline("https://twitter.com/AndrewYNg", "Tweets by Andrew Ng")
})
}
))
See How to enable syntax highlighting in R Shiny app with htmlOutput for a similar issue with more details
My objective is to create a ShinyApp that opens a new empty UI whenever user clicks on submitButton.
Currently this is my code below. If the user types something in the text box and press Submit. The app shows what the user typed in the main panel. However I dont want to see the text, instead when the user clicks on the submit button , it should open a new empty UI.
ui = shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
column(3, wellPanel(
textInput("text", "Text:", "text here"),
submitButton("Submit")
)),
verbatimTextOutput("text")
)
)
)
server = function(input, output) {
output$plot1 <- renderPlot({
hist(rnorm(input$n))
})
output$text <- renderText({
paste("Input text is:", input$text)
})
}
shinyApp(ui=ui, server=server)
Is this possible ? Any tips or pointers are appreciated.
Well, this is not yet very functional, but does what you asked for.
ui = shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
uiOutput("newWindowContent", style = "display: none;"),
tags$script(HTML("
$(document).ready(function() {
if(window.location.hash != '') {
$('div:not(#newWindowContent)').hide();
$('#newWindowContent').show();
$('#newWindowContent').appendTo('body');
}
})
")),
a(href = "#NEW", target = "_blank",
actionButton("Submit", "Submit")
)
))
)
server = function(input, output) {
output$newWindowContent <- renderUI({
"Welcome to your new window!"
})
}
shinyApp(ui=ui, server=server)
The app is created, such that the ui created in newWindowContent is displayed in the new window. Sadly, new windows are somewhat cut off from the parent page, such that there is no easy way to configure each page independently. At the moment, all show the same content. None have reactivity features. I guess there can be initial configurations, if one uses the window's hash. But this works only client sided.
Nevertheless, it's a good start!