【发布时间】:2020-05-20 05:12:48
【问题描述】:
我对 Shiny 还很陌生。在下面的应用程序中,如果两个 textInput 框中只有一个被填充,我希望能够呈现数据表。如果两个框都被填充,应用程序工作正常。但我希望数据表只如果只填写一个变量,则渲染一个变量(例如只输入X变量名,表格中只显示X变量数据)。
library(shiny)
library(dplyr)
library(DT)
data("mtcars")
ui <- fluidPage(
titlePanel("Example 01"),
sidebarLayout(
sidebarPanel(
textInput("x", "X Variable"),
textInput("y", "Y Variable")
),
mainPanel(
dataTableOutput(outputId = "table")
)
)
)
server <- function(input, output) {
tabledata <- reactive({
x <- mtcars %>% select(input$x) * 2
y <- mtcars %>% select(input$y) * 3
if (is.null(x) == TRUE){y}
if (is.null(y) == TRUE){x}
else {cbind(x,y)}
})
output$table <- renderDataTable(tabledata())
}
shinyApp(ui = ui, server = server)
为了测试,我输入了 mpg 作为 X 变量,输入 cyl 作为 Y 变量。如果您想知道为什么我使用 textInput 而不是 selectInput,那是因为我使用它的项目有 100,000 多个搜索选项(音乐艺术家)。
提前致谢!
【问题讨论】: