How to plot different graphs according to user input? R shiny












1















I am trying to build a web app in shiny that would allow for different user input and then plot graphs/output data tables accordingly. I am using WHO's data about suicide rates and I want to make a few plots possible:
1. Histogram, y=suicide_no, x=age group, gender neutral (female and male combined) sample=world or one of the countries (if one country is selected create a subset of data) . So far I renderred this separately because there is no need to specify the country when I want the global data, otherwise I would've used input$country as the fill
2. Histogram y=suicide_no, x=age group, gender specific (female and male separately, two bars for each age group), sample=world or one of the countries. I renderred this separately to gender neutral as for gender specific I include fill=gender.
3. Line graph y=suicide_no, x=years, gender neutral, sample=world or one/several countries
4. Line graph y=suicide_no, x=years, gender specific, sample=world or one country only (two lines which represent male and female)



Here is my code:



library(shiny)
library(ggplot2)
setwd("C:\Users\Lenovoi7\Shrewsbury School\IT\Coursework")
who<-data.frame(read.csv("who.csv", stringsAsFactors = TRUE))
countries <- sort(unique(who$country))
countries<-union(countries, c("World"))

ui<-fluidPage(

sidebarLayout(

sidebarPanel(

#Allow user to choose the variable which should be plotted on the x axis
selectInput(
"x", "Please choose the x variable",
c("",
"Year" = "year",
"Age group" = "age")),

#only show this panel when the x variable is chosen
conditionalPanel(
condition = "input.x == 'age' || input.x == 'year'",
selectInput(
inputId = "gender",
label = "Please specify the gender characteristics",
choices = c("", "Gender neutral" = "gender_neutral",
"Gender specific" = "gender_specific"),
selected = NULL),

#only show this panel if the input is gender_specific
conditionalPanel(
condition = "input.gender == 'gender_specific'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")),

conditionalPanel(
condition = "input.gender == 'gender_neutral'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")))),

mainPanel(
conditionalPanel(
condition = 'input.x=="age" & input.gender=="gender_neutral" & input.country=="world"',
plotOutput("hgnw")),

conditionalPanel(
#any one country
condition = 'input.x=="year" & input.gender=="gender_neutral"& input.country==input$country',

plotOutput("lgnw")
))))

server <- function(input, output) {

#render the plot when x=age, gender=gender_neutral, sample=world
output$hgnw<-renderPlot({
validate(need(input$x=="age" & input$gender=="gender_neutral"))
ggplot(data=who, aes(x=age)) + geom_bar(aes(weights=suicides_no))

})

#render the plot when x=year, gender=gender_neutral, sample=world
output$lgnw <- renderPlot({
validate(need(input$x=="year", message=FALSE))

ggplot(data=who, aes(x=year)) + geom_line(aes(y=suicides_no)) + geom_point(aes(y=suicides_no))
})

}
shinyApp(ui = ui, server = server)


I have created a few (not all) conditional panels and plot outputs and then tried to test whether the plot calculated in the server works when called in the main panel (different plot is rendered depending on the user input which I tried to accomplish with validate function) but it does not. I am wondering how do I approach this problem in the server and main panel. Do I use if statements or conditional panels? This does not currently work and it may not even be a good approach for my problem. Any help is greatly appreciated.










share|improve this question







New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

    – A. S. K.
    Jan 18 at 17:32











  • I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

    – Nejla Hidić
    Jan 20 at 15:06













  • Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

    – A. S. K.
    2 days ago
















1















I am trying to build a web app in shiny that would allow for different user input and then plot graphs/output data tables accordingly. I am using WHO's data about suicide rates and I want to make a few plots possible:
1. Histogram, y=suicide_no, x=age group, gender neutral (female and male combined) sample=world or one of the countries (if one country is selected create a subset of data) . So far I renderred this separately because there is no need to specify the country when I want the global data, otherwise I would've used input$country as the fill
2. Histogram y=suicide_no, x=age group, gender specific (female and male separately, two bars for each age group), sample=world or one of the countries. I renderred this separately to gender neutral as for gender specific I include fill=gender.
3. Line graph y=suicide_no, x=years, gender neutral, sample=world or one/several countries
4. Line graph y=suicide_no, x=years, gender specific, sample=world or one country only (two lines which represent male and female)



