我是 R 新手,试图在 R 中完成以下功能 我创建了一个数据框 book3
Region<- c("Americas", "Asia Pacific","Asia Pacific", "EMEA", "EMEA")
Country<- c("Mexico", "China","India", "Germany", "Spain" )
Rating<- c(5,3,3,2,4)
book3<- data.frame(Region, Country, Rating)
我想拉出将成为被告的地区和国家。我正在尝试使用此代码
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Test Dashboard "),
sidebarLayout(
sidebarPanel(
selectInput("data1", "Select Region", choices = c(book3$Region)),
selectInput("data2", "select country", choices = c(book3$Country))
),
mainPanel()
)))
最佳答案
如果您要基于数据创建输入,则需要在 server.R 中创建输入,以便可以访问数据。您可以通过使用 renderUI
和 uiOutput
来完成此操作。请参阅here for reference
这是一个工作示例。请注意 server
中的 renderUI( )
函数,它根据数据生成 selectInput
library(shiny)
ui <- fluidPage(
titlePanel("Test Dashboard "),
sidebarLayout(
sidebarPanel(
uiOutput("data1"), ## uiOutput - gets the UI from the server
uiOutput("data2")
),
mainPanel()
))
server <- function(input, output){
Region<- c("Americas", "Asia Pacific","Asia Pacific", "EMEA", "EMEA")
Country<- c("Mexico", "China","India", "Germany", "Spain" )
Rating<- c(5,3,3,2,4)
book3<- data.frame(Region, Country, Rating, stringsAsFactors = F)
## renderUI - renders a UI element on the server
## used when the UI element is dynamic/dependant on data
output$data1 <- renderUI({
selectInput("data1", "Select Region", choices = c(book3$Region))
})
## input dependant on the choices in `data1`
output$data2 <- renderUI({
selectInput("data2", "select country", choices = c(book3$Country[book3$Region == input$data1]))
})
}
shinyApp(ui, server)
关于r - 在 Shiny R 中创建附属选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38007070/