Below you can see my test application which I built in R with flexdashboard library (pics+code)
---
title: "Test APPLICATION"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(data.table)
library(tidyr)
library(dplyr)
library(tidyverse)
library(reshape)
library(knitr)
library(DT)
library(rpivotTable)
library(openintro)
library(DataCombine)
library(viridisLite)
library(eurostat)
library(tidyquant)
library(scales)
library(OECD)
library(lubridate)
library(wbstats)
library(OECD)
require(scales)
library(ggplot2)
library(dplyr)
library(openintro)
library(DataCombine)
library(viridisLite)
library(eurostat)
library(tidyquant)
library(scales)
library(readxl)
library(dplyr)
library(zoo)
library(rsconnect)
library(tidyverse)
library(tidyr)
library(data.table)
data=mtcars
data$cyl <- as.factor(data$cyl)
```
# Intro {.sidebar}
Testing application
PART 1
=======================================================================
Row {.tabset}
-----------------------------------------------------------------------
### Notes
**DIFFERENT TYPES OF CARS**
- Names:
- Consumption:
- Number of cylinders
### Tab 1
```{r,eval=TRUE}
rpivotTable(
data,
aggregatorName = "Integer Sum",
vals="mpg",
cols = "Car Parametars",
width="150%",
height="400px",
rendererName = "Table", buttons=c('copy','csv','excel','pdf','print'))
```
PART 2
=======================================================================
Row
-----------------------------------------------------------------------
### Cars 1
```{r,eval=TRUE}
fig<-qplot(x = data$wt, y = data$mpg)
fig
```
### Cars 2
```{r,eval=TRUE}
fig<-qplot(data$cyl, geom = "bar")
fig
```
Row {.tabset}
-----------------------------------------------------------------------
### Cars 3
```{r,eval=TRUE}
fig<-ggplot(data, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3)
fig
```
### Cars 4
```{r,eval=TRUE}
plt <-ggplot(data, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3) +
geom_smooth(method="lm", aes(fill=cyl))
plt + facet_wrap(~cyl)
```
Now I want to divide Tab 1. into two separate subtabs, Tab 1a and Tab 1b. Those subtabs need to be placed below Tab 1.
Tab 1 is the existing table from Tab 1, while Tab 1B is the graphs from Part 2.
After adding these tabs final output must be as in the picture below.
So can anybody help me with how to solve this problem?
Related
I am trying to implement an option with a drop-down menu in a simple Shiny application. With this drop-down list, you can choose between two charts. The first chart is prepared with the ggplot2 library, while the second is with the Plotly library. Below you can see my code :
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(plotly)
# 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 <- plot_ly(data, x = ~wt, y = ~mpg, type = 'bar')
renderPlot({
switch(input$clusterNum,
"Chart1" = Chart1,
"Chart2" = Chart2
)
})
```
After executing this code, I saw that Chart 1, which was prepared with ggplot2, works well, while Chart 2 with Plotly is not displayed. So can anybody help me how to solve this problem and to see Chart 2 after selection with the drop-down list?
To render the plotly chart you have to use renderPlotly, while for the ggplot we have to stick with renderPlot. As a consequence switching conditionally between the two render functions requires some more effort and involves wrapping in renderUI and displaying the chart via uiOutput:
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(plotly)
# 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 <- plot_ly(data, x = ~wt, y = ~mpg, type = 'bar')
```
```{r}
observeEvent(input$clusterNum, {
output$plot <- switch(input$clusterNum,
"Chart1" = renderUI({renderPlot(Chart1)}),
"Chart2" = renderUI({renderPlotly(Chart2)})
)
})
uiOutput("plot")
```
So im trying to create a dashboard however, whenever I load the dashboard there is code on the dashboard and you can only see half of the chart . I'm unsure where the error is occurring and why the dashboard looks like it does. How do I make it show only the chart is shown and not the code?
```
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org")
p_load(tidyverse, ggthemes, magrittr, lubridate, tidyquant, gridExtra,flexdashboard, knitr, RColorBrewer, hrbrthemes, anytime, plotly)
library(flexdashboard)
Grammy <- read_csv("Grammy.csv") |> rename("Brandi Carlile" = "Brandi Carilie")
Grammy$Date <- strptime(Grammy$Date, format="%m/%d/%Y %H:%M")
Grammy <- mutate(Grammy, Date = as.Date(Grammy$Date, "%m/%d/%Y"))
Grammy$Date <-anydate(Grammy$Date)
Grammy$Date <- as.Date(Grammy$Date)
Grammy$Date<- as.Date(Grammy$Date) #convert to date
Grammylonger <- Grammy |>
pivot_longer(cols = `Brandi Carlile`:`Silk Sonic`, names_to = "Artists", values_to = "Streams")
Page 1
=====================================
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
p <- ggplot(data = Grammy, aes(x=Date, y=`Brandi Carlile`)) +
geom_line()+
geom_point()+
scale_x_date(date_breaks = "1 day", date_labels = "%b %Y") +
scale_y_continuous(breaks=seq(3000000, 4000000, 10000))
ggplotly(p)
```
you can add this argument to the quotation code mark
```{r warning=TRUE, include=FALSE}
```
This will hide the code and the warning message if happen
If I'm understanding your question correctly, try setting {r echo = FALSE}
This is the code:
---
title: "Data Analysis"
author: "Author"
date: "`r Sys.Date()`"
output: word_document
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(viridisLite)
library(ggplot2)
library(GGally)
library(plotly)
library(readxl)
library(vtable)
library(imputeTS)
library(janitor)
library(tibble)
library(readr)
library(survival)
library(survminer)
library(tidyr)
library(tidyverse)
library(broom)
library(DataExplorer)
library(dplyr)
library(WeibullR)
library(ggfortify)
library(factoextra)
library(gridExtra)
```
Text Text Text
{r unsorted, echo=FALSE, warning=FALSE}
setwd("C:/Users/R//")
df = data.frame(read.csv("file.csv"
, header = TRUE
, sep = ";"
, dec = ","
, na.strings = "---"))
# clean data frame ----
df<- df %>%
clean_names()
df<- df %>% janitor::remove_empty(whic=c("rows"))
df<- df %>% janitor::remove_empty(whic=c("cols"))
df<- dplyr::distinct(df)
colnames(df)[1]<- "country"
df_unsorted<- df
DataExplorer::plot_missing(df_unsorted[, 1:ncol(df_unsorted)]
, theme_config =list(axis.text=element_text(size = 12))) + theme_bw()
```
which, whyever, results in:
weird enough, that the plots look slightly different but I don't see any reasons why they are plotted, respectively why one of them.
I've also seen Why is this graph showing up twice in R Markdown? but there is no answer given.
The issue is that as a side effect DataExplorer::plot_missing prints the plot and returns the ggplot object invisibly. By adjusting the theme of the returned ggplot object via + theme_bw you get a second plot.
One option to prevent that would be to set the theme via the ggtheme argument.
Making use of the default example from ?DataExplorer::plot_missing:
---
output: html_document
date: '2022-04-20'
---
```{r}
library(DataExplorer)
library(ggplot2)
plot_missing(airquality, theme_config = list(axis.text = element_text(size = 12)), ggtheme = theme_bw())
```
I want the flex dashboard to take up the full screen (full width, full height) and scroll to fit a large number of plots in a facet wrap. Made my issue reproducible using nycflights13, provided below and produces un-readable plots super compressed vertically. how can I achieve this in flex dashboard?
output of knitting below: https://imgur.com/a/LyfZTw3
---
title: "facet test"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
---
```{r setup, include=FALSE}
library(flexdashboard)
library(nycflights13)
library(tidyr)
library(dplyr)
library(ggplot2)
```
Column
-----------------------------------------------------------------------
### Chart A
```{r dfs and plots, include=FALSE, warning=FALSE, cache=FALSE}
df <- flights %>%
group_by(dest, time_hour) %>%
summarise(n = n()) %>%
ungroup()
sp <- ggplot(df, aes(x=time_hour, y=n)) + geom_line()
fr <- sp + facet_wrap(~ dest)
```
```{r facet, out.width = '100%', warning=FALSE, echo=FALSE, message=FALSE, error=TRUE}
fr
```
You could try experimenting with the fig.height and fig.width options. This gives a fairly reasonable output for me:
---
title: "facet test"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
---
```{r setup, include=FALSE}
library(flexdashboard)
library(nycflights13)
library(tidyr)
library(dplyr)
library(ggplot2)
```
Column
-----------------------------------------------------------------------
### Chart A
```{r dfs and plots, include=FALSE, warning=FALSE, cache=FALSE}
df <- flights %>%
group_by(dest, time_hour) %>%
summarise(n = n()) %>%
ungroup()
sp <- ggplot(df, aes(x=time_hour, y=n)) + geom_line()
fr <-
sp +
facet_wrap(~ dest) +
theme(strip.text.x = element_text(size = 8))
```
```{r facet, echo=FALSE, error=TRUE, fig.height=20, fig.width=16, message=FALSE, warning=FALSE}
fr
```
I'm displaying long figures in a markdown report.
These are long because they use ggplot2::facet_wrap so their height depend on the data, which is not constant.
I can set the figure.height parameter of the chunk but then it's fixed and my report looks bad. Is their a way around this ?
Example :
---
title: "title"
author: "author"
date: '`r Sys.Date()`'
output: html_document
---
```{r, figure.height=40}
library(dplyr)
library(ggplot2)
iris %>%
mutate_at("Sepal.Length",cut, 5) %>%
mutate_at("Sepal.Width",cut,2) %>%
group_by_at(c(1,2,5)) %>%
summarize_at("Petal.Length",mean) %>%
ggplot(aes(Species, Petal.Length)) +
geom_col() +
facet_wrap(Sepal.Length ~ Sepal.Width,ncol=2)
```
I had a similar issue and was not able to get Peter's solution to work. From what I'm able to gather, eval.after does not work with fig.height.
But thanks to Peter's example, I was able to find a work-around:
---
author: "author"
date: '`r Sys.Date()`'
output: html_document
---
```{r setup}
library(dplyr)
library(ggplot2)
library(knitr)
FACET_HEIGHT <- 3.4
```
In chunk 1: First, create the ggplot.
Then, use `ggplot_build` to create a new variable called `adaptive_figure_height`.
Finally, use knitr::opts_chunk$set to update the chunk option `fig.height` to better suit your ggplot.
```{r}
g <-
iris %>%
mutate_at("Sepal.Length",cut, 5) %>%
mutate_at("Sepal.Width",cut,2) %>%
group_by_at(c(1,2,5)) %>%
summarize_at("Petal.Length",mean) %>%
ggplot(aes(Species, Petal.Length)) +
geom_col() +
facet_wrap(Sepal.Length ~ Sepal.Width, ncol = 2)
adaptive_fig_height <- FACET_HEIGHT * max(ggplot_build(g)$layout$layout$ROW)
opts_chunk$set( fig.height = adaptive_fig_height )
```
In chunk 2: Plot the ggplot.
If needed, you can revert `fig.height` back to a default value.
```{r }
g
opts_chunk$set( fig.height = 7 )
```
Repeat the setup in chunk 1 and 2 if you have multiple long plots with differing heights.
To go along with the n * single_height idea: you can use the chunk option eval.after so that the fig.width and fig.height options will be evaluated after the rest of the chunk is evaluated and then use the ggplot_build to pull apart a ggplot object and determine the number of rows and columns used in the facets.
For example:
---
author: "author"
date: '`r Sys.Date()`'
output: html_document
---
```{r setup}
library(dplyr)
library(ggplot2)
library(knitr)
FACET_HEIGHT = 3.4
FACET_WIDTH = 5
opts_chunk$set(out.width = "80%",
out.height = "80%",
eval.after = c("fig.height", "fig.width"))
```
For the example we'll have one basic plot to which we will set different facets.
```{r}
g <-
iris %>%
mutate_at("Sepal.Length",cut, 5) %>%
mutate_at("Sepal.Width",cut,2) %>%
group_by_at(c(1,2,5)) %>%
summarize_at("Petal.Length",mean) %>%
ggplot(aes(Species, Petal.Length)) +
geom_col()
```
A graphic with two columns
```{r fig1, fig.height = FACET_HEIGHT * max(ggplot_build(g1)$layout$layout$ROW), fig.width = FACET_WIDTH * max(ggplot_build(g1)$layout$layout$COL)}
g1 <- g + facet_wrap(Sepal.Length ~ Sepal.Width, ncol = 2)
g1
```
A graphic with two rows
```{r fig2, fig.height = FACET_HEIGHT * max(ggplot_build(g2)$layout$layout$ROW), fig.width = FACET_WIDTH * max(ggplot_build(g2)$layout$layout$COL)}
g2 <- g + facet_wrap(Sepal.Length ~ Sepal.Width, nrow = 2)
g2
```
A screenshot of the resulting html is:
Some fine tuning of the image width and height will be needed, but this should be a good starting point.