title: Dynamic Pie Chart in R Using A Shiny Dashboard
body: <p>I am currently working on a Shiny dashboard that has a slider that outputs the optimal portfolio weights of stocks, gold and silver for a given volatility value using the slider input. I have been able to output the values with a dynamic text output given the slider input, but I cannot figure out how to turn these values into a graph as the text output in Shiny seems to require a function. How can I add a pie chart to this code using the values I get from <code>slidervalues()</code>? It outputs a list of 5 numerical values, now I want to chart the first 3 of them in a pie chart:</p>
  <pre><code>library(shiny)
  shinyUI(fluidPage(
    headerPanel(title = &quot;Volatility Slider&quot;),
    sidebarLayout(
      sidebarPanel(
        sliderInput(&quot;Risk&quot;,&quot;Volatility&quot;, 0, 0.24, 0.12)
      ),
      mainPanel(
        textOutput(&quot;Output&quot;)
        
      )
    )
  ))
  
  
  server &lt;- function(input, output) {
    
    
    sliderValues &lt;- reactive({
      #This part finds the optimal portfolio using CAPM(which is found in a different script).
      custom &lt;- three_assets %&gt;%
        filter(sd_p &gt; input$Risk,
               sd_p &lt; input$Risk+0.0001)
      max_er_custom &lt;- custom[custom$er_p == max(custom$er_p)]
      toString(max_er_custom)
    })
    
    
    output$Output &lt;- renderText({
      sliderValues()
    
    })
    
    
  }
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/CXWgD.png" rel="nofollow noreferrer">Here is a screenshot of the dashboard. The first three values are the weights of the three assets, the forth value is the expected return of that portfolio and the last value is the volatility of that portfolio all using historical data.</a></p>