How is data passed from reactive Shiny expression to ggvis plot?
ggvis can be passed "bare reactives" for a dataset. When you do this ggvis will automatically replot the data when it changes but it doesn't need to replot the entire plot:
get_rct_df = reactive({ #code to change dataset from shiny input )}
get_rct_df %>% ggvis() #rest of plotting code %>% bind_shiny("plot")
This will update the data points in the plot (but not redraw the entire plot) each time the data from get_rct_df changes. This also means that certain things cannot be updated without redrawing the entire plot (plot labels, equations for layer_model_predictions).
You can do the other suggestion and wrap the entire plot in a reactive:
reactive({
get_rct_df %>%
ggvis() #rest of plotting code %>%
add_axis("x", title = input$title)
}) %>% bind_shiny("plot")
This will allow you to update plot titles and other parts of the plot. But it also forces ggvis to replot the entire plot when something changes as opposed to just replotting the data. If you test out both methods, method 1 will look "smoother"; ggvis has built in transition animations when you data changes.
Shiny ggvis reactive plot
Try
1) not change df into reactive
data2 <- reactive({
df3=df
df3 <- df3[df3$Category %in% input$Category,]
df3 <- df3[df3$Brand %in% input$Brand,] # if I comment this line, I get All brands graph
df3 <- droplevels(df3)
df3<- ddply(df3, c("Letter", "Category", "Brand"), summarise, "USD" = sum(USD))
})
2)to add if statement
if(!"All" %in% input$Brand){
df3 <- df3[df3$Brand %in% input$Brand,] # if I comment this line, I get All brands graph
}
ggvis plots rendered using bind_shiny() aren't reactive
So this answer might be useful.
It looks like in order to reference your input$maxValParam
inside a ggvis() function, the entire ggvis function needs to be wrapped in a reactive.
blatantly ripping off the above answer, yours might look something like:
reactive({
dataframe %>%
ggvis() #rest of plotting code %>%
add_axis("x", title = input$maxValParam)
}) %>% bind_shiny("plot")
Order of execution in ggvis and reactive slider input shiny
Placing an invalidateLater
for 1 second in your reactive plot command will make the renderer wait just enough for the slider to reinitialize. I also changed the Sys.sleep
to a much shorter time, so the re-plot delay is almost imperceptible
# note the added 'session' argument
server <- function(input, output, session) {
data_0 <- reactive({
df <- module1_df %>%
filter(prod == input$prod)
})
output$in_value <- renderUI({
df2 <- data_0()
var <- 'value'
min_value <- min(df2[,var])
sliderInput('value','', min = round(min_value ,0),
max = 1, value = c(0, 1), step = 0.1)
})
data <- reactive({
df3 <- data_0()
if (!is.null(input$value)) {
df3 <- df3 %>%
filter(df3[['value']] >= input$value[1] &
df3[['value']] <= input$value[2])
}
return(df3)
})
plot <- reactive({
invalidateLater(1000, session)
withProgress('', value = 0, {
y_min <- input$value[1]
y_max <- input$value[2]
plot <- isolate(data()) %>%
ggvis(~id, ~value) %>%
layer_points() %>%
scale_numeric('y', domain = c(y_min, y_max))
Sys.sleep(0.01)
})
return(plot)
})
plot %>% bind_shiny('plot_show')
}
Understanding Reactive in R-Shiny: Preventing Re-loading Data
The nice thing about reactivity is that if you call data1()
multiple times, but the input hasn't changed, the code inside data1
isn't actually going to run multiple times. It will only run once. That's the main idea of reactive variables - they cache their value, meaning that if you call them 100 times without any of their inputs changing, they only evaluate once, and the next 99 times they just immediately return their value that they remember. So the data doesn't actually get re-loaded every time you call data1()
. You can convince yourself of this by putting a print()
statement inside data1
and see that it only gets printed once.
I suggest doing some readings on reactive variables and reactivity to internalize this concept. This tutorial (disclaimer: I wrote it) can be a good resource on this topic: Reactivity 101
How to read a reactive expression from reactivefilereader?
Instead of :
chemin_min_rea <- reactiveFileReader(1000, NULL,chemin_min, readLines)
and then :
read_csv(chemin_min_rea, col_types = cols(.default = "c"), skip = 1 )
do :
chemin_min_rea <- reactiveFileReader(1000,
NULL,
chemin_min,
read_csv,
col_types = cols(.default = "c"),
skip = 1 )
Done!
Dynamic resizing of ggvis plots in shiny apps
This is now possible in the development version of ggvis. See https://github.com/rstudio/ggvis/pull/381
Cannot render ggvis plot in Shiny app using Rmarkdown
The two questions you've linked to show you need the 'ggvis' code inside a reactive({
, not a renderPlot({
This now works for me
---
title: "test"
runtime: shiny
output: html_document
---
```{r config}
library(ggvis)
inputPanel(
sliderInput('n', 'n:', min = 10, max = 100, value = 50),
actionButton('run', 'Run!')
)
data = eventReactive(input$run, { data = data.frame(x = rnorm(input$n)) })
renderTable({summary(data())})
reactive({
data() %>%
ggvis(~x) %>%
layer_histograms() %>%
bind_shiny('plot')
})
ggvisOutput('plot')
```
Related Topics
Applying Gsub to Various Columns
Using Pivot_Longer with Multiple Paired Columns in the Wide Dataset
Calling Library() in R with a Variable as the Argument
Looping Through Covariates in Regression Using R
Adding Percentages to a Grouped Barchart Columns in Ggplot2
Remove Rows Which Have All Nas in Certain Columns
Summarize Different Columns with Different Functions
Shiny Promises Future Is Not Working on Eventreactive
Transposition of a Tibble Using Pivot_Longer() and Pivot_Wider (Tidyverse)
How to Add Axis Text in This Negative and Positive Bars Differently Using Ggplot2
What Is the Internal Implementation of Lists
Margin Adjustments When Using Ggplot's Geom_Tile()
Loading Dplyr After Plyr Is Causing Issues
Scales = "Free" Works for Facet_Wrap But Doesn't for Facet_Grid
Installing "Rgl" Package in R, MAC Osx El Captian
How to Add My Outlook Email Signature to the Com Object Using Rdcomclient