问题
I am creating an app using Shiny and would like to include a thermometer plot, created using the symbols() function. I written the following code for my thermometer plot, and it works perfectly fine in the plot viewer in RStudio:
symbols(0, thermometers = cbind(0.3, 9, 4.1/9), fg = 2, xlab = NA, ylab = NA, inches = 2.5, axes = F)
However, when I try to use this in Shiny, nothing is displayed on the page. Here's my code:
server = function(input, output, session) {
... (not needed for this plot)
}
ui = fluidPage(
tags$div(id="thermometer", style = "height:600px;", symbols(0, thermometers = cbind(0.3, 9, 4.1/9), fg = 2, xlab = NA, ylab = NA, inches = 2.5, axes = F))
)
shinyApp(ui = ui, server = server)
Inspecting the page shows that the div is being created, but the thermometer is not there. Any suggestions?
回答1:
In order to make a plot appear in Shiny, you need to create an output serverside and then render it in the ui:
server = function(input, output, session) {
#... (not needed for this plot)
output$thermometer <- renderPlot({
symbols(0, thermometers = cbind(0.3, 9, 4.1/9), fg = 2, xlab = NA, ylab = NA, inches = 2.5)
})
}
ui = fluidPage(
tags$div(id="thermometer", style = "height:600px;", plotOutput("thermometer"))
)
shinyApp(ui = ui, server = server)
EDIT: based on your comment an alternative way of plotting might be:
library(shiny)
server = function(input, output, session) {
#... (not needed for this plot)
output$thermometer <- renderPlot({
symbols(0, thermometers = cbind(0.3, 1, 4.1/9), fg = 2, xlab = NA, ylab = NA, inches = 2.5, yaxt='n', xaxt='n', bty='n')
})
}
ui = fluidPage(
tags$div(id="thermometer", style = "height:600px;width:200px;margin:auto", plotOutput("thermometer"))
)
shinyApp(ui = ui, server = server)
This removes the axes and box around the thermometer and makes it slightly more visible.
来源:https://stackoverflow.com/questions/45615819/thermometer-symbol-in-shiny