如何根据 R Shiny 中的单选按钮显示输出?
Posted
技术标签:
【中文标题】如何根据 R Shiny 中的单选按钮显示输出?【英文标题】:How to display outputs based on radio button in R Shiny? 【发布时间】:2022-01-17 18:03:50 【问题描述】:您好,我对 R Shiny 有疑问。我在服务器端准备了 3 个“输出”——一个用于显示数据集表,一个用于显示 summary(),一个用于显示 str()。然后我准备了单选按钮preview、str和summary。而我想要实现的是基于单选按钮选择的输出的反应性变化。
服务器:
.
.
.
output$contents =
renderTable(
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
if(input$disp == "head")
return(head(df))
else
return(df)
)
output$summary =
renderPrint(
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
summary(df)
)
output$str =
renderPrint(
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
str(df)
)
用户界面:
.
.
.
sidebarPanel(
radioButtons(
"dman_preview", "Display:",
c("preview", "str", "summary"),
selected = "preview",
inline = TRUE
),
),
mainPanel(
tableOutput("contents"),
verbatimTextOutput("summary"),
verbatimTextOutput("str")
)
【问题讨论】:
【参考方案1】:也许你正在寻找这个
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
radioButtons(
"dman_preview", "Display:",
c("preview", "str", "summary"),
selected = "preview",
inline = TRUE
),
),
mainPanel(
uiOutput("myoutput")
)
)
)
server <- function(input, output)
df1 <- reactive(
# req(input$file1)
# df <- read.csv(input$file1$datapath,
# header = input$header,
# sep = input$sep,
# quote = input$quote)
# df
cars ## temporary dataframe for testing purpose
)
output$contents <- renderTable(
# if(input$disp == "head")
# return(head(df1()))
#
# else
# return(df1())
#
df1()
)
output$summary <- renderPrint(
summary(df1())
)
output$str <- renderPrint( str(df1()) )
output$myoutput <- renderUI(
switch(input$dman_preview,
"preview" = tableOutput("contents"),
"str" = verbatimTextOutput("summary"),
"summary" = verbatimTextOutput("str")
)
)
shinyApp(ui = ui, server = server)
【讨论】:
它可以按我的需要工作,谢谢以上是关于如何根据 R Shiny 中的单选按钮显示输出?的主要内容,如果未能解决你的问题,请参考以下文章