Passing calculations run in renderPlotly to be shown in table below graph (without global variables)

huangapple go评论78阅读模式
英文:

Passing calculations run in renderPlotly to be shown in table below graph (without global variables)

问题

以下是翻译好的部分:

library(shiny)
library(tidyverse)
library(plotly)
library(DT)

# 为绘制直方图的应用程序定义UI界面
ui <- fluidPage(

    # 侧边栏包含滑块输入,用于设置直方图的柱子数量
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "柱子数量:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # 显示生成的分布的图表和数据表格
        mainPanel(
           plotOutput("distPlot"),
           DT::dataTableOutput("bin_val")
        )
    )
)

# 为绘制直方图所需的服务器逻辑
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # 根据来自ui.R的input$bins生成柱子
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # 使用指定数量的柱子绘制直方图
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             xlab = '等待下一次喷发时间(以分钟为单位)',
             main = '等待时间的直方图')
    })
    output$bin_val <- DT::renderDT(datatable(bins))
}

# 运行应用程序
shinyApp(ui = ui, server = server)

请注意,我已经根据您的要求将代码部分保持不翻译。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

What is the best way to take calculations generated in one output and print it as a datatable below the graph. Take the old faithful data where you've defined the "bins" value and you want the user to see what it equals without recalculating it, and without assigning a global variable?

library(shiny)
library(tidyverse)
library(plotly)
library(DT)

# Define UI for application that draws a histogram
ui &lt;- fluidPage(


    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput(&quot;bins&quot;,
                        &quot;Number of bins:&quot;,
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput(&quot;distPlot&quot;),
           DT::dataTableOutput(&quot;bin_val&quot;)
        )
    )
)

# Define server logic required to draw a histogram
server &lt;- function(input, output) {

    output$distPlot &lt;- renderPlot({
        # generate bins based on input$bins from ui.R
        x    &lt;- faithful[, 2]
        bins &lt;- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = &#39;darkgray&#39;, border = &#39;white&#39;,
             xlab = &#39;Waiting time to next eruption (in mins)&#39;,
             main = &#39;Histogram of waiting times&#39;)
    })
    output$bin_val &lt;- DT::renderDT(datatable(bins))
}

# Run the application 
shinyApp(ui = ui, server = server)

答案1

得分: 1

不确定您具体要求的是否可行。您需要将bins保存在某个地方,因为distPlot的输出是一个图形,无法直接访问该函数内的任何变量。

下面是一个实例化reactiveValues对象的选项。然后,在您的renderPlot函数内,您可以将bins保存到该对象中。

在这种情况下,由于bins是一个向量,您需要将其转换为数据框,以便DT::datatable可以正确呈现表格。

# 定义绘制直方图所需的服务器逻辑
server <- function(input, output) {
  
  rv = reactiveValues() # 创建reactiveValues

  output$distPlot <- renderPlot({
    # 根据ui.R中的input$bins生成bins
    x    <- faithful[, 2]
    rv$bins <- seq(min(x), max(x), length.out = input$bins + 1) # 将"bins"设置为rv

    # 使用指定数量的bins绘制直方图
    hist(x, breaks = rv$bins, col = 'darkgray', border = 'white',
         xlab = '等待下次喷发时间(分钟)',
         main = '等待时间直方图')
  })
  
  output$bin_val <- DT::renderDT({
    validate(need(!is.null(rv$bins), "没有数据"))
    datatable(data.frame(bins = rv$bins))
  })
}

请注意,上述代码块中的中文注释是我添加的,以帮助您更好地理解代码的功能。

英文:

Not sure if what you're asking for specifically can be done. You need to save bins somewhere since the output of distPlot is a plot and any variables inside of that function can not be accessed directly.

Below is an option to instantiate a reactiveValues object. Then inside your renderPlot function you can save bins to that object.

In this case since bins is a vector you need to convert it to a data.frame in order for DT::datatable to render the table properly.

# Define server logic required to draw a histogram
server &lt;- function(input, output) {
  
  rv = reactiveValues() # create reactiveValues

  output$distPlot &lt;- renderPlot({
    # generate bins based on input$bins from ui.R
    x    &lt;- faithful[, 2]
    rv$bins &lt;- seq(min(x), max(x), length.out = input$bins + 1) # set &quot;bins&quot; to rv

    # draw the histogram with the specified number of bins
    hist(x, breaks = rv$bins, col = &#39;darkgray&#39;, border = &#39;white&#39;,
         xlab = &#39;Waiting time to next eruption (in mins)&#39;,
         main = &#39;Histogram of waiting times&#39;)
  })
  
  output$bin_val &lt;- DT::renderDT({
    validate(need(!is.null(rv$bins), &quot;No data&quot;))
    datatable(data.frame(bins = rv$bins))
  })
}

答案2

得分: 1

另一个选项是在renderPlot之外使用reactive来计算箱子的数量。此外,我还将“x”的“创建”也移到了一个reactive中:

library(shiny)
library(DT)

# 定义绘制直方图的应用程序的UI
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
        "箱子数量:",
        min = 1,
        max = 50,
        value = 30
      )
    ),
    mainPanel(
      #plotOutput("distPlot"),
      DT::dataTableOutput("bin_val")
    )
  )
)

server <- function(input, output) {
  x <- reactive({
    faithful[, 2]
  })

  bins <- reactive({
    seq(min(x()), max(x()), length.out = input$bins + 1)
  })

  output$distPlot <- renderPlot({
    hist(x(),
      breaks = bins(), col = "darkgray", border = "white",
      xlab = "等待下一次喷发的时间(分钟)",
      main = "等待时间的直方图"
    )
  })
  output$bin_val <- DT::renderDT(
    datatable(data.frame(bins = bins()))
  )
}

shinyApp(ui = ui, server = server)
英文:

Another option would be to use a reactive to compute the number of bins outside of renderPlot. Additionally I moved the "creation" of x to a reactive too:

library(shiny)
library(DT)

# Define UI for application that draws a histogram
ui &lt;- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput(&quot;bins&quot;,
        &quot;Number of bins:&quot;,
        min = 1,
        max = 50,
        value = 30
      )
    ),
    mainPanel(
      #plotOutput(&quot;distPlot&quot;),
      DT::dataTableOutput(&quot;bin_val&quot;)
    )
  )
)

server &lt;- function(input, output) {
  x &lt;- reactive({
    faithful[, 2]
  })

  bins &lt;- reactive({
    seq(min(x()), max(x()), length.out = input$bins + 1)
  })

  output$distPlot &lt;- renderPlot({
    hist(x(),
      breaks = bins(), col = &quot;darkgray&quot;, border = &quot;white&quot;,
      xlab = &quot;Waiting time to next eruption (in mins)&quot;,
      main = &quot;Histogram of waiting times&quot;
    )
  })
  output$bin_val &lt;- DT::renderDT(
    datatable(data.frame(bins = bins()))
  )
}

shinyApp(ui = ui, server = server)

Passing calculations run in renderPlotly to be shown in table below graph (without global variables)

huangapple
  • 本文由 发表于 2023年7月13日 00:20:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定