以闪亮的方式将数据从一个反应部分传递到另一个
Pass data from one reactive part to other in shiny
在下面闪亮的应用程序中,我想在 renderPlot
.
中使用来自 reactive
调用数据的数据框 dt
我以不同的方式尝试过:ggplot(dt, aes(x, y)) + geom_point()
和 ggplot(data(), aes(x, y)) + geom_point()
我只是不知道如何将数据帧从一个反应部分传输到另一个反应部分。
编辑
我想我通过使用找到了解决方案: ggplot(data()$dt, aes(x,y) + ...
但现在问题似乎出在 dplyr
包的 filter
中。
有什么建议吗?
服务器:
# server
library(dplyr)
library(shiny)
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100)) %>%
mutate(id = ntile(x, 4))
shinyServer(function(input, output) {
data <- reactive({
dt <- dt %>%
filter(id == input$id)
})
output$plot <- renderPlot({
ggplot(dt, aes(x,y) +
geom_point()
})
})
ui:
## ui
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
sidebarPanel(width = 2,
selectInput("id",
"Select ID:",
c(1:4))
),
mainPanel(width = 10,
plotOutput("plot")
)
))
您的代码中有一些错误(您提供的甚至没有 运行),但最重要的是您必须了解反应式的工作原理。我建议再次阅读闪亮的教程,尤其是关于反应变量的部分。渲染绘图时,您想使用 data
的值,而不是 dt
.
的值
其他错误:
- 您定义了一个数据框
df
但在您的后续代码中您使用了一个不存在的变量 dt
- 您在 ggplot 调用中没有右括号
这是您的代码的工作版本:
df <- data.frame(x = rnorm(100), y = rnorm(100)) %>%
mutate(id = ntile(x, 4))
runApp(shinyApp(
ui = fluidPage(
sidebarPanel(width = 2,
selectInput("id",
"Select ID:",
c(1:4))
),
mainPanel(width = 10,
plotOutput("plot")
)
),
server = function(input, output, session) {
data <- reactive({
df <- df %>%
filter(id == input$id)
df
})
output$plot <- renderPlot({
ggplot(data(), aes(x,y)) +
geom_point()
})
}
))
在下面闪亮的应用程序中,我想在 renderPlot
.
reactive
调用数据的数据框 dt
我以不同的方式尝试过:ggplot(dt, aes(x, y)) + geom_point()
和 ggplot(data(), aes(x, y)) + geom_point()
我只是不知道如何将数据帧从一个反应部分传输到另一个反应部分。
编辑
我想我通过使用找到了解决方案: ggplot(data()$dt, aes(x,y) + ...
但现在问题似乎出在 dplyr
包的 filter
中。
有什么建议吗?
服务器:
# server
library(dplyr)
library(shiny)
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100)) %>%
mutate(id = ntile(x, 4))
shinyServer(function(input, output) {
data <- reactive({
dt <- dt %>%
filter(id == input$id)
})
output$plot <- renderPlot({
ggplot(dt, aes(x,y) +
geom_point()
})
})
ui:
## ui
library(shiny)
library(ggplot2)
shinyUI(fluidPage(
sidebarPanel(width = 2,
selectInput("id",
"Select ID:",
c(1:4))
),
mainPanel(width = 10,
plotOutput("plot")
)
))
您的代码中有一些错误(您提供的甚至没有 运行),但最重要的是您必须了解反应式的工作原理。我建议再次阅读闪亮的教程,尤其是关于反应变量的部分。渲染绘图时,您想使用 data
的值,而不是 dt
.
其他错误:
- 您定义了一个数据框
df
但在您的后续代码中您使用了一个不存在的变量dt
- 您在 ggplot 调用中没有右括号
这是您的代码的工作版本:
df <- data.frame(x = rnorm(100), y = rnorm(100)) %>%
mutate(id = ntile(x, 4))
runApp(shinyApp(
ui = fluidPage(
sidebarPanel(width = 2,
selectInput("id",
"Select ID:",
c(1:4))
),
mainPanel(width = 10,
plotOutput("plot")
)
),
server = function(input, output, session) {
data <- reactive({
df <- df %>%
filter(id == input$id)
df
})
output$plot <- renderPlot({
ggplot(data(), aes(x,y)) +
geom_point()
})
}
))