ggplot not showing - area is blank - r

I am relatively new to ggplot2 and I have created a plot based on a csv file.
The problem I am having is that the plot is blank:
Nothing shows expect the title and I am not sure why!
As you can see the data is fine within RStudio:
My csv file is in the same directory as my app.R file:
My working directory is the same folder:
So why is ggplot2 failing to pick up the data held within the csv file?
Here is my code:
library(shiny)
library(ggplot2)
ui <- (fluidPage(
titlePanel("Pig Breeding")
)
)
mainPanel(
plotOutput(outputId = "scatterplot")
)
server <- (function(input, output){
output$scatterplot <- renderPlot({
pig_plot <- ggplot(read.csv("pigs_data.csv"),
aes_string(x = "species", y = "sow_count") +
geom_point())
})
})
shinyApp(ui, server)

the below code works with a test data called iris. Your mistake lies in your
ui <- (fluidPage(
titlePanel("Pig Breeding")
)
)
mainPanel(
plotOutput(outputId = "scatterplot")
)
ui code. You have left your mainPanel out of the fluidpage parenthesis. Thus, it does not read the plotOutput.
Example
library(shiny)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Iris data"),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
# Define server logic
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(iris, aes(x=Sepal.Width, y=Petal.Width)) + geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)
The code below should work for your question.
library(shiny)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Pig Breeding"),
# Show a plot of the generated distribution
mainPanel(
plotOutput("scatterplot")
)
)
# Define server logic
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(read.csv("pigs_data.csv"),
aes(x = "species", y = "sow_count")) + geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

Display text in shiny app using ggplotly()

Is there a way to display text in shiny app through ggplotly()?I want to pass some features that require ggplotly() later and I would like to know if this is possible.
library(plotly)
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("HTML"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
textOutput("testHTML"),
plotlyOutput("PLOTLYTEXT")
)
)
))
server <- shinyServer(function(input, output) {
output$testHTML <- renderText({
"text"
})
output$PLOTLYTEXT<-renderPlotly({
p<-renderText({
"text"
})
ggplotly(p)
})
})
shinyApp(ui = ui, server = server)
As I mentioned in my comment one option to display text via ggplotly would be to create a ggplot using e.g. a geom_text to display your text which could then be converted to a plotly object:
library(plotly)
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("HTML"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
textOutput("testHTML"),
plotlyOutput("PLOTLYTEXT")
)
)
))
server <- shinyServer(function(input, output) {
output$testHTML <- renderText({
"text"
})
output$PLOTLYTEXT<-renderPlotly({
p <- ggplot(data.frame(x = 1, y = 1, label = "text")) +
geom_text(aes(x = x, y = y, label = label)) +
theme_void()
ggplotly(p)
})
})
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:8289

Scatter plots for different variables in a single shiny app

I use R shiny to create multiple tabs for the scatter plot of different variables in a single app. My code seems alright, but the error says I have not "defined my main panel."
Can someone please help where I have gone wrong, or if my whole approach is inaccurateenter code here, please let me know!
library(shiny)
library(tidyverse)
India <- read.csv("D:/R/Practice 3/Indiadata.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Deaths vs all variables "),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("Deaths", "All variables:",
choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
"people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
"total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
)
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(plotOutput("plot1")),
tabPanel(plotOutput("plot2")),
tabPanel(plotOutput("plot3")),
tabPanel(plotOutput("plot4")),
tabPanel(plotOutput("plot5")),
tabPanel(plotOutput("plot6")),
tabPanel(plotOutput("plot7"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_cases))
})
output$plot2 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_vaccinations))
})
output$plot3 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=people_vaccinated))
})
output$plot4 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=people_fully_vaccinated))
})
output$plot5 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=total_boosters))
})
output$plot6 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=new_vaccinations))
})
output$plot7 <- renderPlot({
ggplot(India,aes(y=total_deaths,x=median_age))
})
}
# Run the application
shinyApp(ui = ui, server = server)
mainPanel is an argument of the sidebarLayout() function. So, you just need to move it up into the sidebarLayout() function:
ui <- fluidPage(
# Application title
titlePanel("Deaths vs all variables "),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("Deaths", "All variables:",
choices = c("cases"="total_cases","vaccinations"="total_vaccinations",
"people vaccinated"="people_vaccinated","people fully vaccinated"="people_fully_vaccinated",
"total booster"="total_boosters","new vaccinations"="new_vaccinations", "median age"="median_age"))
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(plotOutput("plot1")),
tabPanel(plotOutput("plot2")),
tabPanel(plotOutput("plot3")),
tabPanel(plotOutput("plot4")),
tabPanel(plotOutput("plot5")),
tabPanel(plotOutput("plot6")),
tabPanel(plotOutput("plot7"))
)
)
)
)

