r - 在 Shiny R 中创建附属选择输入

标签 r shiny

我是 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 中创建输入,以便可以访问数据。您可以通过使用 renderUIuiOutput 来完成此操作。请参阅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/

相关文章:

r - 基于值通过R中的ggmap生成空间热图

Rmarkdown 交互文档 Bootswatch 主题颜色不正确

r - Shiny:通过单击 valueBox 触发弹出窗口

r - 第一个 Shiny 应用程序出错

r - echarts4r中如何指定坐标轴值的顺序

r - 在 R 中处理填充轮廓图的颜色

r - 这似乎工作正常但保持 "command #Bugs:gen.inits cannot be executed (is greyed out)"

r - ggplot2中的框图之间的间距

javascript - 为 Shiny 应用程序添加 'Calculation In Process' 指示器

css - 去除shinydashboardPlus中右边栏宽度改变时出现的多余空间