I am unable to subset a dataframe within a reactive shiny structure and display it as plot (using ggplot).
I want to have something with map like that, in my shiny app and of course to change the map with the "Choisir une votation"(refering to my input$objet)
here is my ui.r
library(shiny)
shinyUI(fluidPage(
titlePanel("Swiss votations tool"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("objet", "Choisir une votation",
levels((df.merge$Date.et.objet))
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("plot")
)
)
))
and my server.r (i think i'm doing something with the reactive wrong and/or stupid here...)
shinyServer(function(input, output) {
df.merge2<-reactive({
df.merge2<-df.merge[input$objet,]
d <- as.data.frame(df.merge2)
d
})
output$plot <- renderPlot({
df.merge2<-df.merge2()
p <- ggplot() +geom_polygon(data=df.merge2,
aes(fill =as.numeric(df.merge2$Pourcentage.de.oui),
x = df.merge2$long, y = df.merge2$lat, group = df.merge2$group))
p <- p+geom_path(data=df.merge2,
aes(fill=as.numeric(df.merge2$Pourcentage.de.oui),x =
df.merge2$long, y = df.merge2$lat, group = df.merge2$group),
color = "white", size = 0.1)
p <- p+scale_fill_gradient(low ="#ffffcc" , high = "#253494")
print(p)
})
})
Think you for help, I quite new to shiny
my data:
dataframe with
str(df.merge)
'data.frame': 156672 obs. of 26 variables:
and geographic informations
Related
So, I need to make a shiny app that takes a dynamic number of inputs to eventually do dimension reduction, but I'm stuck trying to figure out how to refer to what's in my inputs when I have a dynamic number of them. I'm using the iris dataset and the inputs are the variables. Part of what I need to do is plot 2 of them with a k means, but I'm just trying to 1st make a basic plot. What I have so far is
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel("Old Faithful Geyser Data"),
fluidRow(
column(2,
textInput(inputId = "number", label = "number of selectInput",value = 2)
),
column(8,
plotOutput("distPlot")),
column(2,
uiOutput(outputId = "putselect"))
)
))
server <- shinyServer(function(input, output) {
output$putselect = renderUI(
if(input$number != 0 ){
lapply(1:(input$number), function(i){
selectInput(inputId = paste0("var",i), label = paste0("input ",i), choices = names(iris))
})
}
)
output$distPlot <- renderPlot({
ggplot(iris, aes(x = input$var1, y = input$var2, color = Species)) +
geom_point()
})
})
shinyApp(ui = ui, server = server)
In my output$distplot what goes in the ggplot x and y? The way I have it now it shows up and the labels on the graph change, but there are no points on the graph. I'm new to using Shiny so any help would be appreciated.
instead of aes use aes_string like:
ggplot(iris, aes_string(x = input$var1,
y = input$var2,
color = "Species"
)
)
note to quote the variables supplied as a string (Species in this case)
see: Shiny: passing input$var to aes() in ggplot2
I have created a shiny app should take input from three sliders and:
Plots a distribution in ggplot
Show a summary table of values underneath the plot in #1 above
If I just want to plot the histogram (and I comment out the table data), I can get the code to work correctly. However, when I add the table, the plot disappears even though the plot header is still there. I have tried moving the commas a braces around to see if it's a simple syntax error but haven't had any luck.
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Test Shiny Layout"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
h4("Input Data"),
sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
),
# Show a plot of the generated distribution
mainPanel(
h4("Histogram"),
plotOutput("distPlot", width = "600", height = "600"),
h4("Table of Values"),
tableOutput("table")
)
)
))
Server
library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
bins <- input$bins
df1 <- (iris$Sepal.Length)
x <- mean(df1)
y <- sd(df1)
ggplot(data = iris) +
geom_histogram(mapping = aes(x = Sepal.Length), color = "blue", binwidth = "bins")
# Create an empty dataframe and then plug in the mean and standard deviation
results <- data.frame("0", "0")
results[1,1] = x
results[1,2] = y
colnames(results) <- c("Mean", "SD")
rownames(results) <- c("Sepal Length")
output$table <- renderTable(results)
})
})
Your renderTable() is inside your renderPlot() call. So renderPlot isn't returning anything.
You were right: it was a simple syntax error. But you also had several other issues in your code. At least a dozen. Three in binwidth = "bins" alone.
Here's a working version. I suspect you will still want to make tweaks, but at least you have both a histogram and a summary table that both look reasonably sensible.
library(shiny)
library(ggplot2)
data(iris)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(data = iris) +
geom_histogram(aes(x = Sepal.Length), color = "blue", bins = input$bins)
})
output$table <- renderTable({
iris %>%
summarise(Mean=mean(Sepal.Length),
SD=sd(Sepal.Length))
})
}
ui <- fluidPage(
titlePanel("Test Shiny Layout"),
sidebarLayout(
sidebarPanel(
h4("Input Data"),
sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
),
mainPanel(
h4("Histogram"),
plotOutput("distPlot", width = "600", height = "600"),
h4("Table of Values"),
tableOutput("table")
)
)
)
shinyApp(ui = ui, server = server)
I want to start a shiny app for practice where a use can choose from a dropdown the values in the "cut" column from the diamonds dataset (from ggplot2).
My ui looks as following:
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Reactive Boxplot"),
# Show a boxplot of the selected cut
mainPanel(
selectInput("column", label = h3("Column to plot"),
choices = c("", diamonds$cut),
selected = 1,
width='55%',
multiple = FALSE),
plotOutput("diamondshist")
)
)
)
I don't know how to define the input variables as the five distinct values in the "cut" column of diamonds dataset. Any input on this?
My server file looks like shared below. I assume I would also need to adapt the input data for the plot.
library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
compute_plot <- reactive({
if (input$column != ""){
ggplot(diamonds[, input$column])+
labs(title = "From diamonds dataset")+
geom_boxplot(aes(x = cut, y = price))+
scale_y_reverse()
}
})
output$diamondshist <- renderPlot({
compute_plot();
})
})
I assume this is what you are after:
pass the levels of diamonds$cut as input selection
subset the diamonds dataset to the selected cut
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui=shinyUI(fluidPage(
# Application title
titlePanel("Reactive Boxplot"),
# Show a boxplot of the selected cut
mainPanel(
selectInput("column", label = h3("Column to plot"),
choices = c("", levels(diamonds$cut)),
selected = NULL,
width='55%',
multiple = FALSE),
plotOutput("diamondshist")
)
)
)
# Define server logic required to draw a histogram
server=shinyServer(function(input, output) {
compute_plot <- reactive({
if (input$column != ""){
ggplot(subset(diamonds, cut==input$column))+
labs(title = "From diamonds dataset")+
geom_boxplot(aes(x = cut, y = price))+
scale_y_reverse()
}
})
output$diamondshist <- renderPlot({
compute_plot();
})
})
shinyApp(ui = ui, server = server)
I am trying to embed an RChart into a shiny app. Specifically, working with the function nPlot and the type=lineChart NVD3 feature. I am trying to plot 3 density curves for simulated Normal data. I am trying to achieve some of the functionality described in the example below:
http://nvd3.org/ghpages/line.html
My issues:
How to have different colours for different density curves?
How to have the feature to click on group variable (1), (2), (3) to effectively remove the selected density. Similar to clicking on "Sine Wave" (top right) in the example to remove the orange sine curve?
How to add x-axis and y-axis labels? My $params$xAxis= call does not work.
Below are my server.R and ui.R files:
## server.r
library(rCharts)
library(shiny)
x <-rnorm(1000,0,1)
y <-rnorm(1000,1,1)
z <-rnorm(1000,2,1)
out <- c(x,y,z)
grp <- c(rep(0,1000),rep(1,1000),rep(2,1000))
data <- as.data.frame(cbind(out,grp))
dens <- by(data$out, data$grp, density)
d <- unlist(c(dens[[1]][1][1], dens[[2]][1][1], dens[[3]][1][1]))
support <- unlist(c(dens[[1]][2][1], dens[[2]][2][1], dens[[3]][2][1]))
grpvar <- c(rep(0,length(unlist(dens[[1]][1][1]))), rep(1,length(unlist(dens[[2]][1][1]))), rep(2,length(unlist(dens[[3]][1][1]))))
dat <- as.data.frame(cbind(d,support,grpvar))
shinyServer(function(input, output) {
output$myChart <- renderChart({
p1 <- nPlot(support~d, group=grpvar, data = dat, type = "lineChart")
p1$addParams(dom = 'myChart')
p1$params$width = 600
p1$params$height = 400
p1$params$xAxis = "Support"
p1$params$yAxis = "Density"
p1$chart(tooltipContent = "#! function(key, x, y, e){
return '<b>Group</b>: ' + e.point.grpvar
} !#")
return(p1)
})
})
## ui.R
library(rCharts)
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("rCharts: Interactive Charts from R using NVD3.js"),
sidebarPanel(
wellPanel(
helpText( "Look at the pretty graph"
)
),
wellPanel(
helpText( "Look at the pretty graph"
)
),
wellPanel(
helpText( "Look at the pretty graph"
)
)
),
mainPanel(
div(class='wrapper',
tags$style(".Nvd3{ height: 600px;}"),
showOutput("myChart","Nvd3")
)
)
))
Thanks in advance for any help/advice you can provide.
You will just need to add quotes around grpvar, so
p1 <- nPlot(support~d, group="grpvar", data = dat, type = "lineChart")
I am a little confused about printing the dynamic output while using R in shiny. The following are my code in shiny, it can just print out the ggplot2 charts, but not along with the new data frame. I just wanna know how to print out "mydata" at the same time. Thanks.
library("reshape2")
library("ggplot2")
Server.R
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
#Draw gragh to compare the forecast values with real data
mydata <- data.frame(years, A, B)
df <- melt(mydata, id = 'years', variable = 'series')
print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))
})
Ui.R
shinyUI(pageWithSidebar(
# Sidebar with controls to select a dataset
# of observations to view
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("Netflix"))),
plotOutput(outputId = "main_plot", height = "500px")
))
Pull out the data frame into a reactive expression, so you can use it from two different outputs. (This is the reactive analog to introducing and reusing a variable.)
server.R
shinyServer(function(input, output) {
mydata <- reactive({
data.frame(years, A, B)
})
output$main_plot <- renderPlot({
#Draw gragh to compare the forecast values with real data
df <- melt(mydata(), id = 'years', variable = 'series')
print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))
})
output$data <- renderTable({ mydata() })
})
ui.R
shinyUI(pageWithSidebar(
# Sidebar with controls to select a dataset
# of observations to view
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("Netflix"))),
plotOutput(outputId = "main_plot", height = "500px"),
tableOutput(outputId = "data")
))