Here is my code:



library(shiny)
library(ggplot2)
setwd("C:\Users\Lenovoi7\Shrewsbury School\IT\Coursework")
who<-data.frame(read.csv("who.csv", stringsAsFactors = TRUE))
countries <- sort(unique(who$country))
countries<-union(countries, c("World"))

ui<-fluidPage(

sidebarLayout(

sidebarPanel(

#Allow user to choose the variable which should be plotted on the x axis
selectInput(
"x", "Please choose the x variable",
c("",
"Year" = "year",
"Age group" = "age")),

#only show this panel when the x variable is chosen
conditionalPanel(
condition = "input.x == 'age' || input.x == 'year'",
selectInput(
inputId = "gender",
label = "Please specify the gender characteristics",
choices = c("", "Gender neutral" = "gender_neutral",
"Gender specific" = "gender_specific"),
selected = NULL),

#only show this panel if the input is gender_specific
conditionalPanel(
condition = "input.gender == 'gender_specific'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")),

conditionalPanel(
condition = "input.gender == 'gender_neutral'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")))),

mainPanel(
conditionalPanel(
condition = 'input.x=="age" & input.gender=="gender_neutral" & input.country=="world"',
plotOutput("hgnw")),

conditionalPanel(
#any one country
condition = 'input.x=="year" & input.gender=="gender_neutral"& input.country==input$country',

plotOutput("lgnw")
))))

server <- function(input, output) {

#render the plot when x=age, gender=gender_neutral, sample=world
output$hgnw<-renderPlot({
validate(need(input$x=="age" & input$gender=="gender_neutral"))
ggplot(data=who, aes(x=age)) + geom_bar(aes(weights=suicides_no))

})

#render the plot when x=year, gender=gender_neutral, sample=world
output$lgnw <- renderPlot({
validate(need(input$x=="year", message=FALSE))

ggplot(data=who, aes(x=year)) + geom_line(aes(y=suicides_no)) + geom_point(aes(y=suicides_no))
})

}
shinyApp(ui = ui, server = server)


I have created a few (not all) conditional panels and plot outputs and then tried to test whether the plot calculated in the server works when called in the main panel (different plot is rendered depending on the user input which I tried to accomplish with validate function) but it does not. I am wondering how do I approach this problem in the server and main panel. Do I use if statements or conditional panels? This does not currently work and it may not even be a good approach for my problem. Any help is greatly appreciated.










share|improve this question







New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

    – A. S. K.
    Jan 18 at 17:32











  • I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

    – Nejla Hidić
    Jan 20 at 15:06













  • Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

    – A. S. K.
    2 days ago














1












1








1








I am trying to build a web app in shiny that would allow for different user input and then plot graphs/output data tables accordingly. I am using WHO's data about suicide rates and I want to make a few plots possible:
1. Histogram, y=suicide_no, x=age group, gender neutral (female and male combined) sample=world or one of the countries (if one country is selected create a subset of data) . So far I renderred this separately because there is no need to specify the country when I want the global data, otherwise I would've used input$country as the fill
2. Histogram y=suicide_no, x=age group, gender specific (female and male separately, two bars for each age group), sample=world or one of the countries. I renderred this separately to gender neutral as for gender specific I include fill=gender.
3. Line graph y=suicide_no, x=years, gender neutral, sample=world or one/several countries
4. Line graph y=suicide_no, x=years, gender specific, sample=world or one country only (two lines which represent male and female)



Here is my code:



