Using plot_click in flexdashboard - r

Is there a way to have plots with mouse interactions using flexdashboard?
In shiny this is not difficult. I want to save mouse clicks, and in shiny UI I would use:
mainPanel(plotOutput("scatterplot", click = "plot_click"))
And in the server you would have:
df <- reactiveValues(Clicksdf = data.frame(clickx = numeric(), clicky = numeric()))
Can I do this in flexdashboard?

Write the code chunk as if it were both the Shiny UI and server:
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
plotOutput("plot1", click = "wt")
output$plot1 <- renderPlot({
plot(mtcars$mpg ~ mtcars$wt)
})
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
renderText({
unlist(input$wt$x)
})
```

Related

Selection of charts in Shiny

I am trying to create a simple Shiny app. My goal is to select one of the charts from the drop-down menu and visualization of the selected chart. Below you will see my script.
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
# Libraries ----
# App
library(flexdashboard)
library(shiny)
library(shinyjs)
library(shinyWidgets)
# Core
library(tidyverse)
library(tidyquant)
# Visualizations
library(ggplot2)
# Data
data=mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
useShinyjs(rmd = TRUE)
selectInput("clusterNum", label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1")
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1<-qplot(x = data$wt, y = data$mpg)
Chart2<-qplot(data$cyl, geom = "bar")
num <- reactive(input$clusterNum)
renderPlot(num())
```
When I run this script, I can change the names of the Charts (e.g., Chart1 or Chart2) from the drop-down menu, but I can't see a visualization of the selected chart. So can anybody help me how to solve this problem and to have a visualization as in the pic below?
You could use switch or an ìf-else inside your renderPlot to display the chosen chart.
Note: qplot was depcrecated in ggplot2 3.4.0 and will probably removed in the future. To take account of this I switched to ggplot().
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
# Data
data <- mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("clusterNum",
label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1"
)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1 <- ggplot(data, aes(x = wt, y = mpg)) +
geom_point()
Chart2 <- ggplot(data, aes(cyl)) +
geom_bar()
renderPlot({
switch(input$clusterNum,
"Chart1" = Chart1,
"Chart2" = Chart2
)
})
```

Flexdashboard columns - text not showing up

In my flexdashboard I have a column with a note, then a table, and I would like to add one more note below the table. But I am struggling to get the second note to show up. I can make a new header there but I really just want another sentence that shows up without a new header. Here is some simple code to illustrate. Thanks!
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(shiny)
library(rhandsontable)
df <- tibble(
`Col 1` = seq(1,24,1), `Col 2` = " ")
```
Column {data-width=650}
-----------------------------------------------------------------------
### Table
I can write a note here
```{r}
output$table_exer <- renderRHandsontable({
rhandsontable(df, rowHeaders = NULL)
})
rHandsontableOutput("table_exer")
```
But I also want a note here
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
```
### Chart C
```{r}
```
very simple, wrap your table inside fluidRow, like this:
```{r}
output$table_exer <- renderRHandsontable({
rhandsontable(df, rowHeaders = NULL)
})
fluidRow(rHandsontableOutput("table_exer"))
```
To make the margin and spacing look nicer, we can also do following:
```{r}
output$table_exer <- renderRHandsontable({
rhandsontable(df, rowHeaders = NULL)
})
column(12, fluidRow(rHandsontableOutput("table_exer")))
br()
```

How to use shiny inputpael to filter my dataframe by a category in a column? Rshiny and RMarkdown

I'm trying to make a simple shiny app where I can select an input of cylinders(4,6, or 8) and then generate a table with the cars who are either 4,6, or 8 cylinders.
This is my code
---
title: "Test Dash"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
Inputs {.sidebar data-width=300}
=====================================
$$\\[.01in]$$
```{r pressure, echo=FALSE, out.width = '100%'}
library(readr)
library(shiny)
library(DT)
```
```{r}
cylinder <- mtcars$cyl
selectInput("my_dropdown", label = "Select Cylinders:", choices = cylinder)
```
# Overview
Column {data-width=750}
-----------------------------------------------------------------------
### Table
```{r}
renderDataTable(
datatable(mtcars[input$my_dropdown,]
)
)
```
i'm able to generate the input and an empty table but the table does not update when I choose a cylinder.
Here is a screenshot.
Any idea how I can fix this?
mtcars2 <- reactive({
mtcars[mtcars$cyl == input$my_dropdown,]
})
renderDataTable(
datatable(mtcars2())
)
)

Flexdashboard disable shiny input

I have a sidebar with multiple shiny selectInput (country and year). This sidebar is the same for all pages (Summary and World Map) of my flexdashboard. How can I disable the country input on the Summary page ?
I've tried using shinyjs::useShinyjs() but I don't know how to use it in a flexdashboard framework.
---
title: "Title"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
theme: yeti
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(shinyjs)
shinyjs::useShinyjs()
``` //
Sidebar {.sidebar}
=====================================
```{r}
selectInput("countryInput", "Select a country", choices = sort(c(data_long$cname)), selected = 1)
selectInput("yearInput", "Select a year", choices = c(data_long$year), selected = 10)
shinyjs::disable("countryInput")
``` //
Summary
=====================================
Row
-----------------------------------------------------------------------
### Graph 1
```{r}
omitted
``` //
### Graph 2
```{r}
omitted
``` //
Row
-----------------------------------------------------------------------
### Data
```{r}
omitted
``` //
World map
=====================================
Row
-----------------------------------------------------------------------
### map
```{r}
omitted
``` //
EDIT: spelling

Input menu contents do not overflow row box in flexdashboard

I am creating a reactive dashboard using flexdashboard with shiny. I would like the first row below the header to contain two separate selectInput() menus. I am keeping the height of the rows small (50), because there will be several more graphs underneath and I need the space. However, when the dashboard is rendered, clicking on the input menu to select another option results in the dropdown "disappearing" behind the row (not overflowing it). This makes it difficult to select items below the second or third element. How do I make the contents overflow? Here is a reproducible code + screenshot:
---
title: "Test Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Row {data-height=50}
-----------------------------------------------------------------------
### Window 1
```{r}
selectInput("project", label = NULL, choices = c("A","B","C","D"))
```
### Window 2
```{r}
selectInput("data", label = NULL, choices = c("1","2","3","4","5", "6"))
```
Row
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot({
plot(rnorm(1000), type = "l", main = paste("Project:",input$project, " Data:", input$data))
})
```
Attached an image of how it looks.
Flexdasboard image:
Thanks much,
Jorge
You said there will be more graphs in the future, but I think you can render the individual plots 'underneath' Flexdashboard, to save space. I changed, the rows to columns, let me know.
---
title: "Test Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Column {data-width=600}
-----------------------------------------------------------------------
### Window 1
```{r}
selectInput("project", label = NULL, choices = c("A","B","C","D"))
```
### Window 2
```{r}
selectInput("data", label = NULL, choices = c("1","2","3","4","5", "6"))
```
Column {data-width=400}
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot({
plot(rnorm(1000), type = "l", main = paste("Project:",input$project, " Data:", input$data))
})
```

Resources