Mouseover in plotly and shiny - r

I have some plotly code that perfectly calls the row names of a dataframe on mouseover both within RStudio and on RPubs . . . but not when embedded in Shiny.
The basic code is:
require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)
plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "markers")
The Shiny version, however, is completely dead. What am I missing?
require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)
ui <- fluidPage(
titlePanel("Word Frequency Analysis for Meiji-era Authors"),
mainPanel(
plotOutput("plot"),
dataTableOutput("Print")
)
)
server <- function(input, output){
output$plot<-renderPlot({
p <- plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "text")
plot(p)
})
output$Print<-renderDataTable({Trial})
}
shinyApp(ui = ui, server = server)

You need to swap out some base shiny functions for their plotly counterparts. Namely plotOutput -> plotlyOutput and renderPlot -> renderPlotly. Also, that last plot(p) isn't what you want to return: you just want to return p (the plot object).
require(shiny)
require(plotly)
Trial <- read.table("http://history.emory.edu/RAVINA/Aozora/Data/Trial.txt", row.names = 1)
ui <- fluidPage(
titlePanel("Word Frequency Analysis for Meiji-era Authors"),
mainPanel(
plotlyOutput("plot"),
dataTableOutput("Print")
)
)
server <- function(input, output){
output$plot<-renderPlotly({
p <- plot_ly(Trial, x=V1, y=V2, text=rownames(Trial), mode = "text")
#plot(p)
p
})
output$Print<-renderDataTable({Trial})
}
shinyApp(ui = ui, server = server)

Related

How can I plot a heatmap with the heatmaply package in Shiny?

I am trying to use the heatmaply package in order to plot a heatmap and it works well.
On the other hand, when I try to do the same plot in Shiny it doesn't appear in the interface (when I click "run app"). However, when I close the window suddenly the plot appears in the R viewer. Is it possible that the heatmaply package doesn't work with Shiny?
This is my code, when I plot it in R.
library(heatmaply)
x <- as.matrix(datasets::mtcars)
rc <- colorspace::rainbow_hcl(nrow(x))
heatmaply(
x[, -c(8, 9)],
col_side_colors = rc[1:9],
showticklabels=FALSE,
Rowv = TRUE,
Colv = FALSE
)
This is my code in Shiny.
library(shiny)
library(heatmaply)
ui <- fluidPage(
# Application title
titlePanel("Heatmap"),
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
x <- as.matrix(datasets::mtcars)
rc <- colorspace::rainbow_hcl(nrow(x))
server <- function(input, output) {
output$distPlot <- renderPlot({
heatmaply(
x[, -c(8, 9)],
col_side_colors = rc[1:9],
showticklabels=FALSE,
Rowv = TRUE,
Colv = FALSE
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have tried another packages to have an interactive heatmap but it is the only one that it has what I want, so for that reason I need to ask here if someone knows how to use it in Shiny.
Thanks in advance,
Regards
You can use plotlyOutput and renderPlotly :
library(shiny)
library(heatmaply)
library(plotly)
ui <- fluidPage(
# Application title
titlePanel("Heatmap"),
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
plotlyOutput("distPlot")
)
)
)
x <- as.matrix(datasets::mtcars)
rc <- colorspace::rainbow_hcl(nrow(x))
server <- function(input, output) {
output$distPlot <- renderPlotly({
heatmaply(
x[, -c(8, 9)],
col_side_colors = rc[1:9],
showticklabels=FALSE,
Rowv = TRUE,
Colv = FALSE
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Also there is a package shinyHeatmaply which might be of interest.

How to use plotmeans or geom_errorbar for a Shiny app

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.

Graph is not generating properly in shiny only a single bar is shown in graph

I have imported the dataset = students for generating the reactive plots but proper plot is not generated .I am using ggplot for plots so could you
please tell me whats wrong in my code.
library(shiny)
library(ggplot2)
ui <- navbarPage("A SHINY APP!! ",
tabPanel("Plots",headerPanel("Different plots of data"),
sidebarLayout(
sidebarPanel(
selectInput("x.col","x.variable",choices=names(students))
),
mainPanel(plotOutput("histPlot")))
)
)
server <- function(input, output) {
plot <- reactive({ ggplot(students,aes(x=input$x.col))
})
output$histPlot <- renderPlot({
plot() + geom_histogram(stat = "count",bins = 30)
})
}
shinyApp(ui = ui, server = server
Try with get() function like the following:
ggplot(students, aes(x = get(input$x.col)))

shiny - interactive ggplot with subset

I am new to R&shiny. I'd like to make a shiny app that the plot can be interactive with subset I choose, but ggplot cannot work with warning
Error in ouptut$Trendplot <- renderPlot({ : object 'ouptut' not found
It will be really appreciated if you can help to figure it works.
The following is my code:
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- pageWithSidebar(
# Application title
headerPanel("Pre-report situation"),
# Sidebar with a slider input for number of bins
sidebarPanel(selectizeInput("DMS", "DMS:", choices = unique(datass$DMS)
)),
# Show a plot of the generated distribution
mainPanel(
h3(textOutput("caption")),
plotOutput("Trendplot"))
)
datass <- read.csv("C:/Users/yyu6/Documents/PR.csv", sep=",", stringsAsFactors = FALSE)
# Define server logic required to draw a histogram
server <- function(input, output) {
formulaText <- reactive({
input$DMS })
datasetInput <- reactive({
selection <- Input$DMS
subset(datass, DMS == selection)
})
output$caption <- renderText({formulaText()
})
ouptut$Trendplot <- renderPlot({
ggplot(datasetInput(), mapping = aes(x=DMS))+geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)

fix selectInput error on initial shinyr app load

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))})

Resources