【问题标题】:Dynamic Pie Chart in R Using A Shiny DashboardR中的动态饼图使用闪亮的仪表板
【发布时间】:2021-10-15 14:10:47
【问题描述】:

我目前正在开发一个带有滑块的 Shiny 仪表板,该滑块使用滑块输入输出给定波动率值的股票、黄金和白银的最佳投资组合权重。在给定滑块输入的情况下,我已经能够通过动态文本输出输出值,但我无法弄清楚如何将这些值转换为图形,因为 Shiny 中的文本输出似乎需要一个函数。如何使用从slidervalues() 获得的值向此代码添加饼图?它输出一个包含 5 个数值的列表,现在我想在饼图中绘制前 3 个数值:

library(shiny)
shinyUI(fluidPage(
  headerPanel(title = "Volatility Slider"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("Risk","Volatility", 0, 0.24, 0.12)
    ),
    mainPanel(
      textOutput("Output")
      
    )
  )
))


server <- function(input, output) {
  
  
  sliderValues <- reactive({
    #This part finds the optimal portfolio using CAPM(which is found in a different script).
    custom <- three_assets %>%
      filter(sd_p > input$Risk,
             sd_p < input$Risk+0.0001)
    max_er_custom <- custom[custom$er_p == max(custom$er_p)]
    toString(max_er_custom)
  })
  
  
  output$Output <- renderText({
    sliderValues()
  
  })
  
  
}

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.

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    不确定如何在没有文件的情况下复制您的逻辑,但这里有一个示例,其中输入滑块确定 sliderValues 数据框中的值,这些值又用于创建条形图。

    library(shiny); library(ggplot2)
    ui <- fluidPage(
        headerPanel(title = "Volatility Slider"),
        sidebarLayout(
            sidebarPanel(
                sliderInput("Risk","Volatility", 0, 0.24, 0.12)
            ),
            mainPanel(
                plotOutput("Output")
            )
        )
    )
    
    server <- function(input, output) {
        sliderValues <- reactive({
            data.frame(values = c(input$Risk, 0.7 * (1 - input$Risk), 0.3 * (1-input$Risk)),
                       categories = c("A", "B", "C"))
        })
        
        output$Output <- renderPlot(
            ggplot(sliderValues(), aes(1, values, fill = categories)) +
                geom_col() +
                coord_polar(theta = "y")
        )}
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2019-07-26
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      相关资源
      最近更新 更多