如何基于 R Shiny 中的单选按钮显示输出?
How to display outputs based on radio button in R Shiny?
您好,我对 R Shiny 有疑问。我在服务器端准备了 3 个“输出”——一个显示数据集 table,一个显示 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)
})
UI:
.
.
.
sidebarPanel(
radioButtons(
"dman_preview", "Display:",
c("preview", "str", "summary"),
selected = "preview",
inline = TRUE
),
),
mainPanel(
tableOutput("contents"),
verbatimTextOutput("summary"),
verbatimTextOutput("str")
)
也许您正在寻找这个
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 有疑问。我在服务器端准备了 3 个“输出”——一个显示数据集 table,一个显示 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)
})
UI:
.
.
.
sidebarPanel(
radioButtons(
"dman_preview", "Display:",
c("preview", "str", "summary"),
selected = "preview",
inline = TRUE
),
),
mainPanel(
tableOutput("contents"),
verbatimTextOutput("summary"),
verbatimTextOutput("str")
)
也许您正在寻找这个
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)