I want to control the xlim using numericrangeInput. Initial values are set as c(NA,NA).
If I try to change the scale min and max values and before updating one of the values it throws error Missing values cannot be used where TRUE/FALSE values are required
Any suggestions to fix this error.
Below is the code
library(shiny)
library(tidyverse)
data <- faithful %>% mutate(eruptionTime=lubridate::now() + lubridate::dhours(cumsum(waiting)))
ui <- fluidPage(
numericRangeInput(inputId = "noui1", label = "Numeric Range Input:",
value = c(NA, NA)
),
plotOutput("plot9")
)
server <- function(input, output) {
output$plot9 <- renderPlot({
print(paste("What is the condition",!anyNA(input$noui1)))
iris %>%ggplot() + geom_point(aes(x=Sepal.Length, y=Petal.Length)) +
coord_cartesian(xlim=input$noui1)
})
}
shinyApp(ui = ui, server = server)
You can update the plot only after you input both the numbers in numericRangeInput.
Try this code -
library(shiny)
library(tidyverse)
data <- faithful %>% mutate(eruptionTime=lubridate::now() + lubridate::dhours(cumsum(waiting)))
ui <- fluidPage(
numericRangeInput(inputId = "noui1", label = "Numeric Range Input:",
value = c(NA, NA)
),
plotOutput("plot9")
)
server <- function(input, output) {
output$plot9 <- renderPlot({
plot <- iris %>%ggplot() + geom_point(aes(x=Sepal.Length, y=Petal.Length))
if(length(input$noui1) == 2)
plot <- plot + coord_cartesian(xlim=input$noui1)
plot
})
}
shinyApp(ui = ui, server = server)
Related
For the first time I really can't find this answer here already, so I hope you all can help me, I'm sure there is a pretty easy fix.
I am making a Shiny volcano plot with clickable points to give me a table with the data about that point. If I use a trans function (that I found here, thank you helpful stranger) within scale_y_continuous() in my plot, points in the scaled region are no longer clickable. How can I scale the axis this way and still be able to have the clickable points?
My code, with some fake data that has the same problem:
## Read in necessary libraries, function, and data
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
reverselog_trans <- function(base = exp(1)) {
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
trans_new(paste0("reverselog-", format(base)), trans, inv,
log_breaks(base = base),
domain = c(1e-100, Inf))
}
pretend_data <- tibble(data=1:5, estimate = runif(5, min = -1, max = 2), plot = c(1e-50, 2e-35, 5e-1, 1, 50))
# Define UI for application that draws a volcano plot
ui <- fluidPage(
# Application title
titlePanel("Pretend Plot"),
plotOutput("plot", click = "plot_click"),
tableOutput("data")
)
# Define server logic required to draw a volcano plot
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(data = pretend_data, aes(x=estimate, y=plot)) +
geom_vline(xintercept=c(-1, 1), linetype=3) +
geom_hline(yintercept=0.01, linetype=3) +
geom_point() +
scale_y_continuous(trans = reverselog_trans(10))
}, res = 96)
output$data <- renderTable({
req(input$plot_click)
nearPoints(pretend_data, input$plot_click)
})
}
# Run the application
shinyApp(ui = ui, server = server)
The problem is that input$plot_click returns the coordinates on the transformed scale. nearPoints tries then to match those to the original scale which does not work.
You have a couple of options though:
Transform the data yourself and adapt y axis ticks via scale_y_continuous
Adapt pretend_data in the nearPoints call.
Option 1
This requires that you control y axis tick marks yourself and would need some more fiddling to get the exact same reuslts as in your example.
pretend_data_traf <- pretend_data %>%
mutate(plot = reverselog_trans(10)$transform(plot))
# Define UI for application that draws a volcano plot
ui <- fluidPage(
# Application title
titlePanel("Pretend Plot"),
plotOutput("plot", click = "plot_click"),
tableOutput("data")
)
# Define server logic required to draw a volcano plot
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(data = pretend_data_traf, aes(x=estimate, y=plot)) +
geom_vline(xintercept=c(-1, 1), linetype=3) +
geom_hline(yintercept=0.01, linetype=3) +
geom_point() +
## would need to define breaks = to get same tick mark positions
scale_y_continuous(labels = reverselog_trans(10)$inverse)
}, res = 96)
output$data <- renderTable({
req(input$plot_click)
nearPoints(pretend_data_traf, input$plot_click)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Option 2
pretend_data_traf <- pretend_data %>%
mutate(plot = reverselog_trans(10)$transform(plot))
# Define UI for application that draws a volcano plot
ui <- fluidPage(
# Application title
titlePanel("Pretend Plot"),
plotOutput("plot", click = "plot_click"),
tableOutput("data")
)
# Define server logic required to draw a volcano plot
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(data = pretend_data, aes(x=estimate, y=plot)) +
geom_vline(xintercept=c(-1, 1), linetype=3) +
geom_hline(yintercept=0.01, linetype=3) +
geom_point() +
scale_y_continuous(trans = reverselog_trans(10))
}, res = 96)
output$data <- renderTable({
req(input$plot_click)
nearPoints(pretend_data_traf, input$plot_click) %>%
mutate(plot = reverselog_trans(10)$inverse(plot))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Good day
I am trying to plot the means and 95% confidence intervals for my shiny webpage but I can't seem to get it right.
I would like output similar to this
I have tried two methods
Using geom_errorbar
Here I tried creating a summary table that calculates the 95% CI and then plotting from there.
My code follows
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(Data[,3:(ncol(Data)-1)]))
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
ci <- reactive({
groupwiseMean(input$question ~ Date,
data = Data,
conf = 0.95,
digits = 3)
})
output$meanCI <- renderPlot(
ggplot(ci, aes(x=Date, y=Mean)) +
geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
geom_point()
)
}
shinyApp(ui = ui, server = server)
But it gives me this error,
data must be a data frame, or other object coercible by fortify(), not an S3 object with class reactiveExpr/reactive
Option 2 was to use plotmeans from the gplot package
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(Data[,3:(ncol(Data)-1)]))
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
output$meanCI <- renderPlot(
plotmeans(input$question~Data$Date, connect = FALSE)
)
}
shinyApp(ui = ui, server = server)
But it results is this error,
variable lengths differ (found for 'Data$Date')
Any help will be greatly appreciated!
library(shiny)
library(rcompanion)
library(ggplot2)
ui <- fluidPage(
titlePanel("questionnaire"),
sidebarLayout(
sidebarPanel(
selectInput("question", "Choose a question",
colnames(iris)[1:4])
),
mainPanel(
plotOutput("meanCI")
)
)
)
server <- function(input, output) {
ci <- reactive({
groupwiseMean(data = iris,
var = input[["question"]],
group = "Species",
conf = 0.95,
digits = 3)
})
output[["meanCI"]] <- renderPlot({
ggplot(ci(), aes(x=Species, y=Mean)) +
geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
geom_point()
})
}
shinyApp(ui = ui, server = server)
Your main error is the missing parentheses in ggplot(ci(), ....... The other one is input$question ~ Date, which doesn't work because input$question is a character string.
I am getting an error with the plotting index using plotly in conjunction with reactive values in shiny. The sidebar panel loads with no issues but there is a problem displaying the chart that I cannot determine. Any help solving the index problem would be much appreciated. Thanks!
library(shiny)
library(plotly)
data(economics, package = "ggplot2")
nms <- names(economics)
ui <- fluidPage(
headerPanel("TEST"),
sidebarPanel(
selectInput('x', 'X', choices = nms, selected = nms[[1]]),
selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
sliderInput('plotHeight', 'Height of plot (in pixels)',
min = 100, max = 2000, value = 1000)
),
mainPanel(
plotlyOutput('trendPlot', height = "900px")
)
)
server <- function(input, output) {
#add reactive data information. Dataset = built in diamonds data
dataset <- reactive({economics[, c(input$xcol, input$ycol)]
})
output$trendPlot <- renderPlotly({
# build graph with ggplot syntax
p <- ggplot(dataset(), aes_string(x = input$x, y = input$y)) +
geom_line()
ggplotly(p) %>%
layout(height = input$plotHeight, autosize=TRUE)
})
}
shinyApp(ui, server)
Warning: Error in : Unsupported index type: NULL
You have mistakenly used xcol and ycol not sure why. Without those names the code works fine.
library(shiny)
library(plotly)
library(tidyverse)
data(economics, package = "ggplot2")
nms <- names(economics)
ui <- fluidPage(
headerPanel("TEST"),
sidebarPanel(
selectInput('x', 'X', choices = nms, selected = nms[[1]]),
selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
sliderInput('plotHeight', 'Height of plot (in pixels)',
min = 100, max = 2000, value = 1000)
),
mainPanel(
plotlyOutput('trendPlot', height = "900px")
)
)
server <- function(input, output) {
#add reactive data information. Dataset = built in diamonds data
dataset <- reactive({
economics[, c(input$x, input$y)]
})
output$trendPlot <- renderPlotly({
# build graph with ggplot syntax
p <- ggplot(dataset(), aes_string(input$x, input$y)) +
geom_line()
ggplotly(p, height = input$plotHeight)
})
}
shinyApp(ui, 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()
})
})
}
When the shiny app below is run I initially get the error - invalid type/length (symbol/0) in vector allocation. However, as soon as I click "Submit" the app functions as intended.
Is there a way to avoid this launch error and have it work correctly from the start?
plot_and_summary <- function(dat, col){
summary <- dat %>% summarize_(mean = interp(~mean(x), x = as.name(col)),
sd = interp(~sd(x), x = as.name(col)))
plot <- ggplot(dat, aes_string(x = col)) + geom_histogram()
return(list(summary = summary, plot = plot))
}
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
uiOutput("column_select"),
submitButton("Submit")
),
mainPanel(
tableOutput("summary"),
plotOutput("plot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output){
dat <- reactive({iris})
output$column_select <- renderUI({selectInput("col", label = "select column", choices = as.list(names(dat())))})
pas <- reactive({plot_and_summary(dat(), input$col)})
output$plot <- renderPlot({pas()$plot})
output$summary <- renderTable({pas()$summary})
}
shinyApp(ui = ui, server = server)
The req function should solve your problem
http://shiny.rstudio.com/reference/shiny/latest/req.html
pas <- reactive({plot_and_summary(dat(), req(input$col))})