Plotting after Checking the radio Button and Pressing action Button - r

I have a dataframe, please help me in executing this. The moment I check "HoltWinters" and press "Execute" button, dataframe "HW" should appear. I have tried half way. But need anyone help here please................................
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
---
library(flexdashboard)
library(readxl)
library(tidyverse)
library(shiny)
library(rhandsontable)
library(dplyr)
library(forecast)
library(fpp)
library("TTR")
x <- c(1:123)
x <- ts(x, start = c(2017, 23), end = c(2019, 39), frequency = 53)
x.hw <- HoltWinters(x)
HW <- forecast(x.hw, h = 6)
HW <- as.data.frame(HW)
Model Execution
Inputs {.sidebar}
radioButtons("r",h5("Models"),choices = list("Regression", "Arima","HoltWinters","Model4","Model5"),selected = "No", inline = F)
actionButton("a","Execute",icon = NULL)
Row {.tabset .tabset-fade}
HoltWinters
output$table1 <- renderRHandsontable({
eventReactive(input$a,{
rhandsontable(HW)
})
})
rHandsontableOutput("table1")

You should not use eventReactive but observeEvent instead (check here). Also, this condition should be outside of the output part: "if I observe this event, then I will display this table" (and not "I will display this table and then fill it according to which button is ticked").
Here's the solution to your problem (you should customize it so that just selecting HoltWinters displays the table but at least you have a working basis here):
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(shiny)
library(rhandsontable)
library(dplyr)
library(forecast)
library(fpp)
library(TTR)
```
```{r}
x <- c(1:123)
x <- ts(x, start = c(2017, 23), end = c(2019, 39), frequency = 53)
x.hw <- HoltWinters(x)
HW <- forecast(x.hw, h = 6)
HW <- as.data.frame(HW)
```
Model Execution
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
radioButtons("r",h5("Models"),choices = list("Regression", "Arima","HoltWinters","Model4","Model5"),selected = "No", inline = F)
actionButton("a","Execute",icon = NULL)
```
Row {.tabset .tabset-fade}
-------------------------------------
### HoltWinters
```{r}
observeEvent(input$a,{
output$table1 <- renderRHandsontable({
rhandsontable(HW)
})
})
rHandsontableOutput("table1")
```
Edit: you can add a condition within the observeEvent so that the table is displayed only if HoltWinters is ticked:
observeEvent(input$a,{
if (input$r == "HoltWinters") {
output$table1 <- renderRHandsontable({
rhandsontable(HW)
})
}
else {
output$table1 <- renderRHandsontable({
})
}
})
rHandsontableOutput("table1")

Related

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()
```

Reactive Shiny flexdashboard Dynamic Inputs

I am developing a small app that runs on a server. The data is filtered using a session user id. The radio choices should be conditional on the reactive data (only the values of var1 that exist for user "A" in var3 should be available for choice when user "A" is logged in). Any hint? Is observe and updateSelectInput the way to go?
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(magrittr)
library(shiny)
library(ggplot2)
library(dplyr)
```
Column {data-width=650}
-----------------------------------------------------------------------
```{r global, include=FALSE}
set.seed(1234)
df <- data.frame(
var1 = sample(letters, 50, replace = T),
var2 = runif(50),
var3 = sample(LETTERS[1:5], 50, replace = T)
)
df_tmp <- reactive({
var_user <- "A" #session$user
df %>% filter(var3 == var_user) %>% return()
})
```
### Filter
```{r}
radioButtons(label = h4("Choose data"),
inputId = "var1_filter",
choices = df$var1 %>% unique,
selected = df$var1 %>% unique %>% .[1])
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot({
df_tmp() %>% ggplot(aes(var2)) + geom_density()
})
```
See also:
Create an input variable that is dependent on another input variable in flexdashboard shiny widget

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

Enable observeEvent to trigger a eventReactive event

I have this small example of what I'm trying to do:
I am generating a normal distribution of data sims which changes based on which button is pressed. The first 5 values are printed in the first output box. From this, we collect samples of data and calculate the mean of each sample - samp_sims. As the slider is moved, the sample size changes and so do the mean -visible in the second output box.
It is ok that the population values sims don't change when the slider moves. However, if a user is then to click "optionA" after "optionB", the population sims numbers change - but the sample mean numbers (output 2 at the bottom) do not update - they are still from the previous option. They don't update until the slider is moved again. I would like them to also automatically update when the button is pressed. I tried doing this using an observeEvent() action - putting the same code for my eventReactive() event inside, but this didn't work.
Is there a way to ensure that the samp_sims values update whenever the slider moves or a button is pressed?
---
title: "test"
author: "James"
date: "9/28/2020"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE}
radioButtons("go", h3(""),
choices = list("optionA" = 10,
"optionB" = 20),
inline = TRUE
)
```
```{r, echo=FALSE}
sliderInput("sampsize", label = "Sample Size:",
min = 5, max = 100, value = 10, step = 1)
```
```{r, echo=FALSE}
sims <- eventReactive(input$go, {
rnorm(1000, mean = as.numeric(input$go), sd = 2.5)
})
```
```{r, echo = FALSE}
samp_sims <- eventReactive(input$sampsize , {
apply(replicate(100, sample(sims(), input$sampsize, T)),2,mean)
})
```
```{r,echo=FALSE}
observeEvent(input$go, {
})
```
```{r, echo=FALSE}
renderPrint({ sims()[1:5]})
```
```{r, echo=FALSE}
renderPrint({ samp_sims()[1:5]})
```
You should try a list() of two events for eventReactive. In your case,
samp_sims <- eventReactive(list(input$sampsize,input$go) , {
apply(replicate(100, sample(sims(), input$sampsize, T)),2,mean)
})
A working example is below
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("t1", "t1"),
actionButton("t2", "t2")
),
mainPanel(
verbatimTextOutput("view")
)
)
)
server <- function(input, output) {
view <- eventReactive(list(input$t1, input$t2),{
t1 <- input$t1
t2 <- input$t2
rnorm(1)
})
output$view <- renderPrint(view())
}
shinyApp(ui = ui, server = server)

No able to use click/brush in plotlyOutput

I am working on flexdashboard. I have used the below code for one of the plots. Well, when I try to use click or brush under plotlyOutput it is throwing an error like I have used in the second below. Is there a way to use Click under plotlyOutput? Is there a way?......................................
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(lubridate)
library(ggplot2)
library(reshape)
library(shiny)
library(plotly)
```
```{r}
df <- read_excel("E:/Analytics/Freelancing projects/New
folder/df.xlsx")
```
Summary
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("p1",h5("Select"),choices = c("","Plot"))
selectInput("f1",h5("Components"),choices =
c("ALL",levels(factor(Copy_of_mill_para1$variable))),selected = "ALL")
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart A
```{r}
plotlyOutput("g1")
output$g1 <- renderPlotly({
if (input$p1 == "Plot") {
s_data <- df
}
if (input$p1 == "Plot" && input$f1 != "ALL") {
s_data <- s_data %>% filter(variable %in% input$f1)
}
p1 <- ggplot(s_data,aes(x=Date,y=value,color=variable))+geom_line(size =
.2)+theme(axis.text.y=element_text(angle=0,hjust=1,size=0.1))+theme(axis.title.y=element_blank(),axis.title.x=element_blank())+theme(ax
is.text.x=element_blank())+theme(legend.title = element_blank())+
theme(legend.text=element_text(size=7.5))+theme(legend.position = "none")
print(ggplotly(p1))
})
```
### Table
```{r}
verbatimTextOutput("click")
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)"
})
```

Resources