英文:
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 <- fluidPage(
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
DT::dataTableOutput("bin_val")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
output$bin_val <- 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 <- function(input, output) {
rv = reactiveValues() # create reactiveValues
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
rv$bins <- seq(min(x), max(x), length.out = input$bins + 1) # set "bins" to rv
# draw the histogram with the specified number of bins
hist(x, breaks = rv$bins, col = 'darkgray', border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
output$bin_val <- DT::renderDT({
validate(need(!is.null(rv$bins), "No data"))
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 <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of 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 = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
})
output$bin_val <- DT::renderDT(
datatable(data.frame(bins = bins()))
)
}
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论