JS4Shiny R2D3 Section
Repo Layout
data/
: Various CSVs with raw and cleaned data.data_manipulation_scripts/
: R Scripts to read and clean data for use in app.step_*/
: Full shiny app as it should exist at the end of each step.tree_app/
: Final form of Shiny app.slides/
: RMarkdown slides for R2D3 presentation
How to download
Either clone this repo or download the zip file of all code.
Project
Lost?
There are a few things you can do to get unstuck
- You can skip a sticky step by simply going each steps folder to find the code as it should be at the end of the step.
- Click the step title to see the exact code that should change for each step
- Ask questions! If it's confusing I guarentee it's not just you. This is complicated stuff!
Steps
Step 1: Run shiny app with regression model to see how it works
- Open
step_1/app.R
- Press "Run App".
Step 2: Find visualization on Observable and paste into r2d3 script in RStudio
- Go to observable notebook with species visualization
- Copy all the code inside comments
// Start copying here
and// End copying here
. - Create a new "D3 Script" in RStudio
- Paste all code from Observable into new file
- Replace
data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)
in header withreadr::read_csv("data/tree_benefits.csv")
- Save file as
tree_viz.js
instep_1/
folder
Step 3: Add display of plot to shiny app
- Add
d3Output("tree_viz")
in your UI function - Add
output$tree_viz <- renderD3({r2d3(data = tree_benefits, script = "tree_viz.js")})
to Server function - Run app again
Step 4: Wire up message from visualization to shiny
- In
tree_viz.js
, search for the functionsubmit_selection()
and uncomment the lines to send to shiny that starts with// if(Shiny){...
. - Replace the lines for getting chosen species from
species_1 <- input$species_1
tospecies_1 <- input$selected_species[1]
andspecies_2 <- input$species_2
tospecies_2 <- input$selected_species[2]
. - Add a
req(input$selected_species)
line at start ofoutput$regressionOutput <- renderPlot({
to avoid shiny attempting to run without input.