英文:
R Shiny: actionButton to generate random value from input values and render plot
问题
我制作了一个简单的Shiny应用程序,它生成了一个绘图,显示用户可以选择的数据集中某个群组的x和y数据。该应用程序对较大的数据框进行子集化,并仅为所选择的个体生成图形。我还想包括一个"actionButton",每次按下按钮时,它会随机选择一个数据集中的个体并绘制图形。
library(shiny)
library(tidyverse)
# 创建示例数据框
Data <- data.frame(Indiv = c(rep(1,10),
rep(2,10),
rep(3,10),
rep(4,10),
rep(5,10),
rep(6,10)),
varX = c(runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10)),
varY = c(runif(10, min = 1, max = 1),
runif(10, min = 1, max = 5),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 15),
runif(10, min = 1, max = 20),
runif(10, min = 1, max = 25))
)
# Shiny App
# 用户界面
ui <- fluidPage(
titlePanel("Random Select Button"), # 页面标题
sidebarLayout(
# 侧边栏面板
sidebarPanel(
# 用于显示在绘图中的TagID的输入
actionButton("do", "Plot Random Indiv"),
selectInput(inputId = "ID",
label = "Individual ID",
choices = levels(factor(Data$Indiv)),
selected = NULL, # 默认情况下不选择任何个体
multiple = FALSE # 允许多选
),
p("选择一个个体,或按下按钮随机选择一个个体。"),
),
# 主面板
mainPanel(
plotOutput("IndivPlot"), # 绘图对象的名称
),
)
)
# 服务器
server <- function(input, output) {
# 绘图
output$IndivPlot <- renderPlot({ # 制作一个实时可调整的绘图
### 仅包括所选个体
Data <- Data[Data$Indiv %in% input$ID,]
# 创建绘图
p = ggplot(data=Data, aes(x=varX, y=varY))+
geom_line()+
expand_limits(y=c(1,30), x = c(1,10))+
scale_y_continuous(breaks=seq(1,30,5), labels=seq(1,30,5))+
scale_x_continuous(breaks = seq(1,10,1), labels = seq(1,10,1))+
labs(x = "My X", y = "My Y")
print(p)
})
}
shinyApp(ui = ui, server = server)
我无法弄清如何使"actionButton"随机选择一个个体ID号并导致绘图渲染。目前,按钮存在于用户界面上,但不起作用。
英文:
I have made a simple shiny app that generates a plot of the x and y data from an individual group within the data which the users can choose from. The app subsets the larger dataframe and produces the graph for only the chosen individual. I would also like to include and actionButton which chooses an individual from within the dataset at random and plots that every time the button is pushed.
library(shiny)
library(tidyverse)
# Create example data frame
Data <- data.frame(Indiv = c(rep(1,10),
rep(2,10),
rep(3,10),
rep(4,10),
rep(5,10),
rep(6,10)),
varX = c(runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 10)),
varY = c(runif(10, min = 1, max = 1),
runif(10, min = 1, max = 5),
runif(10, min = 1, max = 10),
runif(10, min = 1, max = 15),
runif(10, min = 1, max = 20),
runif(10, min = 1, max = 25))
)
###### Shiny App #######
# User interface for shiny app
ui <- fluidPage(
titlePanel("Random Select Button"), # page title
sidebarLayout(
# Sidebar panel
sidebarPanel(
# input for which TagID to display in plot
actionButton("do", "Plot Random Indiv"),
selectInput(inputId = "ID",
label = "Individual ID",
choices = levels(factor(Data$Indiv)),
selected = NULL, # choose no individuals by default
multiple = FALSE # allow for multiple options
),
p("Choose an invidual, or push the button to randomly choose an individual."),
),
# Main Panel
mainPanel(
plotOutput("IndivPlot"), # name of plot object
),
)
)
# Server
server <- function(input, output) {
# Plot
output$IndivPlot <- renderPlot({ # make a live adjustable plot
### Filter to only include selected Individual
Data <- Data[Data$Indiv %in% input$ID,]
# Create plot
p = ggplot(data=Data, aes(x=varX, y=varY))+
geom_line()+
expand_limits(y=c(1,30), x = c(1,10))+
scale_y_continuous(breaks=seq(1,30,5), labels=seq(1,30,5))+
scale_x_continuous(breaks = seq(1,10,1), labels = seq(1,10,1))+
labs(x = "My X", y = "My Y")
print(p)
})
}
shinyApp(ui = ui, server = server)
I can't figure out how to get the action button to randomly choose and individual ID number and cause the plot to render. Right now, the button exists on the UI but doesn't do anything.
答案1
得分: 2
以下是代码部分的翻译:
library(shiny)
library(tidyverse)
###### Shiny App #######
# Shiny应用程序
# 用户界面
ui <- fluidPage(
titlePanel("Random Select Button"), # 页面标题
sidebarLayout(
# 侧边栏
sidebarPanel(
# 用于在图表中显示TagID的输入
actionButton("do", "Plot Random Indiv"), # 动作按钮,用于生成随机ID
selectInput(inputId = "ID",
label = "Individual ID", # 个体ID
choices = levels(factor(Data$Indiv)),
selected = NULL, # 默认选择不指定个体
multiple = FALSE # 允许选择多个选项
),
p("选择一个个体,或点击按钮随机选择一个个体。"),
),
# 主面板
mainPanel(
plotOutput("IndivPlot"), # 图表对象的名称
),
)
)
# 服务器端
server <- function(input, output, session) {
# 用于保存动态ID
rv <- reactiveValues()
observe({
# 从下拉菜单手动选择的ID
rv$ID <- input$ID
})
# 绘图
output$IndivPlot <- renderPlot({ # 生成动态调整的图表
### 仅包括选定的个体
Data <- Data[Data$Indiv %in% rv$ID,]
# 创建图表
p = ggplot(data=Data, aes(x=varX, y=varY))+
geom_line()+
expand_limits(y=c(1,30), x = c(1,10))+
scale_y_continuous(breaks=seq(1,30,5), labels=seq(1,30,5))+
scale_x_continuous(breaks = seq(1,10,1), labels = seq(1,10,1))+
labs(x = "My X", y = "My Y")
print(p)
})
# 点击动作按钮时
observeEvent(input$do, {
# 选择随机ID
rv$ID <- sample(unique(Data$Indiv), 1)
# 更新下拉菜单的值,选择随机选择的ID
updateSelectInput(session, inputId = "ID",
label = "Individual ID",
choices = levels(factor(Data$Indiv)),
selected = rv$ID)
})
}
# 启动Shiny应用程序
shinyApp(ui = ui, server = server)
这是代码的翻译部分,不包含问题的回答。
英文:
You may create a reactive ID which will be dynamic. It will be updated when the ID is selected from the dropdown as well as when the action button is pressed to generate random ID.
The plot will be updated based on the ID.
library(shiny)
library(tidyverse)
###### Shiny App #######
# User interface for shiny app
ui <- fluidPage(
titlePanel("Random Select Button"), # page title
sidebarLayout(
# Sidebar panel
sidebarPanel(
# input for which TagID to display in plot
actionButton("do", "Plot Random Indiv"),
selectInput(inputId = "ID",
label = "Individual ID",
choices = levels(factor(Data$Indiv)),
selected = NULL, # choose no individuals by default
multiple = FALSE # allow for multiple options
),
p("Choose an invidual, or push the button to randomly choose an individual."),
),
# Main Panel
mainPanel(
plotOutput("IndivPlot"), # name of plot object
),
)
)
# Server
server <- function(input, output, session) {
#To save dynamic ID
rv <- reactiveValues()
observe({
#ID selected from the dropdown manually
rv$ID <- input$ID
})
# Plot
output$IndivPlot <- renderPlot({ # make a live adjustable plot
### Filter to only include selected Individual
Data <- Data[Data$Indiv %in% rv$ID,]
# Create plot
p = ggplot(data=Data, aes(x=varX, y=varY))+
geom_line()+
expand_limits(y=c(1,30), x = c(1,10))+
scale_y_continuous(breaks=seq(1,30,5), labels=seq(1,30,5))+
scale_x_continuous(breaks = seq(1,10,1), labels = seq(1,10,1))+
labs(x = "My X", y = "My Y")
print(p)
})
#On action button click
observeEvent(input$do, {
#Select a random ID
rv$ID <- sample(unique(Data$Indiv), 1)
#Update the value of dropdown with random ID selected
updateSelectInput(session, inputId = "ID",
label = "Individual ID",
choices = levels(factor(Data$Indiv)),
selected = rv$ID)
})
}
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论