香港专业教育学院刚刚开始使用闪亮,并得到以下基本问题。

1)上载的纵向数据包括带有处理名称(例如A,B,C,D)的列,另一列包含相应的数字代码:例如1,2,4,6。编码可能会有所不同,具体取决于上传的数据。每种治疗均针对一组患者。

我想使用数字代码选择要比较的处理方式,例如numericInput()。我需要根据提供的实际数据集中的编码来更新列表。到目前为止,我已经使用numericInput()做到了这一点,假设编码将在1到10之间(请参见下面的代码)。

2)如果我想根据治疗名称(此处为A,B,C,D)进行选择,该名称在感兴趣的数据集之间可能会有所不同?

帮助非常感谢。

shinyServer(function(input, output){
## Data reactives:
uploaded_Data <- reactive({
    inFile <- input $ data
    if(is.null(inFile)){return(NULL)}
    read.csv(file = inFile $ datapath,
             header=TRUE)

output $ raw_data <- renderTable({
    uploaded_Data()
})## for table


})

shinyUI(pageWithSidebar(
headerPanel(''),
sidebarPanel(
    fileInput('data', 'File to upload (.csv only):',
              accept=c('.csv')),
    tags $ hr(),
    h4('Select treatments:'),
    numericInput('T1','Treatment1 code:',1, min=1, max=10, step=1),
    numericInput('T2','Treatment2 code:',2, min=2, max=10, step=1)
    ),

## Option for tabsets:
mainPanel(
    tabsetPanel(
        tabPanel('Uploaded Data',
                 tableOutput('raw_data'))
        )
    )
))## overall

最佳答案

我认为您要问的是如何根据上传的数据呈现动态UI输入?

如果是这种情况,请尝试将以下策略集成到您的应用中:

server.R:

#incomplete example
output$groupsToCompare <- renderUI({
  data <- uploaded_data()
  if(is.null(data)){return(NULL)} #This prevents an error if no data is loaded

  #In this example I will use selectInput, but you can also use checkboxInput or whatever really
  selectInput(inputId = 'selectedGroups', label = 'Select which groups to compare', choices = unique(data$treatments), multiple = TRUE)
})

#an example of how to use this input object
output$dataToShow <- renderTable ({
  data <- uploaded_data()
  if(is.null(data)){return(NULL)}

  #subset the data of interest; There are MANY ways to do this, I'm being verbose
  subsetData <- subset(data, input$selectedGroups)

  #alternatively, you could do data[input$selectedGroups]
  return(subsetData)
})


ui.R:

#incomplete example
uiOutput('selectedGroups')


这将动态生成可以选择作为输入的唯一因素的列表。在您的情况下,它将生成“ A”,“ B”,“ C”,“ D”的列表或数字因子列表。这些输入可用于子集数据,或为您梦dream以求的某些变量。我认为这可以回答您的两个问题,但如果不能,请告诉我,以便我澄清。我以前没有用selectInput测试multiple = TRUE,但是我想它会很好用。刚开始这会感觉很不寻常,因为您基本上是在server.R中设置UI元素的,您曾经在ui.R中构建UI,但是一旦执行几次,它就会变得很吸引人。

10-02 02:01
查看更多