My dataset as follows....
fund , sharpe , risk
abc , 1.5 , 7
def , 0 , 5
selectInput("n_breaks", label = "Risk Profile:", choices = c(1,2,3,4,5,6,7,8,9,10), selected = 7)
# Reactive
selectedData <- reactive
a <- mydata %>% filter(risk==as.numeric(input$n_breaks) & sharpe > 0)
renderPlot
ggplot(selectedData(), aes(x = sharpe, y = returns, tooltip = fund, data_id = fund, color=sd)) + geom_point_interactive(size=1)
I am trying to run the below code at renderplot and shiny fails it. Please advise
ggiraph(code = {print(gg_point_3)}, tooltip_offx = 20, tooltip_offy = -10 )
This is an example using the iris dataset.
library(shiny)
library(dplyr)
library(ggplot2)
library(ggiraph)
ui <- shinyUI(fluidPage(
titlePanel("Shiny & ggiraph"),
sidebarLayout(
sidebarPanel(
selectInput("species",
"Select species:",
selected = "setosa",
choices = unique(levels(iris$Species))
)
),
mainPanel(
ggiraphOutput("plotIris")
)
)
))
server <- shinyServer(function(input, output) {
filterIris <- reactive({
filter(iris, Species == input$species)
})
output$plotIris <- renderggiraph({
gg <- ggplot(filterIris(), aes(x = Sepal.Length, y = Petal.Length))
gg <- gg + geom_point_interactive(
aes(tooltip = filterIris()$Sepal.Length), size = 2)
ggiraph(code = print(gg))
})
})
shinyApp(ui = ui, server = server)
Related
I'm new in programming language especially R.
I have data frame and want to show top 3 of my data and sort from the biggest value using bar chart. I have tried some codes but failed to create proper chart. Here is my latest code :
library(shiny)
library(plotly)
my_data <- data.frame(x1 = c("a","b", "c","d","e","f","g","h"),
x2 = c(200, 200, 100,200,200,100,200,100),
x3 = c(100,400,500,50,100,300,100,50))
df1 <- my_data[order(my_data$x3),] #order by x3 value, to create rank
ui <- tabPanel("Test",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "why",
label = "1. Select",
choices = df1$x2),
),
mainPanel(plotlyOutput("test"))
))
server <- function(input, output, session) {
output$test <- renderPlotly({
df2 <- df1 %>%
filter(x2 ==input$why) #filter by x2
p <-ggplot(df2,
aes(x = x1, y=x3)) +
geom_bar(stat = "identity")
fig <- ggplotly(p)
fig
})}
shinyApp(ui = ui, server = server)
the bar chart I created was not ordered correctly (based on x3 values), and I also only want to show top 3 of my data
To filter for the top 3 rows you could use dplyr::slice_max and to reorder your bars use e.g. reorder. Simply reordering the dataset will not work.
library(shiny)
library(dplyr)
library(plotly)
ui <- tabPanel(
"Test",
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "why",
label = "1. Select",
choices = unique(df1$x2),
selected = 200
),
),
mainPanel(plotlyOutput("test"))
)
)
server <- function(input, output, session) {
output$test <- renderPlotly({
df2 <- df1 %>%
filter(x2 == input$why) %>%
slice_max(x3, n = 3, with_ties = FALSE)
p <- ggplot(
df2,
aes(x = reorder(x1, -x3), y = x3)
) +
geom_bar(stat = "identity")
fig <- ggplotly(p)
fig
})
}
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:8022
I know the question is already answered, but I encourage you to keep your server function as small as possible and try to wrap long series of code into functions.
Here is an example of what I mean
library(tidyverse)
library(shiny)
library(plotly)
my_data <- data.frame(x1 = c("a","b", "c","d","e","f","g","h"),
x2 = c(200, 200, 100,200,200,100,200,100),
x3 = c(100,400,500,50,100,300,100,50))
df1 <- my_data[order(my_data$x3),] #order by x3 value, to create rank
myPlot <- function(data, input) {
df <- data |>
filter(x2 == input) #filter by x2
p <-ggplot(df, aes(x = reorder(x1, -x3), y=x3)) +
geom_bar(stat = "identity")
return(ggplotly(p))
}
ui <- tabPanel("Test",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "why",
label = "1. Select",
choices = df1$x2),
),
mainPanel(plotlyOutput("test"))
))
server <- function(input, output, session) {
output$test <- renderPlotly({
myPlot(df1, input$why)
})
}
shinyApp(ui = ui, server = server)
The ggplot just shows a vertical line of values that doesn't change when I try changing the x axis selection. The only thing on the x axis is the word "column" when I try to change the x axis, instead of the values of df$column according to what's selected.
df_variable <- df
df_colnames <- colnames(df)
xaxis_input <- selectInput(
inputId = "xaxis",
label = "Feature of Interest",
choices = df_colnames,
selected = df_colnames['default']
)
ui <- fluidPage(
titlePanel("DF"),
xaxis_input,
plotOutput(
outputId = "df_plot",
)
)
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes(x = input$xaxis, y = some_other_col))
return(plot)
})
}
input$xaxis is a string, so you cannot use it directly inside aes().
Try using aes_string() instead.
Note that some_other_col should also be a string.
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes_string(x = input$xaxis, y = "some_other_col"))
return(plot)
})
A full working example:
library(shiny)
library(ggplot2)
df <- iris
df_colnames <- colnames(df)
xaxis_input <- selectInput(
inputId = "xaxis",
label = "Feature of Interest",
choices = df_colnames
)
ui <- fluidPage(
titlePanel("DF"),
xaxis_input,
plotOutput(
outputId = "df_plot",
)
)
server <- function(input, output) {
output$df_plot <- renderPlot({
plot <- ggplot(data = df) +
geom_point(aes_string(x = input$xaxis, y = "Sepal.Width"))
return(plot)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have the following shiny app, which consists of a numeric input and as outputs two ggplot-graphics.
library(shiny)
n <- 100
dat <- data.frame(var1 = round(rnorm(n, 50, 10),0),
var2 = sample(c("A", "B"), n, replace = TRUE))
# USER INTERFACE
ui <- fluidPage(
titlePanel("My Sample App"),
sidebarLayout(
sidebarPanel(
numericInput("n", "Number of cases", value=100)
),
mainPanel(
plotOutput("boxplot"),
plotOutput("distribution")
)
)
)
# SERVER
server <- function(input, output) {
output$boxplot <- renderPlot({
ggplot(data = dat, aes(x = var2, y = var1)) + geom_boxplot() + ggtitle("Boxplot")
})
output$distribution <- renderPlot({
ggplot(data = dat, aes(var1)) + geom_histogram() + ggtitle("Histogram")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I've been trying to replace n = 10 with n = input$n. However it didn't work and I am unsure, where exactly I have to define the data.frame (inside the server function?). Can someone help please?
input$n is a reactive variable that can only be used in a reactive context. You can only define a reactive context in the server function, e.g. using reactive. Have a look here for an explanation.
library(shiny)
library(ggplot2)
# USER INTERFACE
ui <- fluidPage(
titlePanel("My Sample App"),
sidebarLayout(
sidebarPanel(
numericInput("n", "Number of cases", value=100)
),
mainPanel(
plotOutput("boxplot"),
plotOutput("distribution")
)
)
)
# SERVER
server <- function(input, output) {
dat <- reactive({
data.frame(var1 = round(rnorm(input$n, 50, 10),0),
var2 = sample(c("A", "B"), input$n, replace = TRUE))
})
output$boxplot <- renderPlot({
ggplot(data = dat(), aes(x = var2, y = var1)) + geom_boxplot() + ggtitle("Boxplot")
})
output$distribution <- renderPlot({
ggplot(data = dat(), aes(var1)) + geom_histogram() + ggtitle("Histogram")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am trying to buil a shiny app that returns a graph based on triplet (variable, min, max) selected by the user. I probably miss something in the war shint and dplyr evaluate statements, but my instinctive way of doing this does not work. A condition on the type : filter(iris, input$variable >= input$range[1] & input$variable <= input$range[2] leads no an empty dataset.
Here is an example with the iris dataset:
library(shiny)
library(tidyverse)
ui <- fluidPage(
titlePanel("Filter example"),
sidebarLayout(
sidebarPanel(
sliderInput("dimension",
"Dimension:",
min = 1,
max = 8,
value = c(1,8)),
selectInput("Petal or Sepal", "type",
c("Sepal Length" = "Sepal.Length",
"Petal Length" = "Petal.length"))
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
plotdata <- reactive({
filter(iris, input$type <= input$dimension[1] & input$type >= input$dimension[2])
})
output$distPlot <- renderPlot({
MyPlot <- ggplot(data = plotdata(),
mapping = aes(x = Sepal.Width, fill = Species))
Myplot + geom_histogram()
})
}
shinyApp(ui = ui, server = server)
The issue is obviously in the
plotdata <- reactive({
filter(iris, input$type <= input$dimension[1] & input$type >= input$dimension[2])
})
statement, but after hours of trawling though StackOverflow, I could not find a solution.
Thans for your help
There are some parameter mismatches and typos in the example code. Your filtering implementation also has issues. The following should work:
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
titlePanel("Filter example"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "dimension",
label = "Dimension:",
min = 1,
max = 8,
value = c(1,8)),
selectInput(
inputId = "type",
label = "Petal or Sepal",
choices = c(
"Sepal Length" = "Sepal.Length",
"Petal Length" = "Petal.Length"
)
)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
plotdata <- reactive({
iris[(iris[ , input$type] >= input$dimension[1] & iris[ , input$type] <= input$dimension[2]), ]
})
output$distPlot <- renderPlot({
ggplot(
data = plotdata(),
mapping = aes(x = Sepal.Width, fill = Species)
) +
geom_histogram()
})
}
shinyApp(ui = ui, server = server)
I am new to R and Shiny package. I have a csv file with 4 col and 600 rows and I am trying to plot some graphs using ggplot2.
My ui and server codes are like:
dt<-read.csv('file.csv')
server <- function(input, output) {
output$aPlot <- renderPlot({
ggplot(data = dt, aes(x = Col1, y = Col2, group = 'Col3', color = 'Col4')) + geom_point()
})
}
ui <- fluidPage(sidebarLayout(
sidebarPanel(
sliderInput("Obs", "Log FC", min = 1, max = 600, value = 100)
),
mainPanel(plotOutput("aPlot")) ))
Here, I can get the ggplot output but I don't know how to use this slider input to control the number of rows to be read i.e., I want this "Obs" input to define the size of Col1 to be used in the graph.
Try something like this, example here is with mtcars dataset:
library(shiny)
library(ggplot2)
dt <- mtcars[,1:4]
ui <- fluidPage(
sidebarPanel(
sliderInput("Obs", "Log FC", min = 1, max = nrow(dt), value = nrow(dt)-10)
),
mainPanel(plotOutput("aPlot"))
)
server <- function(input, output) {
mydata <- reactive({
dt[1:as.numeric(input$Obs),]
})
output$aPlot <- renderPlot({
test <- mydata()
ggplot(data = test, aes(x = test[,1], y = test[,2], group = names(test)[3], color = names(test)[4])) + geom_point()
})
}
shinyApp(ui = ui, server = server)
Change your server to:
server <- function(input, output) {
observe({
dt_plot <- dt[1:input$Obs,]
output$aPlot <- renderPlot({
ggplot(data = dt_plot, aes(x = Col1, y = Col2, group = 'Col3', color = 'Col4')) + geom_point()
})
})
}