library(shiny)
library(ggplot2)
setwd("C:\Users\Lenovoi7\Shrewsbury School\IT\Coursework")
who<-data.frame(read.csv("who.csv", stringsAsFactors = TRUE))
countries <- sort(unique(who$country))
countries<-union(countries, c("World"))

ui<-fluidPage(

sidebarLayout(

sidebarPanel(

#Allow user to choose the variable which should be plotted on the x axis
selectInput(
"x", "Please choose the x variable",
c("",
"Year" = "year",
"Age group" = "age")),

#only show this panel when the x variable is chosen
conditionalPanel(
condition = "input.x == 'age' || input.x == 'year'",
selectInput(
inputId = "gender",
label = "Please specify the gender characteristics",
choices = c("", "Gender neutral" = "gender_neutral",
"Gender specific" = "gender_specific"),
selected = NULL),

#only show this panel if the input is gender_specific
conditionalPanel(
condition = "input.gender == 'gender_specific'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")),

conditionalPanel(
condition = "input.gender == 'gender_neutral'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")))),

mainPanel(
conditionalPanel(
condition = 'input.x=="age" & input.gender=="gender_neutral" & input.country=="world"',
plotOutput("hgnw")),

conditionalPanel(
#any one country
condition = 'input.x=="year" & input.gender=="gender_neutral"& input.country==input$country',

plotOutput("lgnw")
))))

server <- function(input, output) {

#render the plot when x=age, gender=gender_neutral, sample=world
output$hgnw<-renderPlot({
validate(need(input$x=="age" & input$gender=="gender_neutral"))
ggplot(data=who, aes(x=age)) + geom_bar(aes(weights=suicides_no))

})

#render the plot when x=year, gender=gender_neutral, sample=world
output$lgnw <- renderPlot({
validate(need(input$x=="year", message=FALSE))

ggplot(data=who, aes(x=year)) + geom_line(aes(y=suicides_no)) + geom_point(aes(y=suicides_no))
})

}
shinyApp(ui = ui, server = server)


I have created a few (not all) conditional panels and plot outputs and then tried to test whether the plot calculated in the server works when called in the main panel (different plot is rendered depending on the user input which I tried to accomplish with validate function) but it does not. I am wondering how do I approach this problem in the server and main panel. Do I use if statements or conditional panels? This does not currently work and it may not even be a good approach for my problem. Any help is greatly appreciated.










share|improve this question







New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am trying to build a web app in shiny that would allow for different user input and then plot graphs/output data tables accordingly. I am using WHO's data about suicide rates and I want to make a few plots possible:
1. Histogram, y=suicide_no, x=age group, gender neutral (female and male combined) sample=world or one of the countries (if one country is selected create a subset of data) . So far I renderred this separately because there is no need to specify the country when I want the global data, otherwise I would've used input$country as the fill
2. Histogram y=suicide_no, x=age group, gender specific (female and male separately, two bars for each age group), sample=world or one of the countries. I renderred this separately to gender neutral as for gender specific I include fill=gender.
3. Line graph y=suicide_no, x=years, gender neutral, sample=world or one/several countries
4. Line graph y=suicide_no, x=years, gender specific, sample=world or one country only (two lines which represent male and female)



Here is my code:



library(shiny)
library(ggplot2)
setwd("C:\Users\Lenovoi7\Shrewsbury School\IT\Coursework")
who<-data.frame(read.csv("who.csv", stringsAsFactors = TRUE))
countries <- sort(unique(who$country))
countries<-union(countries, c("World"))