R Shiny: source command prints TRUE on app

I am building a shiny application that should have different options for different user categories. Therefore, I have different subfiles where the inputs are definied and the dependent on the role the respective file is loaded via the source command.
It works, however, the source command always prints a TRUE on my shiny app. I can't get rid of it no matter what option of the source command I am trying.
Here a screenshot of the problem
And a minimal example: app.R
library(shiny)
library(ggplot2)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
source("sub.R", echo = FALSE, print.eval = FALSE),
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
})
}
shinyApp(ui = ui, server = server)
And the sourced file sub.R:
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 0.1,
max = 5,
value = 1)
Thanks for any help in advance
I found the fix here: add a [1] at the end of the source(.) command:
library(shiny)
library(ggplot2)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
source("~/stackoverflow/17150062/sub.R", echo = FALSE, print.eval = FALSE)[1]
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
ggplot(tibble(x), aes(x=x)) + geom_histogram( binwidth = input$bins)
})
}
shinyApp(ui = ui, server = server)

renderPlot outputs plot in View, not browser

My goal is to plot the output, a process map, onto the browser. The output plot continues to appear in the Viewer pane in RStudio.
All of the functionality works as expected and I'm able to call the appropriate data and generate a dynamic output based on the dropdown menu.
Any help would be much appreciated. Full code is below.
library(shiny)
library(bupaR)
library(pm4py)
library(reticulate)
library(processmapR)
ui <- fluidPage(
tags$h1("Process Mining"),
tags$p("The purpose of this application is to show the Process Map output"),
fluidRow(
selectInput("pm_type","Process Mining Type", c("absolute","relative"))),
fluidRow(
plotOutput("plot1"))
)
server <- function(input, output) {
selectedData <- reactive({input$pm_type})
output$plot1 <- renderPlot({
patients %>%
process_map(type = frequency(selectedData()))
})
}
shinyApp(ui = ui, server = server)
To get output of process_map in shiny you have to use GrViz functions from DiagrammeR.
library(shiny)
library(bupaR)
library(pm4py)
library(reticulate)
library(processmapR)
library(DiagrammeR)
ui <- fluidPage(
tags$h1("Process Mining"),
tags$p("The purpose of this application is to show the Process Map output"),
fluidRow(
selectInput("pm_type","Process Mining Type", c("absolute","relative"))),
fluidRow(
grVizOutput("plot1"))
)
server <- function(input, output) {
selectedData <- reactive({input$pm_type})
output$plot1 <- renderGrViz({
patients %>%
process_map(type = frequency(selectedData()))
})
}
shinyApp(ui = ui, server = server)

Using shiny to highlight specific bar in a bar chart

I am not sure if Im doing this the right way (I am open for suggestions!). However what I try to do if to create a Shiny app where i can pick a bar and then the bar should be highlighted in the graph.
For this example I use the titanic_train dataset.
I do:
library(shiny)
library(ggplot2)
library(titanic)
library(dplyr)
UI <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
selectInput("specific_bar", "Pick bar to highlight:",
choices = unique(titanic_train$Embarked))
),
mainPanel(
plotOutput("plot_nice")
)
)
)
Server <- function(input, output) {
filtered <- reactive({
titanic_train$Specific <- ifelse((titanic_train$Embarked == input$specific_bar), 1,0)
})
output$plot_nice <- renderPlot({
ggplot(filtered(), aes_string(x="Embarked", y="Survived", fill = "Specific")) +
geom_bar(stat = "identity")
})
}
shinyApp(ui = UI, server = Server)
Running this however gives me the following error:
ggplot2 doesn't know how to deal with data of class numeric
And the problem really seems to have to do with the filtered() reactive function. Any thoughts on what is going wrong here?
you have to ask for the data.frame object back in the reactive part,
what you were doing is getting a vector back instead of getting another column added to titanic_train.
this should fix it:
library(shiny)
library(ggplot2)
library(titanic)
library(dplyr)
UI <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
selectInput("specific_bar", "Pick bar to highlight:",
choices = unique(titanic_train$Embarked))
),
mainPanel(
plotOutput("plot_nice")
)
)
)
Server <- function(input, output) {
filtered <- reactive({
titanic_train$Specific <- ifelse((titanic_train$Embarked == input$specific_bar), 1,0)
return(titanic_train)
})
output$plot_nice <- renderPlot({
ggplot(filtered(), aes_string(x="Embarked", y="Survived", fill = "Specific")) +
geom_bar(stat = "identity")
})
}
shinyApp(ui = UI, server = Server)

Resources