ui<-fluidPage(

sidebarLayout(

sidebarPanel(

#Allow user to choose the variable which should be plotted on the x axis
selectInput(
"x", "Please choose the x variable",
c("",
"Year" = "year",
"Age group" = "age")),

#only show this panel when the x variable is chosen
conditionalPanel(
condition = "input.x == 'age' || input.x == 'year'",
selectInput(
inputId = "gender",
label = "Please specify the gender characteristics",
choices = c("", "Gender neutral" = "gender_neutral",
"Gender specific" = "gender_specific"),
selected = NULL),

#only show this panel if the input is gender_specific
conditionalPanel(
condition = "input.gender == 'gender_specific'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")),

conditionalPanel(
condition = "input.gender == 'gender_neutral'",
selectInput(
inputId = "country",
label = "Select a country:",
choices = countries,
selected = "Bosnia and Herzegovina")))),

mainPanel(
conditionalPanel(
condition = 'input.x=="age" & input.gender=="gender_neutral" & input.country=="world"',
plotOutput("hgnw")),

conditionalPanel(
#any one country
condition = 'input.x=="year" & input.gender=="gender_neutral"& input.country==input$country',

plotOutput("lgnw")
))))

server <- function(input, output) {

#render the plot when x=age, gender=gender_neutral, sample=world
output$hgnw<-renderPlot({
validate(need(input$x=="age" & input$gender=="gender_neutral"))
ggplot(data=who, aes(x=age)) + geom_bar(aes(weights=suicides_no))

})

#render the plot when x=year, gender=gender_neutral, sample=world
output$lgnw <- renderPlot({
validate(need(input$x=="year", message=FALSE))

ggplot(data=who, aes(x=year)) + geom_line(aes(y=suicides_no)) + geom_point(aes(y=suicides_no))
})

}
shinyApp(ui = ui, server = server)


I have created a few (not all) conditional panels and plot outputs and then tried to test whether the plot calculated in the server works when called in the main panel (different plot is rendered depending on the user input which I tried to accomplish with validate function) but it does not. I am wondering how do I approach this problem in the server and main panel. Do I use if statements or conditional panels? This does not currently work and it may not even be a good approach for my problem. Any help is greatly appreciated.







r ggplot2 shiny






share|improve this question







New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 18 at 16:16









Nejla HidićNejla Hidić

142




142




New contributor




Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Nejla Hidić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

    – A. S. K.
    Jan 18 at 17:32











  • I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

    – Nejla Hidić
    Jan 20 at 15:06













  • Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

    – A. S. K.
    2 days ago














  • 1





    I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

    – A. S. K.
    Jan 18 at 17:32











  • I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

    – Nejla Hidić
    Jan 20 at 15:06













  • Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

    – A. S. K.
    2 days ago








1




1





I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

– A. S. K.
Jan 18 at 17:32





I would try doing this with a single plotOutput and no conditionalPanels. Inside renderPlot for your one plotOutput, you can use a series of if statements to determine what the user selected and therefore what ggplot commands you need to issue.

– A. S. K.
Jan 18 at 17:32













I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

– Nejla Hidić
Jan 20 at 15:06







I tried to do it like this: server <- function(input, output) { x<-reactive({input$x}) gender<-reactive({input$gender}) country<-reactive({input$country}) output$graph <- renderPlot( if (x()=="age"){ if (gender()=="gender_neutral"){ if (country()=="world"){ ggplot(data=who, aes(x=year)) + geom_bar(aes(weights=suicides_no), position="dodge") } } } ) } But it still does not output anything when I choose age group, gender neutral and world?

– Nejla Hidić
Jan 20 at 15:06















Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

– A. S. K.
2 days ago





Could it be that the list of options coming from the countries vector contains World (capitalized) but the condition in the server looks for world (lowercase)? I can't run the full code without access to who.csv; that's the only thing I can see at the moment.

– A. S. K.
2 days ago












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Nejla Hidić is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257728%2fhow-to-plot-different-graphs-according-to-user-input-r-shiny%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Nejla Hidić is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Nejla Hidić is a new contributor. Be nice, and check out our Code of Conduct.













Nejla Hidić is a new contributor. Be nice, and check out our Code of Conduct.












Nejla Hidić is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257728%2fhow-to-plot-different-graphs-according-to-user-input-r-shiny%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre